Docs: Python: Add copy button to code blocks #119948

Closed
Aaron Carlisle wants to merge 1 commits from Blendify:doc-python-copybutton into main

When changing the target branch, be careful to rebase the branch in your fork to match. See documentation.
Member

Uses the sphinx-copybutton to add a copy icon to code block to quickly copy content to the clipboard.

Uses the sphinx-copybutton to add a copy icon to code block to quickly copy content to the clipboard.
Aaron Carlisle added 1 commit 2024-03-27 05:51:01 +01:00
81cd5e0c08 Docs: Python: Add copy button to code blocks
Uses the sphinx-copybutton to add a copy icon to code block to quickly copy content to the clipboard.
Aaron Carlisle requested review from Campbell Barton 2024-03-27 05:54:06 +01:00
Campbell Barton reviewed 2024-03-27 06:20:56 +01:00
Campbell Barton left a comment
Owner

This seems OK but I'm not keen on making it a hard requirement.

This patch adds a function to make it simpler to check if modules import (also used by furo):

diff --git a/doc/python_api/conf.py b/doc/python_api/conf.py
index 02f95930ae2..8587c8d8091 100644
--- a/doc/python_api/conf.py
+++ b/doc/python_api/conf.py
@@ -5,6 +5,17 @@
 import time
 
 
+def has_module(module_name):
+    found = False
+    try:
+        __import__(module_name)
+        found = True
+    except ModuleNotFoundError as ex:
+        if ex.name != module_name:
+            raise ex
+    return found
+
+
 # These are substituted when this file is copied to the build directory.
 BLENDER_VERSION_STRING = "${BLENDER_VERSION_STRING}"
 BLENDER_VERSION_DOTS = "${BLENDER_VERSION_DOTS}"
@@ -27,6 +38,16 @@ else:
 
 extensions = ["sphinx.ext.intersphinx"]
 intersphinx_mapping = {"blender_manual": ("https://docs.blender.org/manual/en/dev/", None)}
+
+
+# Provides copy button next to code-blocks (nice to have but not essential).
+if has_module("sphinx_copybutton"):
+    extensions.append("sphinx_copybutton")
+
+    # Exclude line numbers, prompts, and console text.
+    copybutton_exclude = ".linenos, .gp, .go"
+
+
 project = "Blender %s Python API" % BLENDER_VERSION_STRING
 root_doc = "index"
 copyright = "Blender Authors"
@@ -48,14 +69,8 @@ html_title = "Blender Python API"
 # The fallback to a built-in theme when `furo` is not found.
 html_theme = "default"
 
-try:
-    import furo
+if has_module("furo"):
     html_theme = "furo"
-    del furo
-except ModuleNotFoundError:
-    pass
-
-if html_theme == "furo":
     html_theme_options = {
         "light_css_variables": {
             "color-brand-primary": "#265787",
diff --git a/doc/python_api/requirements.txt b/doc/python_api/requirements.txt
index 70c385b5e74..8bfd02f05f8 100644
--- a/doc/python_api/requirements.txt
+++ b/doc/python_api/requirements.txt
@@ -11,3 +11,6 @@ requests==2.31.0
 # Without this theme, the default theme will be used.
 furo==2024.1.29
 sphinx-basic-ng==1.0.0b2
+
+# Show a copy button (convenience only).
+sphinx-copybutton==0.5.2
This seems OK but I'm not keen on making it a hard requirement. This patch adds a function to make it simpler to check if modules import (also used by `furo`): ``` diff diff --git a/doc/python_api/conf.py b/doc/python_api/conf.py index 02f95930ae2..8587c8d8091 100644 --- a/doc/python_api/conf.py +++ b/doc/python_api/conf.py @@ -5,6 +5,17 @@ import time +def has_module(module_name): + found = False + try: + __import__(module_name) + found = True + except ModuleNotFoundError as ex: + if ex.name != module_name: + raise ex + return found + + # These are substituted when this file is copied to the build directory. BLENDER_VERSION_STRING = "${BLENDER_VERSION_STRING}" BLENDER_VERSION_DOTS = "${BLENDER_VERSION_DOTS}" @@ -27,6 +38,16 @@ else: extensions = ["sphinx.ext.intersphinx"] intersphinx_mapping = {"blender_manual": ("https://docs.blender.org/manual/en/dev/", None)} + + +# Provides copy button next to code-blocks (nice to have but not essential). +if has_module("sphinx_copybutton"): + extensions.append("sphinx_copybutton") + + # Exclude line numbers, prompts, and console text. + copybutton_exclude = ".linenos, .gp, .go" + + project = "Blender %s Python API" % BLENDER_VERSION_STRING root_doc = "index" copyright = "Blender Authors" @@ -48,14 +69,8 @@ html_title = "Blender Python API" # The fallback to a built-in theme when `furo` is not found. html_theme = "default" -try: - import furo +if has_module("furo"): html_theme = "furo" - del furo -except ModuleNotFoundError: - pass - -if html_theme == "furo": html_theme_options = { "light_css_variables": { "color-brand-primary": "#265787", diff --git a/doc/python_api/requirements.txt b/doc/python_api/requirements.txt index 70c385b5e74..8bfd02f05f8 100644 --- a/doc/python_api/requirements.txt +++ b/doc/python_api/requirements.txt @@ -11,3 +11,6 @@ requests==2.31.0 # Without this theme, the default theme will be used. furo==2024.1.29 sphinx-basic-ng==1.0.0b2 + +# Show a copy button (convenience only). +sphinx-copybutton==0.5.2 ```
Aaron Carlisle closed this pull request 2024-03-30 01:42:19 +01:00
Aaron Carlisle deleted branch doc-python-copybutton 2024-03-30 01:42:31 +01:00

Pull request closed

Sign in to join this conversation.
No reviewers
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender#119948
No description provided.