This commit is contained in:
Tom van Woudenberg 2025-12-19 13:55:25 +01:00 • committed by GitHub
parent 88c67fe4cd
commit b3469006d4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -16,14 +16,60 @@
"\n",
"# Interactive plots: plotly\n",
"\n",
"The use of the [plotly](https://plotly.com/python/getting-started/) package is an example which allows you to create HTML/Javascript widgets from python code without any knowledge on HTML/Javascript."
"The use of the [plotly](https://plotly.com/python/getting-started/) package is an example which allows you to create HTML/Javascript widgets from python code without any knowledge on HTML/Javascript. The example below is taken from the FEM-module by Frans van der Meer."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If you want to create the plot during the build of the book, don't use `fig.show()` but use the code below:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from IPython import get_ipython\n",
"from IPython.display import HTML\n",
"\n",
"def show_plotly(fig):\n",
" ip = get_ipython()\n",
" if ip is None:\n",
" return fig\n",
"\n",
" # Sphinx / Jupyter Book execution\n",
" if \"sphinx\" in ip.config:\n",
" return HTML(fig.to_html(include_plotlyjs=\"cdn\"))\n",
"\n",
" # Local execution in Jupyter Notebook / JupyterLab / VS Code\n",
" fig.show()\n",
"\n",
"....\n",
"\n",
"show_plotly(fig)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Make sure you have in your `_config.yml`:\n",
"\n",
"```yaml\n",
"execute:\n",
" execute_notebooks: \"auto\"\n",
"```\n",
"\n",
"Eventually in combination with [](../_git/github.com_TeachBooks_Sphinx-NB-Execution-Patterns/Manual/README.md)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The example below is taken from the FEM-module by Frans van der Meer.\n",
"When using Plotly graphs, make sure the notebook is executed in Jupyter Lab / Jupyter Notebook for the figures to be shown in the book. Running the code in VS code might break the result in the book, although the output is visible in VS code. Alternatively, add the following lines of code to your notebook to have a valid output in the book as well:\n",
"\n",
"```python\n",
@ -437,58 +483,6 @@
"\n",
"fig.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If you want to create the plot during the build of the book, don't use `fig.show()` but use the code below:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from IPython import get_ipython\n",
"from IPython.display import HTML\n",
"\n",
"def show_plotly(fig):\n",
" ip = get_ipython()\n",
" if ip is None:\n",
" return fig\n",
"\n",
" # Sphinx / Jupyter Book execution\n",
" if \"sphinx\" in ip.config:\n",
" return HTML(fig.to_html(include_plotlyjs=\"cdn\"))\n",
"\n",
" # Local execution in Jupyter Notebook / JupyterLab / VS Code\n",
" fig.show()\n",
"\n",
"....\n",
"\n",
"show_plotly(fig)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Make sure you have in your `_config.yml`:\n",
"\n",
"```yaml\n",
"execute:\n",
" execute_notebooks: \"auto\"\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Eventually in combination with [](../_git/github.com_TeachBooks_Sphinx-NB-Execution-Patterns/Manual/README.md)"
]
}
],
"metadata": {