Reset repository and upload final project (part 20)
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
- .venv_haddock/lib/python3.12/site-packages/plotly-6.5.2.dist-info/._INSTALLER +0 -0
- .venv_haddock/lib/python3.12/site-packages/plotly-6.5.2.dist-info/._METADATA +0 -0
- .venv_haddock/lib/python3.12/site-packages/plotly-6.5.2.dist-info/._RECORD +0 -0
- .venv_haddock/lib/python3.12/site-packages/plotly-6.5.2.dist-info/._WHEEL +0 -0
- .venv_haddock/lib/python3.12/site-packages/plotly-6.5.2.dist-info/._entry_points.txt +0 -0
- .venv_haddock/lib/python3.12/site-packages/plotly-6.5.2.dist-info/._licenses +0 -0
- .venv_haddock/lib/python3.12/site-packages/plotly-6.5.2.dist-info/._top_level.txt +0 -0
- .venv_haddock/lib/python3.12/site-packages/plotly-6.5.2.dist-info/INSTALLER +1 -0
- .venv_haddock/lib/python3.12/site-packages/plotly-6.5.2.dist-info/METADATA +239 -0
- .venv_haddock/lib/python3.12/site-packages/plotly-6.5.2.dist-info/RECORD +0 -0
- .venv_haddock/lib/python3.12/site-packages/plotly-6.5.2.dist-info/WHEEL +5 -0
- .venv_haddock/lib/python3.12/site-packages/plotly-6.5.2.dist-info/entry_points.txt +2 -0
- .venv_haddock/lib/python3.12/site-packages/plotly-6.5.2.dist-info/licenses/._LICENSE.txt +0 -0
- .venv_haddock/lib/python3.12/site-packages/plotly-6.5.2.dist-info/licenses/LICENSE.txt +21 -0
- .venv_haddock/lib/python3.12/site-packages/plotly-6.5.2.dist-info/top_level.txt +2 -0
- .venv_haddock/lib/python3.12/site-packages/plotly/io/.__utils.py +0 -0
- .venv_haddock/lib/python3.12/site-packages/plotly/io/._base_renderers.py +0 -0
- .venv_haddock/lib/python3.12/site-packages/plotly/io/._json.py +0 -0
- .venv_haddock/lib/python3.12/site-packages/plotly/io/._kaleido.py +0 -0
- .venv_haddock/lib/python3.12/site-packages/plotly/io/._orca.py +0 -0
- .venv_haddock/lib/python3.12/site-packages/plotly/io/__init__.py +70 -0
- .venv_haddock/lib/python3.12/site-packages/plotly/io/_base_renderers.py +846 -0
- .venv_haddock/lib/python3.12/site-packages/plotly/io/_defaults.py +19 -0
- .venv_haddock/lib/python3.12/site-packages/plotly/io/_html.py +517 -0
- .venv_haddock/lib/python3.12/site-packages/plotly/io/_json.py +598 -0
- .venv_haddock/lib/python3.12/site-packages/plotly/io/_kaleido.py +918 -0
- .venv_haddock/lib/python3.12/site-packages/plotly/io/_orca.py +1670 -0
- .venv_haddock/lib/python3.12/site-packages/plotly/io/_renderers.py +567 -0
- .venv_haddock/lib/python3.12/site-packages/plotly/io/_sg_scraper.py +100 -0
- .venv_haddock/lib/python3.12/site-packages/plotly/io/_templates.py +492 -0
- .venv_haddock/lib/python3.12/site-packages/plotly/io/_utils.py +93 -0
- .venv_haddock/lib/python3.12/site-packages/plotly/io/base_renderers.py +17 -0
- .venv_haddock/lib/python3.12/site-packages/plotly/io/json.py +10 -0
- .venv_haddock/lib/python3.12/site-packages/plotly/io/kaleido.py +12 -0
- .venv_haddock/lib/python3.12/site-packages/plotly/io/orca.py +9 -0
- .venv_haddock/lib/python3.12/site-packages/plotly/labextension/._package.json +0 -0
- .venv_haddock/lib/python3.12/site-packages/plotly/labextension/._static +0 -0
- .venv_haddock/lib/python3.12/site-packages/plotly/labextension/package.json +39 -0
- .venv_haddock/lib/python3.12/site-packages/plotly/labextension/static/._340.b079a9b51d0cddaf4ab3.js +0 -0
- .venv_haddock/lib/python3.12/site-packages/plotly/labextension/static/._remoteEntry.a1fdd5414cdd3506ffce.js +0 -0
- .venv_haddock/lib/python3.12/site-packages/plotly/labextension/static/._style.js +0 -0
- .venv_haddock/lib/python3.12/site-packages/plotly/labextension/static/340.b079a9b51d0cddaf4ab3.js +0 -0
- .venv_haddock/lib/python3.12/site-packages/plotly/labextension/static/remoteEntry.a1fdd5414cdd3506ffce.js +1 -0
- .venv_haddock/lib/python3.12/site-packages/plotly/labextension/static/style.js +4 -0
- .venv_haddock/lib/python3.12/site-packages/plotly/matplotlylib/.___init__.py +0 -0
- .venv_haddock/lib/python3.12/site-packages/plotly/matplotlylib/.___pycache__ +0 -0
- .venv_haddock/lib/python3.12/site-packages/plotly/matplotlylib/._mplexporter +0 -0
- .venv_haddock/lib/python3.12/site-packages/plotly/matplotlylib/._mpltools.py +0 -0
- .venv_haddock/lib/python3.12/site-packages/plotly/matplotlylib/._renderer.py +0 -0
- .venv_haddock/lib/python3.12/site-packages/plotly/matplotlylib/._tests +0 -0
.venv_haddock/lib/python3.12/site-packages/plotly-6.5.2.dist-info/._INSTALLER
ADDED
|
Binary file (4.1 kB). View file
|
|
|
.venv_haddock/lib/python3.12/site-packages/plotly-6.5.2.dist-info/._METADATA
ADDED
|
Binary file (4.1 kB). View file
|
|
|
.venv_haddock/lib/python3.12/site-packages/plotly-6.5.2.dist-info/._RECORD
ADDED
|
Binary file (4.1 kB). View file
|
|
|
.venv_haddock/lib/python3.12/site-packages/plotly-6.5.2.dist-info/._WHEEL
ADDED
|
Binary file (4.1 kB). View file
|
|
|
.venv_haddock/lib/python3.12/site-packages/plotly-6.5.2.dist-info/._entry_points.txt
ADDED
|
Binary file (4.1 kB). View file
|
|
|
.venv_haddock/lib/python3.12/site-packages/plotly-6.5.2.dist-info/._licenses
ADDED
|
Binary file (4.1 kB). View file
|
|
|
.venv_haddock/lib/python3.12/site-packages/plotly-6.5.2.dist-info/._top_level.txt
ADDED
|
Binary file (4.1 kB). View file
|
|
|
.venv_haddock/lib/python3.12/site-packages/plotly-6.5.2.dist-info/INSTALLER
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
pip
|
.venv_haddock/lib/python3.12/site-packages/plotly-6.5.2.dist-info/METADATA
ADDED
|
@@ -0,0 +1,239 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Metadata-Version: 2.4
|
| 2 |
+
Name: plotly
|
| 3 |
+
Version: 6.5.2
|
| 4 |
+
Summary: An open-source interactive data visualization library for Python
|
| 5 |
+
Author-email: Chris P <chris@plot.ly>
|
| 6 |
+
Maintainer-email: Martha Cryan <martha.cryan@plot.ly>, Emily Kellison-Linn <emily@plot.ly>
|
| 7 |
+
License: MIT License
|
| 8 |
+
|
| 9 |
+
Copyright (c) 2016-2024 Plotly Technologies Inc.
|
| 10 |
+
|
| 11 |
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
| 12 |
+
of this software and associated documentation files (the "Software"), to deal
|
| 13 |
+
in the Software without restriction, including without limitation the rights
|
| 14 |
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
| 15 |
+
copies of the Software, and to permit persons to whom the Software is
|
| 16 |
+
furnished to do so, subject to the following conditions:
|
| 17 |
+
|
| 18 |
+
The above copyright notice and this permission notice shall be included in
|
| 19 |
+
all copies or substantial portions of the Software.
|
| 20 |
+
|
| 21 |
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
| 22 |
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
| 23 |
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
| 24 |
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
| 25 |
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
| 26 |
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
| 27 |
+
THE SOFTWARE.
|
| 28 |
+
|
| 29 |
+
Project-URL: HomePage, https://plotly.com/python/
|
| 30 |
+
Project-URL: Documentation, https://plotly.com/python/
|
| 31 |
+
Project-URL: Github, https://github.com/plotly/plotly.py
|
| 32 |
+
Project-URL: Changelog, https://github.com/plotly/plotly.py/blob/main/CHANGELOG.md
|
| 33 |
+
Classifier: Development Status :: 5 - Production/Stable
|
| 34 |
+
Classifier: Programming Language :: Python :: 3.8
|
| 35 |
+
Classifier: Programming Language :: Python :: 3.9
|
| 36 |
+
Classifier: Programming Language :: Python :: 3.10
|
| 37 |
+
Classifier: Programming Language :: Python :: 3.11
|
| 38 |
+
Classifier: Programming Language :: Python :: 3.12
|
| 39 |
+
Classifier: Programming Language :: Python :: 3.13
|
| 40 |
+
Classifier: Topic :: Scientific/Engineering :: Visualization
|
| 41 |
+
Classifier: License :: OSI Approved :: MIT License
|
| 42 |
+
Requires-Python: >=3.8
|
| 43 |
+
Description-Content-Type: text/markdown
|
| 44 |
+
License-File: LICENSE.txt
|
| 45 |
+
Requires-Dist: narwhals>=1.15.1
|
| 46 |
+
Requires-Dist: packaging
|
| 47 |
+
Provides-Extra: express
|
| 48 |
+
Requires-Dist: numpy; extra == "express"
|
| 49 |
+
Provides-Extra: kaleido
|
| 50 |
+
Requires-Dist: kaleido>=1.1.0; extra == "kaleido"
|
| 51 |
+
Provides-Extra: dev-core
|
| 52 |
+
Requires-Dist: pytest; extra == "dev-core"
|
| 53 |
+
Requires-Dist: requests; extra == "dev-core"
|
| 54 |
+
Requires-Dist: ruff==0.11.12; extra == "dev-core"
|
| 55 |
+
Provides-Extra: dev-build
|
| 56 |
+
Requires-Dist: plotly[dev_core]; extra == "dev-build"
|
| 57 |
+
Requires-Dist: build; extra == "dev-build"
|
| 58 |
+
Requires-Dist: jupyter; extra == "dev-build"
|
| 59 |
+
Provides-Extra: dev-optional
|
| 60 |
+
Requires-Dist: plotly[dev_build]; extra == "dev-optional"
|
| 61 |
+
Requires-Dist: plotly[kaleido]; extra == "dev-optional"
|
| 62 |
+
Requires-Dist: anywidget; extra == "dev-optional"
|
| 63 |
+
Requires-Dist: colorcet; extra == "dev-optional"
|
| 64 |
+
Requires-Dist: fiona<=1.9.6; python_version <= "3.8" and extra == "dev-optional"
|
| 65 |
+
Requires-Dist: geopandas; extra == "dev-optional"
|
| 66 |
+
Requires-Dist: inflect; extra == "dev-optional"
|
| 67 |
+
Requires-Dist: numpy; extra == "dev-optional"
|
| 68 |
+
Requires-Dist: orjson; extra == "dev-optional"
|
| 69 |
+
Requires-Dist: pandas; extra == "dev-optional"
|
| 70 |
+
Requires-Dist: pdfrw; extra == "dev-optional"
|
| 71 |
+
Requires-Dist: pillow; extra == "dev-optional"
|
| 72 |
+
Requires-Dist: plotly-geo; extra == "dev-optional"
|
| 73 |
+
Requires-Dist: polars[timezone]; extra == "dev-optional"
|
| 74 |
+
Requires-Dist: pyarrow; extra == "dev-optional"
|
| 75 |
+
Requires-Dist: pyshp; extra == "dev-optional"
|
| 76 |
+
Requires-Dist: pytz; extra == "dev-optional"
|
| 77 |
+
Requires-Dist: scikit-image; extra == "dev-optional"
|
| 78 |
+
Requires-Dist: scipy; extra == "dev-optional"
|
| 79 |
+
Requires-Dist: shapely; extra == "dev-optional"
|
| 80 |
+
Requires-Dist: statsmodels; extra == "dev-optional"
|
| 81 |
+
Requires-Dist: vaex; python_version <= "3.9" and extra == "dev-optional"
|
| 82 |
+
Requires-Dist: xarray; extra == "dev-optional"
|
| 83 |
+
Provides-Extra: dev
|
| 84 |
+
Requires-Dist: plotly[dev_optional]; extra == "dev"
|
| 85 |
+
Dynamic: license-file
|
| 86 |
+
|
| 87 |
+
# plotly.py
|
| 88 |
+
|
| 89 |
+
<table>
|
| 90 |
+
<tr>
|
| 91 |
+
<td>Latest Release</td>
|
| 92 |
+
<td>
|
| 93 |
+
<a href="https://pypi.org/project/plotly/"/>
|
| 94 |
+
<img src="https://badge.fury.io/py/plotly.svg"/>
|
| 95 |
+
</td>
|
| 96 |
+
</tr>
|
| 97 |
+
<tr>
|
| 98 |
+
<td>User forum</td>
|
| 99 |
+
<td>
|
| 100 |
+
<a href="https://community.plotly.com/"/>
|
| 101 |
+
<img src="https://img.shields.io/badge/help_forum-discourse-blue.svg"/>
|
| 102 |
+
</td>
|
| 103 |
+
</tr>
|
| 104 |
+
<tr>
|
| 105 |
+
<td>PyPI Downloads</td>
|
| 106 |
+
<td>
|
| 107 |
+
<a href="https://pepy.tech/project/plotly"/>
|
| 108 |
+
<img src="https://pepy.tech/badge/plotly/month"/>
|
| 109 |
+
</td>
|
| 110 |
+
</tr>
|
| 111 |
+
<tr>
|
| 112 |
+
<td>License</td>
|
| 113 |
+
<td>
|
| 114 |
+
<a href="https://opensource.org/licenses/MIT"/>
|
| 115 |
+
<img src="https://img.shields.io/badge/License-MIT-yellow.svg"/>
|
| 116 |
+
</td>
|
| 117 |
+
</tr>
|
| 118 |
+
</table>
|
| 119 |
+
|
| 120 |
+
<div align="center">
|
| 121 |
+
<a href="https://dash.plotly.com/project-maintenance">
|
| 122 |
+
<img src="https://dash.plotly.com/assets/images/maintained-by-plotly.png" width="400px" alt="Maintained by Plotly">
|
| 123 |
+
</a>
|
| 124 |
+
</div>
|
| 125 |
+
|
| 126 |
+
## Quickstart
|
| 127 |
+
|
| 128 |
+
`pip install plotly`
|
| 129 |
+
|
| 130 |
+
```python
|
| 131 |
+
import plotly.express as px
|
| 132 |
+
fig = px.bar(x=["a", "b", "c"], y=[1, 3, 2])
|
| 133 |
+
fig.show()
|
| 134 |
+
```
|
| 135 |
+
|
| 136 |
+
See the [Python documentation](https://plotly.com/python/) for more examples.
|
| 137 |
+
|
| 138 |
+
## Overview
|
| 139 |
+
|
| 140 |
+
[plotly.py](https://plotly.com/python/) is an interactive, open-source, and browser-based graphing library for Python :sparkles:
|
| 141 |
+
|
| 142 |
+
Built on top of [plotly.js](https://github.com/plotly/plotly.js), `plotly.py` is a high-level, declarative charting library. plotly.js ships with over 30 chart types, including scientific charts, 3D graphs, statistical charts, SVG maps, financial charts, and more.
|
| 143 |
+
|
| 144 |
+
`plotly.py` is [MIT Licensed](https://github.com/plotly/plotly.py/blob/main/LICENSE.txt). Plotly graphs can be viewed in [Jupyter notebooks](https://jupyter.org), other Python notebook software such as [marimo](https://marimo.io), as standalone HTML files, or integrated into [Dash applications](https://dash.plotly.com/).
|
| 145 |
+
|
| 146 |
+
[Contact us](https://plotly.com/consulting-and-oem/) for consulting, dashboard development, application integration, and feature additions.
|
| 147 |
+
|
| 148 |
+
<p align="center">
|
| 149 |
+
<a href="https://plotly.com/python/" target="_blank">
|
| 150 |
+
<img src="https://raw.githubusercontent.com/cldougl/plot_images/add_r_img/plotly_2017.png">
|
| 151 |
+
</a></p>
|
| 152 |
+
|
| 153 |
+
---
|
| 154 |
+
|
| 155 |
+
- [Online Documentation](https://plotly.com/python/)
|
| 156 |
+
- [Contributing to plotly](https://github.com/plotly/plotly.py/blob/main/CONTRIBUTING.md)
|
| 157 |
+
- [Changelog](https://github.com/plotly/plotly.py/blob/main/CHANGELOG.md)
|
| 158 |
+
- [Code of Conduct](https://github.com/plotly/plotly.py/blob/main/CODE_OF_CONDUCT.md)
|
| 159 |
+
- [Community forum](https://community.plotly.com)
|
| 160 |
+
|
| 161 |
+
---
|
| 162 |
+
|
| 163 |
+
## Installation
|
| 164 |
+
|
| 165 |
+
plotly.py may be installed using pip
|
| 166 |
+
|
| 167 |
+
```
|
| 168 |
+
pip install plotly
|
| 169 |
+
```
|
| 170 |
+
|
| 171 |
+
or conda.
|
| 172 |
+
|
| 173 |
+
```
|
| 174 |
+
conda install -c conda-forge plotly
|
| 175 |
+
```
|
| 176 |
+
|
| 177 |
+
### Jupyter Widget Support
|
| 178 |
+
|
| 179 |
+
For use as a Jupyter widget, install `jupyter` and `anywidget`
|
| 180 |
+
packages using `pip`:
|
| 181 |
+
|
| 182 |
+
```
|
| 183 |
+
pip install jupyter anywidget
|
| 184 |
+
```
|
| 185 |
+
|
| 186 |
+
or `conda`:
|
| 187 |
+
|
| 188 |
+
```
|
| 189 |
+
conda install jupyter anywidget
|
| 190 |
+
```
|
| 191 |
+
|
| 192 |
+
### Static Image Export
|
| 193 |
+
|
| 194 |
+
plotly.py supports [static image export](https://plotly.com/python/static-image-export/),
|
| 195 |
+
using either the [`kaleido`](https://github.com/plotly/Kaleido)
|
| 196 |
+
package (recommended, supported as of `plotly` version 4.9) or the [orca](https://github.com/plotly/orca)
|
| 197 |
+
command line utility (legacy as of `plotly` version 4.9).
|
| 198 |
+
|
| 199 |
+
#### Kaleido
|
| 200 |
+
|
| 201 |
+
The [`kaleido`](https://github.com/plotly/Kaleido) package has no dependencies and can be installed
|
| 202 |
+
using pip
|
| 203 |
+
|
| 204 |
+
```
|
| 205 |
+
pip install -U kaleido
|
| 206 |
+
```
|
| 207 |
+
|
| 208 |
+
or conda
|
| 209 |
+
|
| 210 |
+
```
|
| 211 |
+
conda install -c conda-forge python-kaleido
|
| 212 |
+
```
|
| 213 |
+
|
| 214 |
+
### Extended Geo Support
|
| 215 |
+
|
| 216 |
+
Some plotly.py features rely on fairly large geographic shape files. The county
|
| 217 |
+
choropleth figure factory is one such example. These shape files are distributed as a
|
| 218 |
+
separate `plotly-geo` package. This package can be installed using pip...
|
| 219 |
+
|
| 220 |
+
```
|
| 221 |
+
pip install plotly-geo==1.0.0
|
| 222 |
+
```
|
| 223 |
+
|
| 224 |
+
or conda
|
| 225 |
+
|
| 226 |
+
```
|
| 227 |
+
conda install -c plotly plotly-geo=1.0.0
|
| 228 |
+
```
|
| 229 |
+
|
| 230 |
+
`plotly-geo` can be found on Github at https://github.com/plotly/plotly-geo.
|
| 231 |
+
|
| 232 |
+
## Copyright and Licenses
|
| 233 |
+
|
| 234 |
+
Code and documentation copyright 2019 Plotly, Inc.
|
| 235 |
+
|
| 236 |
+
Code released under the [MIT license](https://github.com/plotly/plotly.py/blob/main/LICENSE.txt).
|
| 237 |
+
|
| 238 |
+
Docs released under the [Creative Commons license](https://github.com/plotly/documentation/blob/source/LICENSE).
|
| 239 |
+
|
.venv_haddock/lib/python3.12/site-packages/plotly-6.5.2.dist-info/RECORD
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
.venv_haddock/lib/python3.12/site-packages/plotly-6.5.2.dist-info/WHEEL
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Wheel-Version: 1.0
|
| 2 |
+
Generator: setuptools (80.9.0)
|
| 3 |
+
Root-Is-Purelib: true
|
| 4 |
+
Tag: py3-none-any
|
| 5 |
+
|
.venv_haddock/lib/python3.12/site-packages/plotly-6.5.2.dist-info/entry_points.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[console_scripts]
|
| 2 |
+
plotly_get_chrome = plotly.io._kaleido:plotly_get_chrome
|
.venv_haddock/lib/python3.12/site-packages/plotly-6.5.2.dist-info/licenses/._LICENSE.txt
ADDED
|
Binary file (4.1 kB). View file
|
|
|
.venv_haddock/lib/python3.12/site-packages/plotly-6.5.2.dist-info/licenses/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
MIT License
|
| 2 |
+
|
| 3 |
+
Copyright (c) 2016-2024 Plotly Technologies Inc.
|
| 4 |
+
|
| 5 |
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
| 6 |
+
of this software and associated documentation files (the "Software"), to deal
|
| 7 |
+
in the Software without restriction, including without limitation the rights
|
| 8 |
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
| 9 |
+
copies of the Software, and to permit persons to whom the Software is
|
| 10 |
+
furnished to do so, subject to the following conditions:
|
| 11 |
+
|
| 12 |
+
The above copyright notice and this permission notice shall be included in
|
| 13 |
+
all copies or substantial portions of the Software.
|
| 14 |
+
|
| 15 |
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
| 16 |
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
| 17 |
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
| 18 |
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
| 19 |
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
| 20 |
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
| 21 |
+
THE SOFTWARE.
|
.venv_haddock/lib/python3.12/site-packages/plotly-6.5.2.dist-info/top_level.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
_plotly_utils
|
| 2 |
+
plotly
|
.venv_haddock/lib/python3.12/site-packages/plotly/io/.__utils.py
ADDED
|
Binary file (4.1 kB). View file
|
|
|
.venv_haddock/lib/python3.12/site-packages/plotly/io/._base_renderers.py
ADDED
|
Binary file (4.1 kB). View file
|
|
|
.venv_haddock/lib/python3.12/site-packages/plotly/io/._json.py
ADDED
|
Binary file (4.1 kB). View file
|
|
|
.venv_haddock/lib/python3.12/site-packages/plotly/io/._kaleido.py
ADDED
|
Binary file (4.1 kB). View file
|
|
|
.venv_haddock/lib/python3.12/site-packages/plotly/io/._orca.py
ADDED
|
Binary file (4.1 kB). View file
|
|
|
.venv_haddock/lib/python3.12/site-packages/plotly/io/__init__.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# ruff: noqa: F401
|
| 2 |
+
|
| 3 |
+
from _plotly_utils.importers import relative_import
|
| 4 |
+
from typing import TYPE_CHECKING
|
| 5 |
+
|
| 6 |
+
if TYPE_CHECKING:
|
| 7 |
+
from ._kaleido import (
|
| 8 |
+
to_image,
|
| 9 |
+
write_image,
|
| 10 |
+
write_images,
|
| 11 |
+
full_figure_for_development,
|
| 12 |
+
)
|
| 13 |
+
from . import orca, kaleido
|
| 14 |
+
from . import json
|
| 15 |
+
from ._json import to_json, from_json, read_json, write_json
|
| 16 |
+
from ._templates import templates, to_templated
|
| 17 |
+
from ._html import to_html, write_html
|
| 18 |
+
from ._renderers import renderers, show
|
| 19 |
+
from . import base_renderers
|
| 20 |
+
from ._kaleido import defaults, get_chrome
|
| 21 |
+
|
| 22 |
+
__all__ = [
|
| 23 |
+
"to_image",
|
| 24 |
+
"write_image",
|
| 25 |
+
"write_images",
|
| 26 |
+
"orca",
|
| 27 |
+
"json",
|
| 28 |
+
"to_json",
|
| 29 |
+
"from_json",
|
| 30 |
+
"read_json",
|
| 31 |
+
"write_json",
|
| 32 |
+
"templates",
|
| 33 |
+
"to_templated",
|
| 34 |
+
"to_html",
|
| 35 |
+
"write_html",
|
| 36 |
+
"renderers",
|
| 37 |
+
"show",
|
| 38 |
+
"base_renderers",
|
| 39 |
+
"full_figure_for_development",
|
| 40 |
+
"defaults",
|
| 41 |
+
"get_chrome",
|
| 42 |
+
]
|
| 43 |
+
else:
|
| 44 |
+
__all__, __getattr__, __dir__ = relative_import(
|
| 45 |
+
__name__,
|
| 46 |
+
[".orca", ".kaleido", ".json", ".base_renderers"],
|
| 47 |
+
[
|
| 48 |
+
"._kaleido.to_image",
|
| 49 |
+
"._kaleido.write_image",
|
| 50 |
+
"._kaleido.write_images",
|
| 51 |
+
"._kaleido.full_figure_for_development",
|
| 52 |
+
"._json.to_json",
|
| 53 |
+
"._json.from_json",
|
| 54 |
+
"._json.read_json",
|
| 55 |
+
"._json.write_json",
|
| 56 |
+
"._templates.templates",
|
| 57 |
+
"._templates.to_templated",
|
| 58 |
+
"._html.to_html",
|
| 59 |
+
"._html.write_html",
|
| 60 |
+
"._renderers.renderers",
|
| 61 |
+
"._renderers.show",
|
| 62 |
+
"._kaleido.defaults",
|
| 63 |
+
"._kaleido.get_chrome",
|
| 64 |
+
],
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
+
# Set default template (for < 3.7 this is done in ploty/__init__.py)
|
| 68 |
+
from plotly.io import templates
|
| 69 |
+
|
| 70 |
+
templates._default = "plotly"
|
.venv_haddock/lib/python3.12/site-packages/plotly/io/_base_renderers.py
ADDED
|
@@ -0,0 +1,846 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import base64
|
| 2 |
+
import json
|
| 3 |
+
import webbrowser
|
| 4 |
+
import inspect
|
| 5 |
+
import os
|
| 6 |
+
from os.path import isdir
|
| 7 |
+
|
| 8 |
+
from plotly import optional_imports
|
| 9 |
+
from plotly.io import to_json, to_image, write_image, write_html
|
| 10 |
+
from plotly.io._utils import plotly_cdn_url
|
| 11 |
+
from plotly.offline.offline import _get_jconfig, get_plotlyjs
|
| 12 |
+
from plotly.tools import return_figure_from_figure_or_data
|
| 13 |
+
|
| 14 |
+
ipython_display = optional_imports.get_module("IPython.display")
|
| 15 |
+
IPython = optional_imports.get_module("IPython")
|
| 16 |
+
|
| 17 |
+
try:
|
| 18 |
+
from http.server import BaseHTTPRequestHandler, HTTPServer
|
| 19 |
+
except ImportError:
|
| 20 |
+
# Python 2.7
|
| 21 |
+
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
class BaseRenderer(object):
|
| 25 |
+
"""
|
| 26 |
+
Base class for all renderers
|
| 27 |
+
"""
|
| 28 |
+
|
| 29 |
+
def activate(self):
|
| 30 |
+
pass
|
| 31 |
+
|
| 32 |
+
def __repr__(self):
|
| 33 |
+
try:
|
| 34 |
+
init_sig = inspect.signature(self.__init__)
|
| 35 |
+
init_args = list(init_sig.parameters.keys())
|
| 36 |
+
except AttributeError:
|
| 37 |
+
# Python 2.7
|
| 38 |
+
argspec = inspect.getargspec(self.__init__)
|
| 39 |
+
init_args = [a for a in argspec.args if a != "self"]
|
| 40 |
+
|
| 41 |
+
return "{cls}({attrs})\n{doc}".format(
|
| 42 |
+
cls=self.__class__.__name__,
|
| 43 |
+
attrs=", ".join("{}={!r}".format(k, self.__dict__[k]) for k in init_args),
|
| 44 |
+
doc=self.__doc__,
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
def __hash__(self):
|
| 48 |
+
# Constructor args fully define uniqueness
|
| 49 |
+
return hash(repr(self))
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
class MimetypeRenderer(BaseRenderer):
|
| 53 |
+
"""
|
| 54 |
+
Base class for all mime type renderers
|
| 55 |
+
"""
|
| 56 |
+
|
| 57 |
+
def to_mimebundle(self, fig_dict):
|
| 58 |
+
raise NotImplementedError()
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
class JsonRenderer(MimetypeRenderer):
|
| 62 |
+
"""
|
| 63 |
+
Renderer to display figures as JSON hierarchies. This renderer is
|
| 64 |
+
compatible with JupyterLab and VSCode.
|
| 65 |
+
|
| 66 |
+
mime type: 'application/json'
|
| 67 |
+
"""
|
| 68 |
+
|
| 69 |
+
def to_mimebundle(self, fig_dict):
|
| 70 |
+
value = json.loads(to_json(fig_dict, validate=False, remove_uids=False))
|
| 71 |
+
return {"application/json": value}
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
# Plotly mimetype
|
| 75 |
+
class PlotlyRenderer(MimetypeRenderer):
|
| 76 |
+
"""
|
| 77 |
+
Renderer to display figures using the plotly mime type. This renderer is
|
| 78 |
+
compatible with VSCode and nteract.
|
| 79 |
+
|
| 80 |
+
mime type: 'application/vnd.plotly.v1+json'
|
| 81 |
+
"""
|
| 82 |
+
|
| 83 |
+
def __init__(self, config=None):
|
| 84 |
+
self.config = dict(config) if config else {}
|
| 85 |
+
|
| 86 |
+
def to_mimebundle(self, fig_dict):
|
| 87 |
+
config = _get_jconfig(self.config)
|
| 88 |
+
if config:
|
| 89 |
+
fig_dict["config"] = config
|
| 90 |
+
|
| 91 |
+
json_compatible_fig_dict = json.loads(
|
| 92 |
+
to_json(fig_dict, validate=False, remove_uids=False)
|
| 93 |
+
)
|
| 94 |
+
|
| 95 |
+
return {"application/vnd.plotly.v1+json": json_compatible_fig_dict}
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
# Static Image
|
| 99 |
+
class ImageRenderer(MimetypeRenderer):
|
| 100 |
+
"""
|
| 101 |
+
Base class for all static image renderers
|
| 102 |
+
"""
|
| 103 |
+
|
| 104 |
+
def __init__(
|
| 105 |
+
self,
|
| 106 |
+
mime_type,
|
| 107 |
+
b64_encode=False,
|
| 108 |
+
format=None,
|
| 109 |
+
width=None,
|
| 110 |
+
height=None,
|
| 111 |
+
scale=None,
|
| 112 |
+
engine="auto",
|
| 113 |
+
):
|
| 114 |
+
self.mime_type = mime_type
|
| 115 |
+
self.b64_encode = b64_encode
|
| 116 |
+
self.format = format
|
| 117 |
+
self.width = width
|
| 118 |
+
self.height = height
|
| 119 |
+
self.scale = scale
|
| 120 |
+
self.engine = engine
|
| 121 |
+
|
| 122 |
+
def to_mimebundle(self, fig_dict):
|
| 123 |
+
image_bytes = to_image(
|
| 124 |
+
fig_dict,
|
| 125 |
+
format=self.format,
|
| 126 |
+
width=self.width,
|
| 127 |
+
height=self.height,
|
| 128 |
+
scale=self.scale,
|
| 129 |
+
validate=False,
|
| 130 |
+
engine=self.engine,
|
| 131 |
+
)
|
| 132 |
+
|
| 133 |
+
if self.b64_encode:
|
| 134 |
+
image_str = base64.b64encode(image_bytes).decode("utf8")
|
| 135 |
+
else:
|
| 136 |
+
image_str = image_bytes.decode("utf8")
|
| 137 |
+
|
| 138 |
+
return {self.mime_type: image_str}
|
| 139 |
+
|
| 140 |
+
|
| 141 |
+
class PngRenderer(ImageRenderer):
|
| 142 |
+
"""
|
| 143 |
+
Renderer to display figures as static PNG images. This renderer requires
|
| 144 |
+
either the kaleido package or the orca command-line utility and is broadly
|
| 145 |
+
compatible across IPython environments (classic Jupyter Notebook, JupyterLab,
|
| 146 |
+
QtConsole, VSCode, PyCharm, etc) and nbconvert targets (HTML, PDF, etc.).
|
| 147 |
+
|
| 148 |
+
mime type: 'image/png'
|
| 149 |
+
"""
|
| 150 |
+
|
| 151 |
+
def __init__(self, width=None, height=None, scale=None, engine=None):
|
| 152 |
+
super(PngRenderer, self).__init__(
|
| 153 |
+
mime_type="image/png",
|
| 154 |
+
b64_encode=True,
|
| 155 |
+
format="png",
|
| 156 |
+
width=width,
|
| 157 |
+
height=height,
|
| 158 |
+
scale=scale,
|
| 159 |
+
engine=engine,
|
| 160 |
+
)
|
| 161 |
+
|
| 162 |
+
|
| 163 |
+
class SvgRenderer(ImageRenderer):
|
| 164 |
+
"""
|
| 165 |
+
Renderer to display figures as static SVG images. This renderer requires
|
| 166 |
+
either the kaleido package or the orca command-line utility and is broadly
|
| 167 |
+
compatible across IPython environments (classic Jupyter Notebook, JupyterLab,
|
| 168 |
+
QtConsole, VSCode, PyCharm, etc) and nbconvert targets (HTML, PDF, etc.).
|
| 169 |
+
|
| 170 |
+
mime type: 'image/svg+xml'
|
| 171 |
+
"""
|
| 172 |
+
|
| 173 |
+
def __init__(self, width=None, height=None, scale=None, engine=None):
|
| 174 |
+
super(SvgRenderer, self).__init__(
|
| 175 |
+
mime_type="image/svg+xml",
|
| 176 |
+
b64_encode=False,
|
| 177 |
+
format="svg",
|
| 178 |
+
width=width,
|
| 179 |
+
height=height,
|
| 180 |
+
scale=scale,
|
| 181 |
+
engine=engine,
|
| 182 |
+
)
|
| 183 |
+
|
| 184 |
+
|
| 185 |
+
class JpegRenderer(ImageRenderer):
|
| 186 |
+
"""
|
| 187 |
+
Renderer to display figures as static JPEG images. This renderer requires
|
| 188 |
+
either the kaleido package or the orca command-line utility and is broadly
|
| 189 |
+
compatible across IPython environments (classic Jupyter Notebook, JupyterLab,
|
| 190 |
+
QtConsole, VSCode, PyCharm, etc) and nbconvert targets (HTML, PDF, etc.).
|
| 191 |
+
|
| 192 |
+
mime type: 'image/jpeg'
|
| 193 |
+
"""
|
| 194 |
+
|
| 195 |
+
def __init__(self, width=None, height=None, scale=None, engine=None):
|
| 196 |
+
super(JpegRenderer, self).__init__(
|
| 197 |
+
mime_type="image/jpeg",
|
| 198 |
+
b64_encode=True,
|
| 199 |
+
format="jpg",
|
| 200 |
+
width=width,
|
| 201 |
+
height=height,
|
| 202 |
+
scale=scale,
|
| 203 |
+
engine=engine,
|
| 204 |
+
)
|
| 205 |
+
|
| 206 |
+
|
| 207 |
+
class PdfRenderer(ImageRenderer):
|
| 208 |
+
"""
|
| 209 |
+
Renderer to display figures as static PDF images. This renderer requires
|
| 210 |
+
either the kaleido package or the orca command-line utility and is compatible
|
| 211 |
+
with JupyterLab and the LaTeX-based nbconvert export to PDF.
|
| 212 |
+
|
| 213 |
+
mime type: 'application/pdf'
|
| 214 |
+
"""
|
| 215 |
+
|
| 216 |
+
def __init__(self, width=None, height=None, scale=None, engine=None):
|
| 217 |
+
super(PdfRenderer, self).__init__(
|
| 218 |
+
mime_type="application/pdf",
|
| 219 |
+
b64_encode=True,
|
| 220 |
+
format="pdf",
|
| 221 |
+
width=width,
|
| 222 |
+
height=height,
|
| 223 |
+
scale=scale,
|
| 224 |
+
engine=engine,
|
| 225 |
+
)
|
| 226 |
+
|
| 227 |
+
|
| 228 |
+
# HTML
|
| 229 |
+
# Build script to set global PlotlyConfig object. This must execute before
|
| 230 |
+
# plotly.js is loaded.
|
| 231 |
+
_window_plotly_config = """\
|
| 232 |
+
window.PlotlyConfig = {MathJaxConfig: 'local'};"""
|
| 233 |
+
|
| 234 |
+
_mathjax_config = """\
|
| 235 |
+
if (window.MathJax && window.MathJax.Hub && window.MathJax.Hub.Config) {window.MathJax.Hub.Config({SVG: {font: "STIX-Web"}});}"""
|
| 236 |
+
|
| 237 |
+
|
| 238 |
+
class HtmlRenderer(MimetypeRenderer):
|
| 239 |
+
"""
|
| 240 |
+
Base class for all HTML mime type renderers
|
| 241 |
+
|
| 242 |
+
mime type: 'text/html'
|
| 243 |
+
"""
|
| 244 |
+
|
| 245 |
+
def __init__(
|
| 246 |
+
self,
|
| 247 |
+
connected=False,
|
| 248 |
+
full_html=False,
|
| 249 |
+
global_init=False,
|
| 250 |
+
config=None,
|
| 251 |
+
auto_play=False,
|
| 252 |
+
post_script=None,
|
| 253 |
+
animation_opts=None,
|
| 254 |
+
include_plotlyjs=True,
|
| 255 |
+
):
|
| 256 |
+
self.config = dict(config) if config else {}
|
| 257 |
+
self.auto_play = auto_play
|
| 258 |
+
self.connected = connected
|
| 259 |
+
self.global_init = global_init
|
| 260 |
+
self.full_html = full_html
|
| 261 |
+
self.animation_opts = animation_opts
|
| 262 |
+
self.post_script = post_script
|
| 263 |
+
self.include_plotlyjs = "cdn" if self.connected else include_plotlyjs
|
| 264 |
+
|
| 265 |
+
def activate(self):
|
| 266 |
+
if self.global_init:
|
| 267 |
+
if not ipython_display:
|
| 268 |
+
raise ValueError(
|
| 269 |
+
"The {cls} class requires ipython but it is not installed".format(
|
| 270 |
+
cls=self.__class__.__name__
|
| 271 |
+
)
|
| 272 |
+
)
|
| 273 |
+
|
| 274 |
+
if self.connected:
|
| 275 |
+
script = """\
|
| 276 |
+
<script type="text/javascript">
|
| 277 |
+
{win_config}
|
| 278 |
+
{mathjax_config}
|
| 279 |
+
</script>
|
| 280 |
+
<script type="module">import \"{plotly_cdn}\"</script>
|
| 281 |
+
""".format(
|
| 282 |
+
win_config=_window_plotly_config,
|
| 283 |
+
mathjax_config=_mathjax_config,
|
| 284 |
+
plotly_cdn=plotly_cdn_url().rstrip(".js"),
|
| 285 |
+
)
|
| 286 |
+
|
| 287 |
+
else:
|
| 288 |
+
# If not connected then we embed a copy of the plotly.js
|
| 289 |
+
# library in the notebook
|
| 290 |
+
script = """\
|
| 291 |
+
<script type="text/javascript">
|
| 292 |
+
{win_config}
|
| 293 |
+
{mathjax_config}
|
| 294 |
+
</script>
|
| 295 |
+
<script>{script}</script>
|
| 296 |
+
""".format(
|
| 297 |
+
script=get_plotlyjs(),
|
| 298 |
+
win_config=_window_plotly_config,
|
| 299 |
+
mathjax_config=_mathjax_config,
|
| 300 |
+
)
|
| 301 |
+
|
| 302 |
+
ipython_display.display_html(script, raw=True)
|
| 303 |
+
|
| 304 |
+
def to_mimebundle(self, fig_dict):
|
| 305 |
+
from plotly.io import to_html
|
| 306 |
+
|
| 307 |
+
include_mathjax = "cdn"
|
| 308 |
+
|
| 309 |
+
# build post script
|
| 310 |
+
post_script = [
|
| 311 |
+
"""
|
| 312 |
+
var gd = document.getElementById('{plot_id}');
|
| 313 |
+
var x = new MutationObserver(function (mutations, observer) {{
|
| 314 |
+
var display = window.getComputedStyle(gd).display;
|
| 315 |
+
if (!display || display === 'none') {{
|
| 316 |
+
console.log([gd, 'removed!']);
|
| 317 |
+
Plotly.purge(gd);
|
| 318 |
+
observer.disconnect();
|
| 319 |
+
}}
|
| 320 |
+
}});
|
| 321 |
+
|
| 322 |
+
// Listen for the removal of the full notebook cells
|
| 323 |
+
var notebookContainer = gd.closest('#notebook-container');
|
| 324 |
+
if (notebookContainer) {{
|
| 325 |
+
x.observe(notebookContainer, {childList: true});
|
| 326 |
+
}}
|
| 327 |
+
|
| 328 |
+
// Listen for the clearing of the current output cell
|
| 329 |
+
var outputEl = gd.closest('.output');
|
| 330 |
+
if (outputEl) {{
|
| 331 |
+
x.observe(outputEl, {childList: true});
|
| 332 |
+
}}
|
| 333 |
+
"""
|
| 334 |
+
]
|
| 335 |
+
|
| 336 |
+
# Add user defined post script
|
| 337 |
+
if self.post_script:
|
| 338 |
+
if not isinstance(self.post_script, (list, tuple)):
|
| 339 |
+
post_script.append(self.post_script)
|
| 340 |
+
else:
|
| 341 |
+
post_script.extend(self.post_script)
|
| 342 |
+
|
| 343 |
+
html = to_html(
|
| 344 |
+
fig_dict,
|
| 345 |
+
config=self.config,
|
| 346 |
+
auto_play=self.auto_play,
|
| 347 |
+
include_plotlyjs=self.include_plotlyjs,
|
| 348 |
+
include_mathjax=include_mathjax,
|
| 349 |
+
post_script=post_script,
|
| 350 |
+
full_html=self.full_html,
|
| 351 |
+
animation_opts=self.animation_opts,
|
| 352 |
+
default_width="100%",
|
| 353 |
+
default_height=525,
|
| 354 |
+
validate=False,
|
| 355 |
+
)
|
| 356 |
+
|
| 357 |
+
return {"text/html": html}
|
| 358 |
+
|
| 359 |
+
|
| 360 |
+
class NotebookRenderer(HtmlRenderer):
|
| 361 |
+
"""
|
| 362 |
+
Renderer to display interactive figures in the classic Jupyter Notebook.
|
| 363 |
+
This renderer is also useful for notebooks that will be converted to
|
| 364 |
+
HTML using nbconvert/nbviewer as it will produce standalone HTML files
|
| 365 |
+
that include interactive figures.
|
| 366 |
+
|
| 367 |
+
This renderer automatically performs global notebook initialization when
|
| 368 |
+
activated.
|
| 369 |
+
|
| 370 |
+
mime type: 'text/html'
|
| 371 |
+
"""
|
| 372 |
+
|
| 373 |
+
def __init__(
|
| 374 |
+
self,
|
| 375 |
+
connected=False,
|
| 376 |
+
config=None,
|
| 377 |
+
auto_play=False,
|
| 378 |
+
post_script=None,
|
| 379 |
+
animation_opts=None,
|
| 380 |
+
include_plotlyjs=False,
|
| 381 |
+
):
|
| 382 |
+
super(NotebookRenderer, self).__init__(
|
| 383 |
+
connected=connected,
|
| 384 |
+
full_html=False,
|
| 385 |
+
global_init=True,
|
| 386 |
+
config=config,
|
| 387 |
+
auto_play=auto_play,
|
| 388 |
+
post_script=post_script,
|
| 389 |
+
animation_opts=animation_opts,
|
| 390 |
+
include_plotlyjs=include_plotlyjs,
|
| 391 |
+
)
|
| 392 |
+
|
| 393 |
+
|
| 394 |
+
class KaggleRenderer(HtmlRenderer):
|
| 395 |
+
"""
|
| 396 |
+
Renderer to display interactive figures in Kaggle Notebooks.
|
| 397 |
+
|
| 398 |
+
Same as NotebookRenderer but with connected=True so that the plotly.js
|
| 399 |
+
bundle is loaded from a CDN rather than being embedded in the notebook.
|
| 400 |
+
|
| 401 |
+
This renderer is enabled by default when running in a Kaggle notebook.
|
| 402 |
+
|
| 403 |
+
mime type: 'text/html'
|
| 404 |
+
"""
|
| 405 |
+
|
| 406 |
+
def __init__(
|
| 407 |
+
self, config=None, auto_play=False, post_script=None, animation_opts=None
|
| 408 |
+
):
|
| 409 |
+
super(KaggleRenderer, self).__init__(
|
| 410 |
+
connected=True,
|
| 411 |
+
full_html=False,
|
| 412 |
+
global_init=True,
|
| 413 |
+
config=config,
|
| 414 |
+
auto_play=auto_play,
|
| 415 |
+
post_script=post_script,
|
| 416 |
+
animation_opts=animation_opts,
|
| 417 |
+
include_plotlyjs=False,
|
| 418 |
+
)
|
| 419 |
+
|
| 420 |
+
|
| 421 |
+
class AzureRenderer(HtmlRenderer):
|
| 422 |
+
"""
|
| 423 |
+
Renderer to display interactive figures in Azure Notebooks.
|
| 424 |
+
|
| 425 |
+
Same as NotebookRenderer but with connected=True so that the plotly.js
|
| 426 |
+
bundle is loaded from a CDN rather than being embedded in the notebook.
|
| 427 |
+
|
| 428 |
+
This renderer is enabled by default when running in an Azure notebook.
|
| 429 |
+
|
| 430 |
+
mime type: 'text/html'
|
| 431 |
+
"""
|
| 432 |
+
|
| 433 |
+
def __init__(
|
| 434 |
+
self, config=None, auto_play=False, post_script=None, animation_opts=None
|
| 435 |
+
):
|
| 436 |
+
super(AzureRenderer, self).__init__(
|
| 437 |
+
connected=True,
|
| 438 |
+
full_html=False,
|
| 439 |
+
global_init=True,
|
| 440 |
+
config=config,
|
| 441 |
+
auto_play=auto_play,
|
| 442 |
+
post_script=post_script,
|
| 443 |
+
animation_opts=animation_opts,
|
| 444 |
+
include_plotlyjs=False,
|
| 445 |
+
)
|
| 446 |
+
|
| 447 |
+
|
| 448 |
+
class ColabRenderer(HtmlRenderer):
|
| 449 |
+
"""
|
| 450 |
+
Renderer to display interactive figures in Google Colab Notebooks.
|
| 451 |
+
|
| 452 |
+
This renderer is enabled by default when running in a Colab notebook.
|
| 453 |
+
|
| 454 |
+
mime type: 'text/html'
|
| 455 |
+
"""
|
| 456 |
+
|
| 457 |
+
def __init__(
|
| 458 |
+
self, config=None, auto_play=False, post_script=None, animation_opts=None
|
| 459 |
+
):
|
| 460 |
+
super(ColabRenderer, self).__init__(
|
| 461 |
+
connected=True,
|
| 462 |
+
full_html=True,
|
| 463 |
+
global_init=False,
|
| 464 |
+
config=config,
|
| 465 |
+
auto_play=auto_play,
|
| 466 |
+
post_script=post_script,
|
| 467 |
+
animation_opts=animation_opts,
|
| 468 |
+
)
|
| 469 |
+
|
| 470 |
+
|
| 471 |
+
class IFrameRenderer(MimetypeRenderer):
|
| 472 |
+
"""
|
| 473 |
+
Renderer to display interactive figures using an IFrame. HTML
|
| 474 |
+
representations of Figures are saved to an `iframe_figures/` directory and
|
| 475 |
+
iframe HTML elements that reference these files are inserted into the
|
| 476 |
+
notebook.
|
| 477 |
+
|
| 478 |
+
With this approach, neither plotly.js nor the figure data are embedded in
|
| 479 |
+
the notebook, so this is a good choice for notebooks that contain so many
|
| 480 |
+
large figures that basic operations (like saving and opening) become
|
| 481 |
+
very slow.
|
| 482 |
+
|
| 483 |
+
Notebooks using this renderer will display properly when exported to HTML
|
| 484 |
+
as long as the `iframe_figures/` directory is placed in the same directory
|
| 485 |
+
as the exported html file.
|
| 486 |
+
|
| 487 |
+
Note that the HTML files in `iframe_figures/` are numbered according to
|
| 488 |
+
the IPython cell execution count and so they will start being overwritten
|
| 489 |
+
each time the kernel is restarted. This directory may be deleted whenever
|
| 490 |
+
the kernel is restarted and it will be automatically recreated.
|
| 491 |
+
|
| 492 |
+
mime type: 'text/html'
|
| 493 |
+
"""
|
| 494 |
+
|
| 495 |
+
def __init__(
|
| 496 |
+
self,
|
| 497 |
+
config=None,
|
| 498 |
+
auto_play=False,
|
| 499 |
+
post_script=None,
|
| 500 |
+
animation_opts=None,
|
| 501 |
+
include_plotlyjs=True,
|
| 502 |
+
html_directory="iframe_figures",
|
| 503 |
+
):
|
| 504 |
+
self.config = config
|
| 505 |
+
self.auto_play = auto_play
|
| 506 |
+
self.post_script = post_script
|
| 507 |
+
self.animation_opts = animation_opts
|
| 508 |
+
self.include_plotlyjs = include_plotlyjs
|
| 509 |
+
self.html_directory = html_directory
|
| 510 |
+
|
| 511 |
+
def to_mimebundle(self, fig_dict):
|
| 512 |
+
from plotly.io import write_html
|
| 513 |
+
|
| 514 |
+
# Make iframe size slightly larger than figure size to avoid
|
| 515 |
+
# having iframe have its own scroll bar.
|
| 516 |
+
iframe_buffer = 20
|
| 517 |
+
layout = fig_dict.get("layout", {})
|
| 518 |
+
|
| 519 |
+
if layout.get("width", False):
|
| 520 |
+
iframe_width = str(layout["width"] + iframe_buffer) + "px"
|
| 521 |
+
else:
|
| 522 |
+
iframe_width = "100%"
|
| 523 |
+
|
| 524 |
+
if layout.get("height", False):
|
| 525 |
+
iframe_height = layout["height"] + iframe_buffer
|
| 526 |
+
else:
|
| 527 |
+
iframe_height = str(525 + iframe_buffer) + "px"
|
| 528 |
+
|
| 529 |
+
# Build filename using ipython cell number
|
| 530 |
+
filename = self.build_filename()
|
| 531 |
+
|
| 532 |
+
# Make directory for
|
| 533 |
+
try:
|
| 534 |
+
os.makedirs(self.html_directory)
|
| 535 |
+
except OSError:
|
| 536 |
+
if not isdir(self.html_directory):
|
| 537 |
+
raise
|
| 538 |
+
|
| 539 |
+
write_html(
|
| 540 |
+
fig_dict,
|
| 541 |
+
filename,
|
| 542 |
+
config=self.config,
|
| 543 |
+
auto_play=self.auto_play,
|
| 544 |
+
include_plotlyjs=self.include_plotlyjs,
|
| 545 |
+
include_mathjax="cdn",
|
| 546 |
+
auto_open=False,
|
| 547 |
+
post_script=self.post_script,
|
| 548 |
+
animation_opts=self.animation_opts,
|
| 549 |
+
default_width="100%",
|
| 550 |
+
default_height=525,
|
| 551 |
+
validate=False,
|
| 552 |
+
)
|
| 553 |
+
|
| 554 |
+
# Build IFrame
|
| 555 |
+
iframe_html = """\
|
| 556 |
+
<iframe
|
| 557 |
+
scrolling="no"
|
| 558 |
+
width="{width}"
|
| 559 |
+
height="{height}"
|
| 560 |
+
src="{src}"
|
| 561 |
+
frameborder="0"
|
| 562 |
+
allowfullscreen
|
| 563 |
+
></iframe>
|
| 564 |
+
""".format(width=iframe_width, height=iframe_height, src=self.build_url(filename))
|
| 565 |
+
|
| 566 |
+
return {"text/html": iframe_html}
|
| 567 |
+
|
| 568 |
+
def build_filename(self):
|
| 569 |
+
ip = IPython.get_ipython() if IPython else None
|
| 570 |
+
try:
|
| 571 |
+
cell_number = list(ip.history_manager.get_tail(1))[0][1] + 1 if ip else 0
|
| 572 |
+
except Exception:
|
| 573 |
+
cell_number = 0
|
| 574 |
+
return "{dirname}/figure_{cell_number}.html".format(
|
| 575 |
+
dirname=self.html_directory, cell_number=cell_number
|
| 576 |
+
)
|
| 577 |
+
|
| 578 |
+
def build_url(self, filename):
|
| 579 |
+
return filename
|
| 580 |
+
|
| 581 |
+
|
| 582 |
+
class CoCalcRenderer(IFrameRenderer):
|
| 583 |
+
_render_count = 0
|
| 584 |
+
|
| 585 |
+
def build_filename(self):
|
| 586 |
+
filename = "{dirname}/figure_{render_count}.html".format(
|
| 587 |
+
dirname=self.html_directory, render_count=CoCalcRenderer._render_count
|
| 588 |
+
)
|
| 589 |
+
|
| 590 |
+
CoCalcRenderer._render_count += 1
|
| 591 |
+
return filename
|
| 592 |
+
|
| 593 |
+
def build_url(self, filename):
|
| 594 |
+
return "{filename}?fullscreen=kiosk".format(filename=filename)
|
| 595 |
+
|
| 596 |
+
|
| 597 |
+
class ExternalRenderer(BaseRenderer):
|
| 598 |
+
"""
|
| 599 |
+
Base class for external renderers. ExternalRenderer subclasses
|
| 600 |
+
do not display figures inline in a notebook environment, but render
|
| 601 |
+
figures by some external means (e.g. a separate browser tab).
|
| 602 |
+
|
| 603 |
+
Unlike MimetypeRenderer subclasses, ExternalRenderer subclasses are not
|
| 604 |
+
invoked when a figure is asked to display itself in the notebook.
|
| 605 |
+
Instead, they are invoked when the plotly.io.show function is called
|
| 606 |
+
on a figure.
|
| 607 |
+
"""
|
| 608 |
+
|
| 609 |
+
def render(self, fig):
|
| 610 |
+
raise NotImplementedError()
|
| 611 |
+
|
| 612 |
+
|
| 613 |
+
def open_html_in_browser(html, using=None, new=0, autoraise=True):
|
| 614 |
+
"""
|
| 615 |
+
Display html in a web browser without creating a temp file.
|
| 616 |
+
|
| 617 |
+
Instantiates a trivial http server and uses the webbrowser module to
|
| 618 |
+
open a URL to retrieve html from that server.
|
| 619 |
+
|
| 620 |
+
Parameters
|
| 621 |
+
----------
|
| 622 |
+
html: str
|
| 623 |
+
HTML string to display
|
| 624 |
+
using, new, autoraise:
|
| 625 |
+
See docstrings in webbrowser.get and webbrowser.open
|
| 626 |
+
"""
|
| 627 |
+
if isinstance(html, str):
|
| 628 |
+
html = html.encode("utf8")
|
| 629 |
+
|
| 630 |
+
browser = None
|
| 631 |
+
|
| 632 |
+
if using is None:
|
| 633 |
+
browser = webbrowser.get(None)
|
| 634 |
+
else:
|
| 635 |
+
if not isinstance(using, tuple):
|
| 636 |
+
using = (using,)
|
| 637 |
+
for browser_key in using:
|
| 638 |
+
try:
|
| 639 |
+
browser = webbrowser.get(browser_key)
|
| 640 |
+
if browser is not None:
|
| 641 |
+
break
|
| 642 |
+
except webbrowser.Error:
|
| 643 |
+
pass
|
| 644 |
+
|
| 645 |
+
if browser is None:
|
| 646 |
+
raise ValueError("Can't locate a browser with key in " + str(using))
|
| 647 |
+
|
| 648 |
+
class OneShotRequestHandler(BaseHTTPRequestHandler):
|
| 649 |
+
def do_GET(self):
|
| 650 |
+
self.send_response(200)
|
| 651 |
+
self.send_header("Content-type", "text/html")
|
| 652 |
+
self.end_headers()
|
| 653 |
+
|
| 654 |
+
bufferSize = 1024 * 1024
|
| 655 |
+
for i in range(0, len(html), bufferSize):
|
| 656 |
+
self.wfile.write(html[i : i + bufferSize])
|
| 657 |
+
|
| 658 |
+
def log_message(self, format, *args):
|
| 659 |
+
# Silence stderr logging
|
| 660 |
+
pass
|
| 661 |
+
|
| 662 |
+
server = HTTPServer(("127.0.0.1", 0), OneShotRequestHandler)
|
| 663 |
+
browser.open(
|
| 664 |
+
"http://127.0.0.1:%s" % server.server_port, new=new, autoraise=autoraise
|
| 665 |
+
)
|
| 666 |
+
|
| 667 |
+
server.handle_request()
|
| 668 |
+
|
| 669 |
+
|
| 670 |
+
class BrowserRenderer(ExternalRenderer):
|
| 671 |
+
"""
|
| 672 |
+
Renderer to display interactive figures in an external web browser.
|
| 673 |
+
This renderer will open a new browser window or tab when the
|
| 674 |
+
plotly.io.show function is called on a figure.
|
| 675 |
+
|
| 676 |
+
This renderer has no ipython/jupyter dependencies and is a good choice
|
| 677 |
+
for use in environments that do not support the inline display of
|
| 678 |
+
interactive figures.
|
| 679 |
+
|
| 680 |
+
mime type: 'text/html'
|
| 681 |
+
"""
|
| 682 |
+
|
| 683 |
+
def __init__(
|
| 684 |
+
self,
|
| 685 |
+
config=None,
|
| 686 |
+
auto_play=False,
|
| 687 |
+
using=None,
|
| 688 |
+
new=0,
|
| 689 |
+
autoraise=True,
|
| 690 |
+
post_script=None,
|
| 691 |
+
animation_opts=None,
|
| 692 |
+
):
|
| 693 |
+
self.config = config
|
| 694 |
+
self.auto_play = auto_play
|
| 695 |
+
self.using = using
|
| 696 |
+
self.new = new
|
| 697 |
+
self.autoraise = autoraise
|
| 698 |
+
self.post_script = post_script
|
| 699 |
+
self.animation_opts = animation_opts
|
| 700 |
+
|
| 701 |
+
def render(self, fig_dict):
|
| 702 |
+
from plotly.io import to_html
|
| 703 |
+
|
| 704 |
+
html = to_html(
|
| 705 |
+
fig_dict,
|
| 706 |
+
config=self.config,
|
| 707 |
+
auto_play=self.auto_play,
|
| 708 |
+
include_plotlyjs=True,
|
| 709 |
+
include_mathjax="cdn",
|
| 710 |
+
post_script=self.post_script,
|
| 711 |
+
full_html=True,
|
| 712 |
+
animation_opts=self.animation_opts,
|
| 713 |
+
default_width="100%",
|
| 714 |
+
default_height="100%",
|
| 715 |
+
validate=False,
|
| 716 |
+
)
|
| 717 |
+
open_html_in_browser(html, self.using, self.new, self.autoraise)
|
| 718 |
+
|
| 719 |
+
|
| 720 |
+
class DatabricksRenderer(ExternalRenderer):
|
| 721 |
+
def __init__(
|
| 722 |
+
self,
|
| 723 |
+
config=None,
|
| 724 |
+
auto_play=False,
|
| 725 |
+
post_script=None,
|
| 726 |
+
animation_opts=None,
|
| 727 |
+
include_plotlyjs="cdn",
|
| 728 |
+
):
|
| 729 |
+
self.config = config
|
| 730 |
+
self.auto_play = auto_play
|
| 731 |
+
self.post_script = post_script
|
| 732 |
+
self.animation_opts = animation_opts
|
| 733 |
+
self.include_plotlyjs = include_plotlyjs
|
| 734 |
+
self._displayHTML = None
|
| 735 |
+
|
| 736 |
+
@property
|
| 737 |
+
def displayHTML(self):
|
| 738 |
+
import inspect
|
| 739 |
+
|
| 740 |
+
if self._displayHTML is None:
|
| 741 |
+
for frame in inspect.getouterframes(inspect.currentframe()):
|
| 742 |
+
global_names = set(frame.frame.f_globals)
|
| 743 |
+
# Check for displayHTML plus a few others to reduce chance of a false
|
| 744 |
+
# hit.
|
| 745 |
+
if all(v in global_names for v in ["displayHTML", "display", "spark"]):
|
| 746 |
+
self._displayHTML = frame.frame.f_globals["displayHTML"]
|
| 747 |
+
break
|
| 748 |
+
|
| 749 |
+
if self._displayHTML is None:
|
| 750 |
+
raise EnvironmentError(
|
| 751 |
+
"""
|
| 752 |
+
Unable to detect the Databricks displayHTML function. The 'databricks' renderer is only
|
| 753 |
+
supported when called from within the Databricks notebook environment."""
|
| 754 |
+
)
|
| 755 |
+
|
| 756 |
+
return self._displayHTML
|
| 757 |
+
|
| 758 |
+
def render(self, fig_dict):
|
| 759 |
+
from plotly.io import to_html
|
| 760 |
+
|
| 761 |
+
html = to_html(
|
| 762 |
+
fig_dict,
|
| 763 |
+
config=self.config,
|
| 764 |
+
auto_play=self.auto_play,
|
| 765 |
+
include_plotlyjs=self.include_plotlyjs,
|
| 766 |
+
include_mathjax="cdn",
|
| 767 |
+
post_script=self.post_script,
|
| 768 |
+
full_html=True,
|
| 769 |
+
animation_opts=self.animation_opts,
|
| 770 |
+
default_width="100%",
|
| 771 |
+
default_height="100%",
|
| 772 |
+
validate=False,
|
| 773 |
+
)
|
| 774 |
+
|
| 775 |
+
# displayHTML is a Databricks notebook built-in function
|
| 776 |
+
self.displayHTML(html)
|
| 777 |
+
|
| 778 |
+
|
| 779 |
+
class SphinxGalleryHtmlRenderer(HtmlRenderer):
|
| 780 |
+
def __init__(
|
| 781 |
+
self,
|
| 782 |
+
connected=True,
|
| 783 |
+
config=None,
|
| 784 |
+
auto_play=False,
|
| 785 |
+
post_script=None,
|
| 786 |
+
animation_opts=None,
|
| 787 |
+
):
|
| 788 |
+
super(SphinxGalleryHtmlRenderer, self).__init__(
|
| 789 |
+
connected=connected,
|
| 790 |
+
full_html=False,
|
| 791 |
+
global_init=False,
|
| 792 |
+
config=config,
|
| 793 |
+
auto_play=auto_play,
|
| 794 |
+
post_script=post_script,
|
| 795 |
+
animation_opts=animation_opts,
|
| 796 |
+
)
|
| 797 |
+
|
| 798 |
+
def to_mimebundle(self, fig_dict):
|
| 799 |
+
from plotly.io import to_html
|
| 800 |
+
|
| 801 |
+
if self.connected:
|
| 802 |
+
include_plotlyjs = "cdn"
|
| 803 |
+
include_mathjax = "cdn"
|
| 804 |
+
else:
|
| 805 |
+
include_plotlyjs = True
|
| 806 |
+
include_mathjax = "cdn"
|
| 807 |
+
|
| 808 |
+
html = to_html(
|
| 809 |
+
fig_dict,
|
| 810 |
+
config=self.config,
|
| 811 |
+
auto_play=self.auto_play,
|
| 812 |
+
include_plotlyjs=include_plotlyjs,
|
| 813 |
+
include_mathjax=include_mathjax,
|
| 814 |
+
full_html=self.full_html,
|
| 815 |
+
animation_opts=self.animation_opts,
|
| 816 |
+
default_width="100%",
|
| 817 |
+
default_height=525,
|
| 818 |
+
validate=False,
|
| 819 |
+
)
|
| 820 |
+
|
| 821 |
+
return {"text/html": html}
|
| 822 |
+
|
| 823 |
+
|
| 824 |
+
class SphinxGalleryOrcaRenderer(ExternalRenderer):
|
| 825 |
+
def render(self, fig_dict):
|
| 826 |
+
stack = inspect.stack()
|
| 827 |
+
# Name of script from which plot function was called is retrieved
|
| 828 |
+
try:
|
| 829 |
+
filename = stack[3].filename # let's hope this is robust...
|
| 830 |
+
except Exception: # python 2
|
| 831 |
+
filename = stack[3][1]
|
| 832 |
+
filename_root, _ = os.path.splitext(filename)
|
| 833 |
+
filename_html = filename_root + ".html"
|
| 834 |
+
filename_png = filename_root + ".png"
|
| 835 |
+
figure = return_figure_from_figure_or_data(fig_dict, True)
|
| 836 |
+
_ = write_html(fig_dict, file=filename_html, include_plotlyjs="cdn")
|
| 837 |
+
try:
|
| 838 |
+
write_image(figure, filename_png)
|
| 839 |
+
except (ValueError, ImportError):
|
| 840 |
+
raise ImportError(
|
| 841 |
+
"orca and psutil are required to use the `sphinx-gallery-orca` renderer. "
|
| 842 |
+
"See https://plotly.com/python/static-image-export/ for instructions on "
|
| 843 |
+
"how to install orca. Alternatively, you can use the `sphinx-gallery` "
|
| 844 |
+
"renderer (note that png thumbnails can only be generated with "
|
| 845 |
+
"the `sphinx-gallery-orca` renderer)."
|
| 846 |
+
)
|
.venv_haddock/lib/python3.12/site-packages/plotly/io/_defaults.py
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Default settings for image generation
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
class _Defaults(object):
|
| 5 |
+
"""
|
| 6 |
+
Class to store default settings for image generation.
|
| 7 |
+
"""
|
| 8 |
+
|
| 9 |
+
def __init__(self):
|
| 10 |
+
self.default_format = "png"
|
| 11 |
+
self.default_width = 700
|
| 12 |
+
self.default_height = 500
|
| 13 |
+
self.default_scale = 1
|
| 14 |
+
self.mathjax = None
|
| 15 |
+
self.topojson = None
|
| 16 |
+
self.plotlyjs = None
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
defaults = _Defaults()
|
.venv_haddock/lib/python3.12/site-packages/plotly/io/_html.py
ADDED
|
@@ -0,0 +1,517 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import uuid
|
| 2 |
+
from pathlib import Path
|
| 3 |
+
import webbrowser
|
| 4 |
+
import hashlib
|
| 5 |
+
import base64
|
| 6 |
+
|
| 7 |
+
from _plotly_utils.optional_imports import get_module
|
| 8 |
+
from plotly.io._utils import validate_coerce_fig_to_dict, plotly_cdn_url
|
| 9 |
+
from plotly.offline.offline import _get_jconfig, get_plotlyjs
|
| 10 |
+
|
| 11 |
+
_json = get_module("json")
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def _generate_sri_hash(content):
|
| 15 |
+
"""Generate SHA256 hash for SRI (Subresource Integrity)"""
|
| 16 |
+
if isinstance(content, str):
|
| 17 |
+
content = content.encode("utf-8")
|
| 18 |
+
sha256_hash = hashlib.sha256(content).digest()
|
| 19 |
+
return "sha256-" + base64.b64encode(sha256_hash).decode("utf-8")
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
# Build script to set global PlotlyConfig object. This must execute before
|
| 23 |
+
# plotly.js is loaded.
|
| 24 |
+
_window_plotly_config = """\
|
| 25 |
+
<script type="text/javascript">\
|
| 26 |
+
window.PlotlyConfig = {MathJaxConfig: 'local'};\
|
| 27 |
+
</script>"""
|
| 28 |
+
|
| 29 |
+
_mathjax_config = """\
|
| 30 |
+
<script type="text/javascript">\
|
| 31 |
+
if (window.MathJax && window.MathJax.Hub && window.MathJax.Hub.Config) {window.MathJax.Hub.Config({SVG: {font: "STIX-Web"}});}\
|
| 32 |
+
</script>"""
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
def to_html(
|
| 36 |
+
fig,
|
| 37 |
+
config=None,
|
| 38 |
+
auto_play=True,
|
| 39 |
+
include_plotlyjs=True,
|
| 40 |
+
include_mathjax=False,
|
| 41 |
+
post_script=None,
|
| 42 |
+
full_html=True,
|
| 43 |
+
animation_opts=None,
|
| 44 |
+
default_width="100%",
|
| 45 |
+
default_height="100%",
|
| 46 |
+
validate=True,
|
| 47 |
+
div_id=None,
|
| 48 |
+
):
|
| 49 |
+
"""
|
| 50 |
+
Convert a figure to an HTML string representation.
|
| 51 |
+
|
| 52 |
+
Parameters
|
| 53 |
+
----------
|
| 54 |
+
fig:
|
| 55 |
+
Figure object or dict representing a figure
|
| 56 |
+
config: dict or None (default None)
|
| 57 |
+
Plotly.js figure config options
|
| 58 |
+
auto_play: bool (default=True)
|
| 59 |
+
Whether to automatically start the animation sequence on page load
|
| 60 |
+
if the figure contains frames. Has no effect if the figure does not
|
| 61 |
+
contain frames.
|
| 62 |
+
include_plotlyjs: bool or string (default True)
|
| 63 |
+
Specifies how the plotly.js library is included/loaded in the output
|
| 64 |
+
div string.
|
| 65 |
+
|
| 66 |
+
If True, a script tag containing the plotly.js source code (~3MB)
|
| 67 |
+
is included in the output. HTML files generated with this option are
|
| 68 |
+
fully self-contained and can be used offline.
|
| 69 |
+
|
| 70 |
+
If 'cdn', a script tag that references the plotly.js CDN is included
|
| 71 |
+
in the output. The url used is versioned to match the bundled plotly.js.
|
| 72 |
+
HTML files generated with this option are about 3MB smaller than those
|
| 73 |
+
generated with include_plotlyjs=True, but they require an active
|
| 74 |
+
internet connection in order to load the plotly.js library.
|
| 75 |
+
|
| 76 |
+
If 'directory', a script tag is included that references an external
|
| 77 |
+
plotly.min.js bundle that is assumed to reside in the same
|
| 78 |
+
directory as the HTML file.
|
| 79 |
+
|
| 80 |
+
If a string that ends in '.js', a script tag is included that
|
| 81 |
+
references the specified path. This approach can be used to point
|
| 82 |
+
the resulting HTML file to an alternative CDN or local bundle.
|
| 83 |
+
|
| 84 |
+
If False, no script tag referencing plotly.js is included. This is
|
| 85 |
+
useful when the resulting div string will be placed inside an HTML
|
| 86 |
+
document that already loads plotly.js. This option is not advised
|
| 87 |
+
when full_html=True as it will result in a non-functional html file.
|
| 88 |
+
include_mathjax: bool or string (default False)
|
| 89 |
+
Specifies how the MathJax.js library is included in the output html
|
| 90 |
+
div string. MathJax is required in order to display labels
|
| 91 |
+
with LaTeX typesetting.
|
| 92 |
+
|
| 93 |
+
If False, no script tag referencing MathJax.js will be included in the
|
| 94 |
+
output.
|
| 95 |
+
|
| 96 |
+
If 'cdn', a script tag that references a MathJax CDN location will be
|
| 97 |
+
included in the output. HTML div strings generated with this option
|
| 98 |
+
will be able to display LaTeX typesetting as long as internet access
|
| 99 |
+
is available.
|
| 100 |
+
|
| 101 |
+
If a string that ends in '.js', a script tag is included that
|
| 102 |
+
references the specified path. This approach can be used to point the
|
| 103 |
+
resulting HTML div string to an alternative CDN.
|
| 104 |
+
post_script: str or list or None (default None)
|
| 105 |
+
JavaScript snippet(s) to be included in the resulting div just after
|
| 106 |
+
plot creation. The string(s) may include '{plot_id}' placeholders
|
| 107 |
+
that will then be replaced by the `id` of the div element that the
|
| 108 |
+
plotly.js figure is associated with. One application for this script
|
| 109 |
+
is to install custom plotly.js event handlers.
|
| 110 |
+
full_html: bool (default True)
|
| 111 |
+
If True, produce a string containing a complete HTML document
|
| 112 |
+
starting with an <html> tag. If False, produce a string containing
|
| 113 |
+
a single <div> element.
|
| 114 |
+
animation_opts: dict or None (default None)
|
| 115 |
+
dict of custom animation parameters to be passed to the function
|
| 116 |
+
Plotly.animate in Plotly.js. See
|
| 117 |
+
https://github.com/plotly/plotly.js/blob/master/src/plots/animation_attributes.js
|
| 118 |
+
for available options. Has no effect if the figure does not contain
|
| 119 |
+
frames, or auto_play is False.
|
| 120 |
+
default_width, default_height: number or str (default '100%')
|
| 121 |
+
The default figure width/height to use if the provided figure does not
|
| 122 |
+
specify its own layout.width/layout.height property. May be
|
| 123 |
+
specified in pixels as an integer (e.g. 500), or as a css width style
|
| 124 |
+
string (e.g. '500px', '100%').
|
| 125 |
+
validate: bool (default True)
|
| 126 |
+
True if the figure should be validated before being converted to
|
| 127 |
+
JSON, False otherwise.
|
| 128 |
+
div_id: str (default None)
|
| 129 |
+
If provided, this is the value of the id attribute of the div tag. If None, the
|
| 130 |
+
id attribute is a UUID.
|
| 131 |
+
|
| 132 |
+
Returns
|
| 133 |
+
-------
|
| 134 |
+
str
|
| 135 |
+
Representation of figure as an HTML div string
|
| 136 |
+
"""
|
| 137 |
+
from plotly.io.json import to_json_plotly
|
| 138 |
+
|
| 139 |
+
# ## Validate figure ##
|
| 140 |
+
fig_dict = validate_coerce_fig_to_dict(fig, validate)
|
| 141 |
+
|
| 142 |
+
# ## Generate div id ##
|
| 143 |
+
plotdivid = div_id or str(uuid.uuid4())
|
| 144 |
+
|
| 145 |
+
# ## Serialize figure ##
|
| 146 |
+
jdata = to_json_plotly(fig_dict.get("data", []))
|
| 147 |
+
jlayout = to_json_plotly(fig_dict.get("layout", {}))
|
| 148 |
+
|
| 149 |
+
if fig_dict.get("frames", None):
|
| 150 |
+
jframes = to_json_plotly(fig_dict.get("frames", []))
|
| 151 |
+
else:
|
| 152 |
+
jframes = None
|
| 153 |
+
|
| 154 |
+
# ## Serialize figure config ##
|
| 155 |
+
config = _get_jconfig(config)
|
| 156 |
+
|
| 157 |
+
# Set responsive
|
| 158 |
+
config.setdefault("responsive", True)
|
| 159 |
+
|
| 160 |
+
# Get div width/height
|
| 161 |
+
layout_dict = fig_dict.get("layout", {})
|
| 162 |
+
template_dict = fig_dict.get("layout", {}).get("template", {}).get("layout", {})
|
| 163 |
+
|
| 164 |
+
div_width = layout_dict.get("width", template_dict.get("width", default_width))
|
| 165 |
+
div_height = layout_dict.get("height", template_dict.get("height", default_height))
|
| 166 |
+
|
| 167 |
+
# Add 'px' suffix to numeric widths
|
| 168 |
+
try:
|
| 169 |
+
float(div_width)
|
| 170 |
+
except (ValueError, TypeError):
|
| 171 |
+
pass
|
| 172 |
+
else:
|
| 173 |
+
div_width = str(div_width) + "px"
|
| 174 |
+
|
| 175 |
+
try:
|
| 176 |
+
float(div_height)
|
| 177 |
+
except (ValueError, TypeError):
|
| 178 |
+
pass
|
| 179 |
+
else:
|
| 180 |
+
div_height = str(div_height) + "px"
|
| 181 |
+
|
| 182 |
+
# ## Get platform URL ##
|
| 183 |
+
if config.get("showLink", False) or config.get("showSendToCloud", False):
|
| 184 |
+
# Figure is going to include a Chart Studio link or send-to-cloud button,
|
| 185 |
+
# So we need to configure the PLOTLYENV.BASE_URL property
|
| 186 |
+
base_url_line = """
|
| 187 |
+
window.PLOTLYENV.BASE_URL='{plotly_platform_url}';\
|
| 188 |
+
""".format(plotly_platform_url=config.get("plotlyServerURL", "https://plot.ly"))
|
| 189 |
+
else:
|
| 190 |
+
# Figure is not going to include a Chart Studio link or send-to-cloud button,
|
| 191 |
+
# In this case we don't want https://plot.ly to show up anywhere in the HTML
|
| 192 |
+
# output
|
| 193 |
+
config.pop("plotlyServerURL", None)
|
| 194 |
+
config.pop("linkText", None)
|
| 195 |
+
config.pop("showLink", None)
|
| 196 |
+
base_url_line = ""
|
| 197 |
+
|
| 198 |
+
# ## Build script body ##
|
| 199 |
+
# This is the part that actually calls Plotly.js
|
| 200 |
+
|
| 201 |
+
# build post script snippet(s)
|
| 202 |
+
then_post_script = ""
|
| 203 |
+
if post_script:
|
| 204 |
+
if not isinstance(post_script, (list, tuple)):
|
| 205 |
+
post_script = [post_script]
|
| 206 |
+
for ps in post_script:
|
| 207 |
+
then_post_script += """.then(function(){{
|
| 208 |
+
{post_script}
|
| 209 |
+
}})""".format(post_script=ps.replace("{plot_id}", plotdivid))
|
| 210 |
+
|
| 211 |
+
then_addframes = ""
|
| 212 |
+
then_animate = ""
|
| 213 |
+
if jframes:
|
| 214 |
+
then_addframes = """.then(function(){{
|
| 215 |
+
Plotly.addFrames('{id}', {frames});
|
| 216 |
+
}})""".format(id=plotdivid, frames=jframes)
|
| 217 |
+
|
| 218 |
+
if auto_play:
|
| 219 |
+
if animation_opts:
|
| 220 |
+
animation_opts_arg = ", " + _json.dumps(animation_opts)
|
| 221 |
+
else:
|
| 222 |
+
animation_opts_arg = ""
|
| 223 |
+
then_animate = """.then(function(){{
|
| 224 |
+
Plotly.animate('{id}', null{animation_opts});
|
| 225 |
+
}})""".format(id=plotdivid, animation_opts=animation_opts_arg)
|
| 226 |
+
|
| 227 |
+
# Serialize config dict to JSON
|
| 228 |
+
jconfig = _json.dumps(config)
|
| 229 |
+
|
| 230 |
+
script = """\
|
| 231 |
+
if (document.getElementById("{id}")) {{\
|
| 232 |
+
Plotly.newPlot(\
|
| 233 |
+
"{id}",\
|
| 234 |
+
{data},\
|
| 235 |
+
{layout},\
|
| 236 |
+
{config}\
|
| 237 |
+
){then_addframes}{then_animate}{then_post_script}\
|
| 238 |
+
}}""".format(
|
| 239 |
+
id=plotdivid,
|
| 240 |
+
data=jdata,
|
| 241 |
+
layout=jlayout,
|
| 242 |
+
config=jconfig,
|
| 243 |
+
then_addframes=then_addframes,
|
| 244 |
+
then_animate=then_animate,
|
| 245 |
+
then_post_script=then_post_script,
|
| 246 |
+
)
|
| 247 |
+
|
| 248 |
+
# ## Handle loading/initializing plotly.js ##
|
| 249 |
+
include_plotlyjs_orig = include_plotlyjs
|
| 250 |
+
if isinstance(include_plotlyjs, str):
|
| 251 |
+
include_plotlyjs = include_plotlyjs.lower()
|
| 252 |
+
|
| 253 |
+
# Init and load
|
| 254 |
+
load_plotlyjs = ""
|
| 255 |
+
|
| 256 |
+
if include_plotlyjs == "cdn":
|
| 257 |
+
# Generate SRI hash from the bundled plotly.js content
|
| 258 |
+
plotlyjs_content = get_plotlyjs()
|
| 259 |
+
sri_hash = _generate_sri_hash(plotlyjs_content)
|
| 260 |
+
|
| 261 |
+
load_plotlyjs = """\
|
| 262 |
+
{win_config}
|
| 263 |
+
<script charset="utf-8" src="{cdn_url}" integrity="{integrity}" crossorigin="anonymous"></script>\
|
| 264 |
+
""".format(
|
| 265 |
+
win_config=_window_plotly_config,
|
| 266 |
+
cdn_url=plotly_cdn_url(),
|
| 267 |
+
integrity=sri_hash,
|
| 268 |
+
)
|
| 269 |
+
|
| 270 |
+
elif include_plotlyjs == "directory":
|
| 271 |
+
load_plotlyjs = """\
|
| 272 |
+
{win_config}
|
| 273 |
+
<script charset="utf-8" src="plotly.min.js"></script>\
|
| 274 |
+
""".format(win_config=_window_plotly_config)
|
| 275 |
+
|
| 276 |
+
elif isinstance(include_plotlyjs, str) and include_plotlyjs.endswith(".js"):
|
| 277 |
+
load_plotlyjs = """\
|
| 278 |
+
{win_config}
|
| 279 |
+
<script charset="utf-8" src="{url}"></script>\
|
| 280 |
+
""".format(win_config=_window_plotly_config, url=include_plotlyjs_orig)
|
| 281 |
+
|
| 282 |
+
elif include_plotlyjs:
|
| 283 |
+
load_plotlyjs = """\
|
| 284 |
+
{win_config}
|
| 285 |
+
<script type="text/javascript">{plotlyjs}</script>\
|
| 286 |
+
""".format(win_config=_window_plotly_config, plotlyjs=get_plotlyjs())
|
| 287 |
+
|
| 288 |
+
# ## Handle loading/initializing MathJax ##
|
| 289 |
+
include_mathjax_orig = include_mathjax
|
| 290 |
+
if isinstance(include_mathjax, str):
|
| 291 |
+
include_mathjax = include_mathjax.lower()
|
| 292 |
+
|
| 293 |
+
mathjax_template = """\
|
| 294 |
+
<script src="{url}?config=TeX-AMS-MML_SVG"></script>"""
|
| 295 |
+
|
| 296 |
+
if include_mathjax == "cdn":
|
| 297 |
+
mathjax_script = (
|
| 298 |
+
mathjax_template.format(
|
| 299 |
+
url=("https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js")
|
| 300 |
+
)
|
| 301 |
+
+ _mathjax_config
|
| 302 |
+
)
|
| 303 |
+
|
| 304 |
+
elif isinstance(include_mathjax, str) and include_mathjax.endswith(".js"):
|
| 305 |
+
mathjax_script = (
|
| 306 |
+
mathjax_template.format(url=include_mathjax_orig) + _mathjax_config
|
| 307 |
+
)
|
| 308 |
+
elif not include_mathjax:
|
| 309 |
+
mathjax_script = ""
|
| 310 |
+
else:
|
| 311 |
+
raise ValueError(
|
| 312 |
+
"""\
|
| 313 |
+
Invalid value of type {typ} received as the include_mathjax argument
|
| 314 |
+
Received value: {val}
|
| 315 |
+
|
| 316 |
+
include_mathjax may be specified as False, 'cdn', or a string ending with '.js'
|
| 317 |
+
""".format(typ=type(include_mathjax), val=repr(include_mathjax))
|
| 318 |
+
)
|
| 319 |
+
|
| 320 |
+
plotly_html_div = """\
|
| 321 |
+
<div>\
|
| 322 |
+
{mathjax_script}\
|
| 323 |
+
{load_plotlyjs}\
|
| 324 |
+
<div id="{id}" class="plotly-graph-div" \
|
| 325 |
+
style="height:{height}; width:{width};"></div>\
|
| 326 |
+
<script type="text/javascript">\
|
| 327 |
+
window.PLOTLYENV=window.PLOTLYENV || {{}};{base_url_line}\
|
| 328 |
+
{script};\
|
| 329 |
+
</script>\
|
| 330 |
+
</div>""".format(
|
| 331 |
+
mathjax_script=mathjax_script,
|
| 332 |
+
load_plotlyjs=load_plotlyjs,
|
| 333 |
+
id=plotdivid,
|
| 334 |
+
width=div_width,
|
| 335 |
+
height=div_height,
|
| 336 |
+
base_url_line=base_url_line,
|
| 337 |
+
script=script,
|
| 338 |
+
).strip()
|
| 339 |
+
|
| 340 |
+
if full_html:
|
| 341 |
+
return """\
|
| 342 |
+
<html>
|
| 343 |
+
<head><meta charset="utf-8" /></head>
|
| 344 |
+
<body>
|
| 345 |
+
{div}
|
| 346 |
+
</body>
|
| 347 |
+
</html>""".format(div=plotly_html_div)
|
| 348 |
+
else:
|
| 349 |
+
return plotly_html_div
|
| 350 |
+
|
| 351 |
+
|
| 352 |
+
def write_html(
|
| 353 |
+
fig,
|
| 354 |
+
file,
|
| 355 |
+
config=None,
|
| 356 |
+
auto_play=True,
|
| 357 |
+
include_plotlyjs=True,
|
| 358 |
+
include_mathjax=False,
|
| 359 |
+
post_script=None,
|
| 360 |
+
full_html=True,
|
| 361 |
+
animation_opts=None,
|
| 362 |
+
validate=True,
|
| 363 |
+
default_width="100%",
|
| 364 |
+
default_height="100%",
|
| 365 |
+
auto_open=False,
|
| 366 |
+
div_id=None,
|
| 367 |
+
):
|
| 368 |
+
"""
|
| 369 |
+
Write a figure to an HTML file representation
|
| 370 |
+
|
| 371 |
+
Parameters
|
| 372 |
+
----------
|
| 373 |
+
fig:
|
| 374 |
+
Figure object or dict representing a figure
|
| 375 |
+
file: str or writeable
|
| 376 |
+
A string representing a local file path or a writeable object
|
| 377 |
+
(e.g. a pathlib.Path object or an open file descriptor)
|
| 378 |
+
config: dict or None (default None)
|
| 379 |
+
Plotly.js figure config options
|
| 380 |
+
auto_play: bool (default=True)
|
| 381 |
+
Whether to automatically start the animation sequence on page load
|
| 382 |
+
if the figure contains frames. Has no effect if the figure does not
|
| 383 |
+
contain frames.
|
| 384 |
+
include_plotlyjs: bool or string (default True)
|
| 385 |
+
Specifies how the plotly.js library is included/loaded in the output
|
| 386 |
+
div string.
|
| 387 |
+
|
| 388 |
+
If True, a script tag containing the plotly.js source code (~3MB)
|
| 389 |
+
is included in the output. HTML files generated with this option are
|
| 390 |
+
fully self-contained and can be used offline.
|
| 391 |
+
|
| 392 |
+
If 'cdn', a script tag that references the plotly.js CDN is included
|
| 393 |
+
in the output. The url used is versioned to match the bundled plotly.js.
|
| 394 |
+
HTML files generated with this option are about 3MB smaller than those
|
| 395 |
+
generated with include_plotlyjs=True, but they require an active
|
| 396 |
+
internet connection in order to load the plotly.js library.
|
| 397 |
+
|
| 398 |
+
If 'directory', a script tag is included that references an external
|
| 399 |
+
plotly.min.js bundle that is assumed to reside in the same
|
| 400 |
+
directory as the HTML file. If `file` is a string to a local file
|
| 401 |
+
path and `full_html` is True, then the plotly.min.js bundle is copied
|
| 402 |
+
into the directory of the resulting HTML file. If a file named
|
| 403 |
+
plotly.min.js already exists in the output directory then this file
|
| 404 |
+
is left unmodified and no copy is performed. HTML files generated
|
| 405 |
+
with this option can be used offline, but they require a copy of
|
| 406 |
+
the plotly.min.js bundle in the same directory. This option is
|
| 407 |
+
useful when many figures will be saved as HTML files in the same
|
| 408 |
+
directory because the plotly.js source code will be included only
|
| 409 |
+
once per output directory, rather than once per output file.
|
| 410 |
+
|
| 411 |
+
If a string that ends in '.js', a script tag is included that
|
| 412 |
+
references the specified path. This approach can be used to point
|
| 413 |
+
the resulting HTML file to an alternative CDN or local bundle.
|
| 414 |
+
|
| 415 |
+
If False, no script tag referencing plotly.js is included. This is
|
| 416 |
+
useful when the resulting div string will be placed inside an HTML
|
| 417 |
+
document that already loads plotly.js. This option is not advised
|
| 418 |
+
when full_html=True as it will result in a non-functional html file.
|
| 419 |
+
|
| 420 |
+
include_mathjax: bool or string (default False)
|
| 421 |
+
Specifies how the MathJax.js library is included in the output html
|
| 422 |
+
div string. MathJax is required in order to display labels
|
| 423 |
+
with LaTeX typesetting.
|
| 424 |
+
|
| 425 |
+
If False, no script tag referencing MathJax.js will be included in the
|
| 426 |
+
output.
|
| 427 |
+
|
| 428 |
+
If 'cdn', a script tag that references a MathJax CDN location will be
|
| 429 |
+
included in the output. HTML div strings generated with this option
|
| 430 |
+
will be able to display LaTeX typesetting as long as internet access
|
| 431 |
+
is available.
|
| 432 |
+
|
| 433 |
+
If a string that ends in '.js', a script tag is included that
|
| 434 |
+
references the specified path. This approach can be used to point the
|
| 435 |
+
resulting HTML div string to an alternative CDN.
|
| 436 |
+
post_script: str or list or None (default None)
|
| 437 |
+
JavaScript snippet(s) to be included in the resulting div just after
|
| 438 |
+
plot creation. The string(s) may include '{plot_id}' placeholders
|
| 439 |
+
that will then be replaced by the `id` of the div element that the
|
| 440 |
+
plotly.js figure is associated with. One application for this script
|
| 441 |
+
is to install custom plotly.js event handlers.
|
| 442 |
+
full_html: bool (default True)
|
| 443 |
+
If True, produce a string containing a complete HTML document
|
| 444 |
+
starting with an <html> tag. If False, produce a string containing
|
| 445 |
+
a single <div> element.
|
| 446 |
+
animation_opts: dict or None (default None)
|
| 447 |
+
dict of custom animation parameters to be passed to the function
|
| 448 |
+
Plotly.animate in Plotly.js. See
|
| 449 |
+
https://github.com/plotly/plotly.js/blob/master/src/plots/animation_attributes.js
|
| 450 |
+
for available options. Has no effect if the figure does not contain
|
| 451 |
+
frames, or auto_play is False.
|
| 452 |
+
default_width, default_height: number or str (default '100%')
|
| 453 |
+
The default figure width/height to use if the provided figure does not
|
| 454 |
+
specify its own layout.width/layout.height property. May be
|
| 455 |
+
specified in pixels as an integer (e.g. 500), or as a css width style
|
| 456 |
+
string (e.g. '500px', '100%').
|
| 457 |
+
validate: bool (default True)
|
| 458 |
+
True if the figure should be validated before being converted to
|
| 459 |
+
JSON, False otherwise.
|
| 460 |
+
auto_open: bool (default True)
|
| 461 |
+
If True, open the saved file in a web browser after saving.
|
| 462 |
+
This argument only applies if `full_html` is True.
|
| 463 |
+
div_id: str (default None)
|
| 464 |
+
If provided, this is the value of the id attribute of the div tag. If None, the
|
| 465 |
+
id attribute is a UUID.
|
| 466 |
+
|
| 467 |
+
Returns
|
| 468 |
+
-------
|
| 469 |
+
None
|
| 470 |
+
"""
|
| 471 |
+
|
| 472 |
+
# Build HTML string
|
| 473 |
+
html_str = to_html(
|
| 474 |
+
fig,
|
| 475 |
+
config=config,
|
| 476 |
+
auto_play=auto_play,
|
| 477 |
+
include_plotlyjs=include_plotlyjs,
|
| 478 |
+
include_mathjax=include_mathjax,
|
| 479 |
+
post_script=post_script,
|
| 480 |
+
full_html=full_html,
|
| 481 |
+
animation_opts=animation_opts,
|
| 482 |
+
default_width=default_width,
|
| 483 |
+
default_height=default_height,
|
| 484 |
+
validate=validate,
|
| 485 |
+
div_id=div_id,
|
| 486 |
+
)
|
| 487 |
+
|
| 488 |
+
# Check if file is a string
|
| 489 |
+
if isinstance(file, str):
|
| 490 |
+
# Use the standard pathlib constructor to make a pathlib object.
|
| 491 |
+
path = Path(file)
|
| 492 |
+
elif isinstance(file, Path): # PurePath is the most general pathlib object.
|
| 493 |
+
# `file` is already a pathlib object.
|
| 494 |
+
path = file
|
| 495 |
+
else:
|
| 496 |
+
# We could not make a pathlib object out of file. Either `file` is an open file
|
| 497 |
+
# descriptor with a `write()` method or it's an invalid object.
|
| 498 |
+
path = None
|
| 499 |
+
|
| 500 |
+
# Write HTML string
|
| 501 |
+
if path is not None:
|
| 502 |
+
# To use a different file encoding, pass a file descriptor
|
| 503 |
+
path.write_text(html_str, "utf-8")
|
| 504 |
+
else:
|
| 505 |
+
file.write(html_str)
|
| 506 |
+
|
| 507 |
+
# Check if we should copy plotly.min.js to output directory
|
| 508 |
+
if path is not None and full_html and include_plotlyjs == "directory":
|
| 509 |
+
bundle_path = path.parent / "plotly.min.js"
|
| 510 |
+
|
| 511 |
+
if not bundle_path.exists():
|
| 512 |
+
bundle_path.write_text(get_plotlyjs(), encoding="utf-8")
|
| 513 |
+
|
| 514 |
+
# Handle auto_open
|
| 515 |
+
if path is not None and full_html and auto_open:
|
| 516 |
+
url = path.absolute().as_uri()
|
| 517 |
+
webbrowser.open(url)
|
.venv_haddock/lib/python3.12/site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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import decimal
|
| 3 |
+
import datetime
|
| 4 |
+
import warnings
|
| 5 |
+
from pathlib import Path
|
| 6 |
+
|
| 7 |
+
from plotly.io._utils import validate_coerce_fig_to_dict, validate_coerce_output_type
|
| 8 |
+
from _plotly_utils.optional_imports import get_module
|
| 9 |
+
from _plotly_utils.basevalidators import ImageUriValidator
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
# Orca configuration class
|
| 13 |
+
# ------------------------
|
| 14 |
+
class JsonConfig(object):
|
| 15 |
+
_valid_engines = ("json", "orjson", "auto")
|
| 16 |
+
|
| 17 |
+
def __init__(self):
|
| 18 |
+
self._default_engine = "auto"
|
| 19 |
+
|
| 20 |
+
@property
|
| 21 |
+
def default_engine(self):
|
| 22 |
+
return self._default_engine
|
| 23 |
+
|
| 24 |
+
@default_engine.setter
|
| 25 |
+
def default_engine(self, val):
|
| 26 |
+
if val not in JsonConfig._valid_engines:
|
| 27 |
+
raise ValueError(
|
| 28 |
+
"Supported JSON engines include {valid}\n Received {val}".format(
|
| 29 |
+
valid=JsonConfig._valid_engines, val=val
|
| 30 |
+
)
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
if val == "orjson":
|
| 34 |
+
self.validate_orjson()
|
| 35 |
+
|
| 36 |
+
self._default_engine = val
|
| 37 |
+
|
| 38 |
+
@classmethod
|
| 39 |
+
def validate_orjson(cls):
|
| 40 |
+
orjson = get_module("orjson")
|
| 41 |
+
if orjson is None:
|
| 42 |
+
raise ValueError("The orjson engine requires the orjson package")
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
config = JsonConfig()
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
def coerce_to_strict(const):
|
| 49 |
+
"""
|
| 50 |
+
This is used to ultimately *encode* into strict JSON, see `encode`
|
| 51 |
+
|
| 52 |
+
"""
|
| 53 |
+
# before python 2.7, 'true', 'false', 'null', were include here.
|
| 54 |
+
if const in ("Infinity", "-Infinity", "NaN"):
|
| 55 |
+
return None
|
| 56 |
+
else:
|
| 57 |
+
return const
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
_swap_json = (
|
| 61 |
+
("<", "\\u003c"),
|
| 62 |
+
(">", "\\u003e"),
|
| 63 |
+
("/", "\\u002f"),
|
| 64 |
+
)
|
| 65 |
+
_swap_orjson = _swap_json + (
|
| 66 |
+
("\u2028", "\\u2028"),
|
| 67 |
+
("\u2029", "\\u2029"),
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
def _safe(json_str, _swap):
|
| 72 |
+
out = json_str
|
| 73 |
+
for unsafe_char, safe_char in _swap:
|
| 74 |
+
if unsafe_char in out:
|
| 75 |
+
out = out.replace(unsafe_char, safe_char)
|
| 76 |
+
return out
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
def to_json_plotly(plotly_object, pretty=False, engine=None):
|
| 80 |
+
"""
|
| 81 |
+
Convert a plotly/Dash object to a JSON string representation
|
| 82 |
+
|
| 83 |
+
Parameters
|
| 84 |
+
----------
|
| 85 |
+
plotly_object:
|
| 86 |
+
A plotly/Dash object represented as a dict, graph_object, or Dash component
|
| 87 |
+
|
| 88 |
+
pretty: bool (default False)
|
| 89 |
+
True if JSON representation should be pretty-printed, False if
|
| 90 |
+
representation should be as compact as possible.
|
| 91 |
+
|
| 92 |
+
engine: str (default None)
|
| 93 |
+
The JSON encoding engine to use. One of:
|
| 94 |
+
- "json" for an engine based on the built-in Python json module
|
| 95 |
+
- "orjson" for a faster engine that requires the orjson package
|
| 96 |
+
- "auto" for the "orjson" engine if available, otherwise "json"
|
| 97 |
+
If not specified, the default engine is set to the current value of
|
| 98 |
+
plotly.io.json.config.default_engine.
|
| 99 |
+
|
| 100 |
+
Returns
|
| 101 |
+
-------
|
| 102 |
+
str
|
| 103 |
+
Representation of input object as a JSON string
|
| 104 |
+
|
| 105 |
+
See Also
|
| 106 |
+
--------
|
| 107 |
+
to_json : Convert a plotly Figure to JSON with validation
|
| 108 |
+
"""
|
| 109 |
+
orjson = get_module("orjson", should_load=True)
|
| 110 |
+
|
| 111 |
+
# Determine json engine
|
| 112 |
+
if engine is None:
|
| 113 |
+
engine = config.default_engine
|
| 114 |
+
|
| 115 |
+
if engine == "auto":
|
| 116 |
+
if orjson is not None:
|
| 117 |
+
engine = "orjson"
|
| 118 |
+
else:
|
| 119 |
+
engine = "json"
|
| 120 |
+
elif engine not in ["orjson", "json"]:
|
| 121 |
+
raise ValueError("Invalid json engine: %s" % engine)
|
| 122 |
+
|
| 123 |
+
modules = {
|
| 124 |
+
"sage_all": get_module("sage.all", should_load=False),
|
| 125 |
+
"np": get_module("numpy", should_load=False),
|
| 126 |
+
"pd": get_module("pandas", should_load=False),
|
| 127 |
+
"image": get_module("PIL.Image", should_load=False),
|
| 128 |
+
}
|
| 129 |
+
|
| 130 |
+
# Dump to a JSON string and return
|
| 131 |
+
# --------------------------------
|
| 132 |
+
if engine == "json":
|
| 133 |
+
opts = {}
|
| 134 |
+
if pretty:
|
| 135 |
+
opts["indent"] = 2
|
| 136 |
+
else:
|
| 137 |
+
# Remove all whitespace
|
| 138 |
+
opts["separators"] = (",", ":")
|
| 139 |
+
|
| 140 |
+
from _plotly_utils.utils import PlotlyJSONEncoder
|
| 141 |
+
|
| 142 |
+
return _safe(
|
| 143 |
+
json.dumps(plotly_object, cls=PlotlyJSONEncoder, **opts), _swap_json
|
| 144 |
+
)
|
| 145 |
+
elif engine == "orjson":
|
| 146 |
+
JsonConfig.validate_orjson()
|
| 147 |
+
opts = orjson.OPT_NON_STR_KEYS | orjson.OPT_SERIALIZE_NUMPY
|
| 148 |
+
|
| 149 |
+
if pretty:
|
| 150 |
+
opts |= orjson.OPT_INDENT_2
|
| 151 |
+
|
| 152 |
+
# Plotly
|
| 153 |
+
try:
|
| 154 |
+
plotly_object = plotly_object.to_plotly_json()
|
| 155 |
+
except AttributeError:
|
| 156 |
+
pass
|
| 157 |
+
|
| 158 |
+
# Try without cleaning
|
| 159 |
+
try:
|
| 160 |
+
return _safe(
|
| 161 |
+
orjson.dumps(plotly_object, option=opts).decode("utf8"), _swap_orjson
|
| 162 |
+
)
|
| 163 |
+
except TypeError:
|
| 164 |
+
pass
|
| 165 |
+
|
| 166 |
+
cleaned = clean_to_json_compatible(
|
| 167 |
+
plotly_object,
|
| 168 |
+
numpy_allowed=True,
|
| 169 |
+
datetime_allowed=True,
|
| 170 |
+
modules=modules,
|
| 171 |
+
)
|
| 172 |
+
return _safe(orjson.dumps(cleaned, option=opts).decode("utf8"), _swap_orjson)
|
| 173 |
+
|
| 174 |
+
|
| 175 |
+
def to_json(fig, validate=True, pretty=False, remove_uids=True, engine=None):
|
| 176 |
+
"""
|
| 177 |
+
Convert a figure to a JSON string representation
|
| 178 |
+
|
| 179 |
+
Parameters
|
| 180 |
+
----------
|
| 181 |
+
fig:
|
| 182 |
+
Figure object or dict representing a figure
|
| 183 |
+
|
| 184 |
+
validate: bool (default True)
|
| 185 |
+
True if the figure should be validated before being converted to
|
| 186 |
+
JSON, False otherwise.
|
| 187 |
+
|
| 188 |
+
pretty: bool (default False)
|
| 189 |
+
True if JSON representation should be pretty-printed, False if
|
| 190 |
+
representation should be as compact as possible.
|
| 191 |
+
|
| 192 |
+
remove_uids: bool (default True)
|
| 193 |
+
True if trace UIDs should be omitted from the JSON representation
|
| 194 |
+
|
| 195 |
+
engine: str (default None)
|
| 196 |
+
The JSON encoding engine to use. One of:
|
| 197 |
+
- "json" for an engine based on the built-in Python json module
|
| 198 |
+
- "orjson" for a faster engine that requires the orjson package
|
| 199 |
+
- "auto" for the "orjson" engine if available, otherwise "json"
|
| 200 |
+
If not specified, the default engine is set to the current value of
|
| 201 |
+
plotly.io.json.config.default_engine.
|
| 202 |
+
|
| 203 |
+
Returns
|
| 204 |
+
-------
|
| 205 |
+
str
|
| 206 |
+
Representation of figure as a JSON string
|
| 207 |
+
|
| 208 |
+
See Also
|
| 209 |
+
--------
|
| 210 |
+
to_json_plotly : Convert an arbitrary plotly graph_object or Dash component to JSON
|
| 211 |
+
"""
|
| 212 |
+
# Validate figure
|
| 213 |
+
# ---------------
|
| 214 |
+
fig_dict = validate_coerce_fig_to_dict(fig, validate)
|
| 215 |
+
|
| 216 |
+
# Remove trace uid
|
| 217 |
+
# ----------------
|
| 218 |
+
if remove_uids:
|
| 219 |
+
for trace in fig_dict.get("data", []):
|
| 220 |
+
trace.pop("uid", None)
|
| 221 |
+
|
| 222 |
+
return to_json_plotly(fig_dict, pretty=pretty, engine=engine)
|
| 223 |
+
|
| 224 |
+
|
| 225 |
+
def write_json(fig, file, validate=True, pretty=False, remove_uids=True, engine=None):
|
| 226 |
+
"""
|
| 227 |
+
Convert a figure to JSON and write it to a file or writeable
|
| 228 |
+
object.
|
| 229 |
+
|
| 230 |
+
Note: A figure converted to JSON with one version of Plotly.py may not be compatible with another version.
|
| 231 |
+
|
| 232 |
+
Parameters
|
| 233 |
+
----------
|
| 234 |
+
fig:
|
| 235 |
+
Figure object or dict representing a figure
|
| 236 |
+
|
| 237 |
+
file: str or writeable
|
| 238 |
+
A string representing a local file path or a writeable object
|
| 239 |
+
(e.g. a pathlib.Path object or an open file descriptor)
|
| 240 |
+
|
| 241 |
+
pretty: bool (default False)
|
| 242 |
+
True if JSON representation should be pretty-printed, False if
|
| 243 |
+
representation should be as compact as possible.
|
| 244 |
+
|
| 245 |
+
remove_uids: bool (default True)
|
| 246 |
+
True if trace UIDs should be omitted from the JSON representation
|
| 247 |
+
|
| 248 |
+
engine: str (default None)
|
| 249 |
+
The JSON encoding engine to use. One of:
|
| 250 |
+
- "json" for an engine based on the built-in Python json module
|
| 251 |
+
- "orjson" for a faster engine that requires the orjson package
|
| 252 |
+
- "auto" for the "orjson" engine if available, otherwise "json"
|
| 253 |
+
If not specified, the default engine is set to the current value of
|
| 254 |
+
plotly.io.json.config.default_engine.
|
| 255 |
+
Returns
|
| 256 |
+
-------
|
| 257 |
+
None
|
| 258 |
+
"""
|
| 259 |
+
|
| 260 |
+
# Get JSON string
|
| 261 |
+
# ---------------
|
| 262 |
+
# Pass through validate argument and let to_json handle validation logic
|
| 263 |
+
json_str = to_json(
|
| 264 |
+
fig, validate=validate, pretty=pretty, remove_uids=remove_uids, engine=engine
|
| 265 |
+
)
|
| 266 |
+
|
| 267 |
+
# Try to cast `file` as a pathlib object `path`.
|
| 268 |
+
# ----------------------------------------------
|
| 269 |
+
if isinstance(file, str):
|
| 270 |
+
# Use the standard Path constructor to make a pathlib object.
|
| 271 |
+
path = Path(file)
|
| 272 |
+
elif isinstance(file, Path):
|
| 273 |
+
# `file` is already a Path object.
|
| 274 |
+
path = file
|
| 275 |
+
else:
|
| 276 |
+
# We could not make a Path object out of file. Either `file` is an open file
|
| 277 |
+
# descriptor with a `write()` method or it's an invalid object.
|
| 278 |
+
path = None
|
| 279 |
+
|
| 280 |
+
# Open file
|
| 281 |
+
# ---------
|
| 282 |
+
if path is None:
|
| 283 |
+
# We previously failed to make sense of `file` as a pathlib object.
|
| 284 |
+
# Attempt to write to `file` as an open file descriptor.
|
| 285 |
+
try:
|
| 286 |
+
file.write(json_str)
|
| 287 |
+
return
|
| 288 |
+
except AttributeError:
|
| 289 |
+
pass
|
| 290 |
+
raise ValueError(
|
| 291 |
+
"""
|
| 292 |
+
The 'file' argument '{file}' is not a string, pathlib.Path object, or file descriptor.
|
| 293 |
+
""".format(file=file)
|
| 294 |
+
)
|
| 295 |
+
else:
|
| 296 |
+
# We previously succeeded in interpreting `file` as a pathlib object.
|
| 297 |
+
# Now we can use `write_bytes()`.
|
| 298 |
+
path.write_text(json_str)
|
| 299 |
+
|
| 300 |
+
|
| 301 |
+
def from_json_plotly(value, engine=None):
|
| 302 |
+
"""
|
| 303 |
+
Parse JSON string using the specified JSON engine
|
| 304 |
+
|
| 305 |
+
Parameters
|
| 306 |
+
----------
|
| 307 |
+
value: str or bytes
|
| 308 |
+
A JSON string or bytes object
|
| 309 |
+
|
| 310 |
+
engine: str (default None)
|
| 311 |
+
The JSON decoding engine to use. One of:
|
| 312 |
+
- if "json", parse JSON using built in json module
|
| 313 |
+
- if "orjson", parse using the faster orjson module, requires the orjson
|
| 314 |
+
package
|
| 315 |
+
- if "auto" use orjson module if available, otherwise use the json module
|
| 316 |
+
|
| 317 |
+
If not specified, the default engine is set to the current value of
|
| 318 |
+
plotly.io.json.config.default_engine.
|
| 319 |
+
|
| 320 |
+
Returns
|
| 321 |
+
-------
|
| 322 |
+
dict
|
| 323 |
+
|
| 324 |
+
See Also
|
| 325 |
+
--------
|
| 326 |
+
from_json_plotly : Parse JSON with plotly conventions into a dict
|
| 327 |
+
"""
|
| 328 |
+
orjson = get_module("orjson", should_load=True)
|
| 329 |
+
|
| 330 |
+
# Validate value
|
| 331 |
+
# --------------
|
| 332 |
+
if not isinstance(value, (str, bytes)):
|
| 333 |
+
raise ValueError(
|
| 334 |
+
"""
|
| 335 |
+
from_json_plotly requires a string or bytes argument but received value of type {typ}
|
| 336 |
+
Received value: {value}""".format(typ=type(value), value=value)
|
| 337 |
+
)
|
| 338 |
+
|
| 339 |
+
# Determine json engine
|
| 340 |
+
if engine is None:
|
| 341 |
+
engine = config.default_engine
|
| 342 |
+
|
| 343 |
+
if engine == "auto":
|
| 344 |
+
if orjson is not None:
|
| 345 |
+
engine = "orjson"
|
| 346 |
+
else:
|
| 347 |
+
engine = "json"
|
| 348 |
+
elif engine not in ["orjson", "json"]:
|
| 349 |
+
raise ValueError("Invalid json engine: %s" % engine)
|
| 350 |
+
|
| 351 |
+
if engine == "orjson":
|
| 352 |
+
JsonConfig.validate_orjson()
|
| 353 |
+
# orjson handles bytes input natively
|
| 354 |
+
value_dict = orjson.loads(value)
|
| 355 |
+
else:
|
| 356 |
+
# decode bytes to str for built-in json module
|
| 357 |
+
if isinstance(value, bytes):
|
| 358 |
+
value = value.decode("utf-8")
|
| 359 |
+
value_dict = json.loads(value)
|
| 360 |
+
|
| 361 |
+
return value_dict
|
| 362 |
+
|
| 363 |
+
|
| 364 |
+
def from_json(value, output_type="Figure", skip_invalid=False, engine=None):
|
| 365 |
+
"""
|
| 366 |
+
Construct a figure from a JSON string
|
| 367 |
+
|
| 368 |
+
Parameters
|
| 369 |
+
----------
|
| 370 |
+
value: str or bytes
|
| 371 |
+
String or bytes object containing the JSON representation of a figure
|
| 372 |
+
|
| 373 |
+
output_type: type or str (default 'Figure')
|
| 374 |
+
The output figure type or type name.
|
| 375 |
+
One of: graph_objs.Figure, 'Figure', graph_objs.FigureWidget, 'FigureWidget'
|
| 376 |
+
|
| 377 |
+
skip_invalid: bool (default False)
|
| 378 |
+
False if invalid figure properties should result in an exception.
|
| 379 |
+
True if invalid figure properties should be silently ignored.
|
| 380 |
+
|
| 381 |
+
engine: str (default None)
|
| 382 |
+
The JSON decoding engine to use. One of:
|
| 383 |
+
- if "json", parse JSON using built in json module
|
| 384 |
+
- if "orjson", parse using the faster orjson module, requires the orjson
|
| 385 |
+
package
|
| 386 |
+
- if "auto" use orjson module if available, otherwise use the json module
|
| 387 |
+
|
| 388 |
+
If not specified, the default engine is set to the current value of
|
| 389 |
+
plotly.io.json.config.default_engine.
|
| 390 |
+
|
| 391 |
+
Raises
|
| 392 |
+
------
|
| 393 |
+
ValueError
|
| 394 |
+
if value is not a string, or if skip_invalid=False and value contains
|
| 395 |
+
invalid figure properties
|
| 396 |
+
|
| 397 |
+
Returns
|
| 398 |
+
-------
|
| 399 |
+
Figure or FigureWidget
|
| 400 |
+
"""
|
| 401 |
+
|
| 402 |
+
# Decode JSON
|
| 403 |
+
# -----------
|
| 404 |
+
fig_dict = from_json_plotly(value, engine=engine)
|
| 405 |
+
|
| 406 |
+
# Validate coerce output type
|
| 407 |
+
# ---------------------------
|
| 408 |
+
cls = validate_coerce_output_type(output_type)
|
| 409 |
+
|
| 410 |
+
# Create and return figure
|
| 411 |
+
# ------------------------
|
| 412 |
+
fig = cls(fig_dict, skip_invalid=skip_invalid)
|
| 413 |
+
return fig
|
| 414 |
+
|
| 415 |
+
|
| 416 |
+
def read_json(file, output_type="Figure", skip_invalid=False, engine=None):
|
| 417 |
+
"""
|
| 418 |
+
Construct a figure from the JSON contents of a local file or readable
|
| 419 |
+
Python object.
|
| 420 |
+
|
| 421 |
+
Note: A figure converted to JSON with one version of Plotly.py may not be compatible with another version.
|
| 422 |
+
|
| 423 |
+
Parameters
|
| 424 |
+
----------
|
| 425 |
+
file: str or readable
|
| 426 |
+
A string containing the path to a local file or a read-able Python
|
| 427 |
+
object (e.g. a pathlib.Path object or an open file descriptor)
|
| 428 |
+
|
| 429 |
+
output_type: type or str (default 'Figure')
|
| 430 |
+
The output figure type or type name.
|
| 431 |
+
One of: graph_objs.Figure, 'Figure', graph_objs.FigureWidget, 'FigureWidget'
|
| 432 |
+
|
| 433 |
+
skip_invalid: bool (default False)
|
| 434 |
+
False if invalid figure properties should result in an exception.
|
| 435 |
+
True if invalid figure properties should be silently ignored.
|
| 436 |
+
|
| 437 |
+
engine: str (default None)
|
| 438 |
+
The JSON decoding engine to use. One of:
|
| 439 |
+
- if "json", parse JSON using built in json module
|
| 440 |
+
- if "orjson", parse using the faster orjson module, requires the orjson
|
| 441 |
+
package
|
| 442 |
+
- if "auto" use orjson module if available, otherwise use the json module
|
| 443 |
+
|
| 444 |
+
If not specified, the default engine is set to the current value of
|
| 445 |
+
plotly.io.json.config.default_engine.
|
| 446 |
+
|
| 447 |
+
Returns
|
| 448 |
+
-------
|
| 449 |
+
Figure or FigureWidget
|
| 450 |
+
"""
|
| 451 |
+
|
| 452 |
+
# Try to cast `file` as a pathlib object `path`.
|
| 453 |
+
if isinstance(file, str):
|
| 454 |
+
# Use the standard Path constructor to make a pathlib object.
|
| 455 |
+
path = Path(file)
|
| 456 |
+
elif isinstance(file, Path):
|
| 457 |
+
# `file` is already a Path object.
|
| 458 |
+
path = file
|
| 459 |
+
else:
|
| 460 |
+
# We could not make a Path object out of file. Either `file` is an open file
|
| 461 |
+
# descriptor with a `write()` method or it's an invalid object.
|
| 462 |
+
path = None
|
| 463 |
+
|
| 464 |
+
# Read file contents into JSON string
|
| 465 |
+
# -----------------------------------
|
| 466 |
+
if path is not None:
|
| 467 |
+
json_str = path.read_text()
|
| 468 |
+
else:
|
| 469 |
+
json_str = file.read()
|
| 470 |
+
|
| 471 |
+
# Construct and return figure
|
| 472 |
+
# ---------------------------
|
| 473 |
+
return from_json(
|
| 474 |
+
json_str, skip_invalid=skip_invalid, output_type=output_type, engine=engine
|
| 475 |
+
)
|
| 476 |
+
|
| 477 |
+
|
| 478 |
+
def clean_to_json_compatible(obj, **kwargs):
|
| 479 |
+
# Try handling value as a scalar value that we have a conversion for.
|
| 480 |
+
# Return immediately if we know we've hit a primitive value
|
| 481 |
+
|
| 482 |
+
# Bail out fast for simple scalar types
|
| 483 |
+
if isinstance(obj, (int, float, str)):
|
| 484 |
+
return obj
|
| 485 |
+
|
| 486 |
+
if isinstance(obj, dict):
|
| 487 |
+
return {k: clean_to_json_compatible(v, **kwargs) for k, v in obj.items()}
|
| 488 |
+
elif isinstance(obj, (list, tuple)):
|
| 489 |
+
if obj:
|
| 490 |
+
# Must process list recursively even though it may be slow
|
| 491 |
+
return [clean_to_json_compatible(v, **kwargs) for v in obj]
|
| 492 |
+
|
| 493 |
+
# unpack kwargs
|
| 494 |
+
numpy_allowed = kwargs.get("numpy_allowed", False)
|
| 495 |
+
datetime_allowed = kwargs.get("datetime_allowed", False)
|
| 496 |
+
|
| 497 |
+
modules = kwargs.get("modules", {})
|
| 498 |
+
sage_all = modules["sage_all"]
|
| 499 |
+
np = modules["np"]
|
| 500 |
+
pd = modules["pd"]
|
| 501 |
+
image = modules["image"]
|
| 502 |
+
|
| 503 |
+
# Sage
|
| 504 |
+
if sage_all is not None:
|
| 505 |
+
if obj in sage_all.RR:
|
| 506 |
+
return float(obj)
|
| 507 |
+
elif obj in sage_all.ZZ:
|
| 508 |
+
return int(obj)
|
| 509 |
+
|
| 510 |
+
# numpy
|
| 511 |
+
if np is not None:
|
| 512 |
+
if obj is np.ma.core.masked:
|
| 513 |
+
return float("nan")
|
| 514 |
+
elif isinstance(obj, np.ndarray):
|
| 515 |
+
if numpy_allowed and obj.dtype.kind in ("b", "i", "u", "f"):
|
| 516 |
+
return np.ascontiguousarray(obj)
|
| 517 |
+
elif obj.dtype.kind == "M":
|
| 518 |
+
# datetime64 array
|
| 519 |
+
return np.datetime_as_string(obj).tolist()
|
| 520 |
+
elif obj.dtype.kind == "U":
|
| 521 |
+
return obj.tolist()
|
| 522 |
+
elif obj.dtype.kind == "O":
|
| 523 |
+
# Treat object array as a lists, continue processing
|
| 524 |
+
obj = obj.tolist()
|
| 525 |
+
elif isinstance(obj, np.datetime64):
|
| 526 |
+
return str(obj)
|
| 527 |
+
|
| 528 |
+
# pandas
|
| 529 |
+
if pd is not None:
|
| 530 |
+
if obj is pd.NaT or obj is pd.NA:
|
| 531 |
+
return None
|
| 532 |
+
elif isinstance(obj, (pd.Series, pd.DatetimeIndex)):
|
| 533 |
+
if numpy_allowed and obj.dtype.kind in ("b", "i", "u", "f"):
|
| 534 |
+
return np.ascontiguousarray(obj.values)
|
| 535 |
+
elif obj.dtype.kind == "M":
|
| 536 |
+
if isinstance(obj, pd.Series):
|
| 537 |
+
with warnings.catch_warnings():
|
| 538 |
+
warnings.simplefilter("ignore", FutureWarning)
|
| 539 |
+
# Series.dt.to_pydatetime will return Index[object]
|
| 540 |
+
# https://github.com/pandas-dev/pandas/pull/52459
|
| 541 |
+
dt_values = np.array(obj.dt.to_pydatetime()).tolist()
|
| 542 |
+
else: # DatetimeIndex
|
| 543 |
+
dt_values = obj.to_pydatetime().tolist()
|
| 544 |
+
|
| 545 |
+
if not datetime_allowed:
|
| 546 |
+
# Note: We don't need to handle dropping timezones here because
|
| 547 |
+
# numpy's datetime64 doesn't support them and pandas's tz_localize
|
| 548 |
+
# above drops them.
|
| 549 |
+
for i in range(len(dt_values)):
|
| 550 |
+
dt_values[i] = dt_values[i].isoformat()
|
| 551 |
+
|
| 552 |
+
return dt_values
|
| 553 |
+
|
| 554 |
+
# datetime and date
|
| 555 |
+
try:
|
| 556 |
+
# Need to drop timezone for scalar datetimes. Don't need to convert
|
| 557 |
+
# to string since engine can do that
|
| 558 |
+
obj = obj.to_pydatetime()
|
| 559 |
+
except (TypeError, AttributeError):
|
| 560 |
+
pass
|
| 561 |
+
|
| 562 |
+
if not datetime_allowed:
|
| 563 |
+
try:
|
| 564 |
+
return obj.isoformat()
|
| 565 |
+
except (TypeError, AttributeError):
|
| 566 |
+
pass
|
| 567 |
+
elif isinstance(obj, datetime.datetime):
|
| 568 |
+
return obj
|
| 569 |
+
|
| 570 |
+
# Try .tolist() convertible, do not recurse inside
|
| 571 |
+
try:
|
| 572 |
+
return obj.tolist()
|
| 573 |
+
except AttributeError:
|
| 574 |
+
pass
|
| 575 |
+
|
| 576 |
+
# Do best we can with decimal
|
| 577 |
+
if isinstance(obj, decimal.Decimal):
|
| 578 |
+
return float(obj)
|
| 579 |
+
|
| 580 |
+
# PIL
|
| 581 |
+
if image is not None and isinstance(obj, image.Image):
|
| 582 |
+
return ImageUriValidator.pil_image_to_uri(obj)
|
| 583 |
+
|
| 584 |
+
# Plotly
|
| 585 |
+
try:
|
| 586 |
+
obj = obj.to_plotly_json()
|
| 587 |
+
except AttributeError:
|
| 588 |
+
pass
|
| 589 |
+
|
| 590 |
+
# Recurse into lists and dictionaries
|
| 591 |
+
if isinstance(obj, dict):
|
| 592 |
+
return {k: clean_to_json_compatible(v, **kwargs) for k, v in obj.items()}
|
| 593 |
+
elif isinstance(obj, (list, tuple)):
|
| 594 |
+
if obj:
|
| 595 |
+
# Must process list recursively even though it may be slow
|
| 596 |
+
return [clean_to_json_compatible(v, **kwargs) for v in obj]
|
| 597 |
+
|
| 598 |
+
return obj
|
.venv_haddock/lib/python3.12/site-packages/plotly/io/_kaleido.py
ADDED
|
@@ -0,0 +1,918 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import json
|
| 3 |
+
from pathlib import Path
|
| 4 |
+
from typing import Union, List
|
| 5 |
+
import importlib.metadata as importlib_metadata
|
| 6 |
+
from packaging.version import Version
|
| 7 |
+
import warnings
|
| 8 |
+
|
| 9 |
+
import plotly
|
| 10 |
+
from plotly.io._utils import validate_coerce_fig_to_dict, broadcast_args_to_dicts
|
| 11 |
+
from plotly.io._defaults import defaults
|
| 12 |
+
|
| 13 |
+
ENGINE_SUPPORT_TIMELINE = "September 2025"
|
| 14 |
+
ENABLE_KALEIDO_V0_DEPRECATION_WARNINGS = True
|
| 15 |
+
|
| 16 |
+
PLOTLY_GET_CHROME_ERROR_MSG = """
|
| 17 |
+
|
| 18 |
+
Kaleido requires Google Chrome to be installed.
|
| 19 |
+
|
| 20 |
+
Either download and install Chrome yourself following Google's instructions for your operating system,
|
| 21 |
+
or install it from your terminal by running:
|
| 22 |
+
|
| 23 |
+
$ plotly_get_chrome
|
| 24 |
+
|
| 25 |
+
"""
|
| 26 |
+
|
| 27 |
+
KALEIDO_DEPRECATION_MSG = f"""
|
| 28 |
+
Support for Kaleido versions less than 1.0.0 is deprecated and will be removed after {ENGINE_SUPPORT_TIMELINE}.
|
| 29 |
+
Please upgrade Kaleido to version 1.0.0 or greater (`pip install 'kaleido>=1.0.0'` or `pip install 'plotly[kaleido]'`).
|
| 30 |
+
"""
|
| 31 |
+
ORCA_DEPRECATION_MSG = f"""
|
| 32 |
+
Support for the Orca engine is deprecated and will be removed after {ENGINE_SUPPORT_TIMELINE}.
|
| 33 |
+
Please install Kaleido (`pip install 'kaleido>=1.0.0'` or `pip install 'plotly[kaleido]'`) to use the Kaleido engine.
|
| 34 |
+
"""
|
| 35 |
+
ENGINE_PARAM_DEPRECATION_MSG = f"""
|
| 36 |
+
Support for the 'engine' argument is deprecated and will be removed after {ENGINE_SUPPORT_TIMELINE}.
|
| 37 |
+
Kaleido will be the only supported engine at that time.
|
| 38 |
+
"""
|
| 39 |
+
|
| 40 |
+
_KALEIDO_AVAILABLE = None
|
| 41 |
+
_KALEIDO_MAJOR = None
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
def kaleido_scope_default_warning_func(x):
|
| 45 |
+
return f"""
|
| 46 |
+
Use of plotly.io.kaleido.scope.{x} is deprecated and support will be removed after {ENGINE_SUPPORT_TIMELINE}.
|
| 47 |
+
Please use plotly.io.defaults.{x} instead.
|
| 48 |
+
"""
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
def bad_attribute_error_msg_func(x):
|
| 52 |
+
return f"""
|
| 53 |
+
Attribute plotly.io.defaults.{x} is not valid.
|
| 54 |
+
Also, use of plotly.io.kaleido.scope.* is deprecated and support will be removed after {ENGINE_SUPPORT_TIMELINE}.
|
| 55 |
+
Please use plotly.io.defaults.* instead.
|
| 56 |
+
"""
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
def kaleido_available() -> bool:
|
| 60 |
+
"""
|
| 61 |
+
Returns True if any version of Kaleido is installed, otherwise False.
|
| 62 |
+
"""
|
| 63 |
+
global _KALEIDO_AVAILABLE
|
| 64 |
+
global _KALEIDO_MAJOR
|
| 65 |
+
if _KALEIDO_AVAILABLE is not None:
|
| 66 |
+
return _KALEIDO_AVAILABLE
|
| 67 |
+
try:
|
| 68 |
+
import kaleido # noqa: F401
|
| 69 |
+
|
| 70 |
+
_KALEIDO_AVAILABLE = True
|
| 71 |
+
except ImportError:
|
| 72 |
+
_KALEIDO_AVAILABLE = False
|
| 73 |
+
return _KALEIDO_AVAILABLE
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
def kaleido_major() -> int:
|
| 77 |
+
"""
|
| 78 |
+
Returns the major version number of Kaleido if it is installed,
|
| 79 |
+
otherwise raises a ValueError.
|
| 80 |
+
"""
|
| 81 |
+
global _KALEIDO_MAJOR
|
| 82 |
+
if _KALEIDO_MAJOR is not None:
|
| 83 |
+
return _KALEIDO_MAJOR
|
| 84 |
+
if not kaleido_available():
|
| 85 |
+
raise ValueError("Kaleido is not installed.")
|
| 86 |
+
else:
|
| 87 |
+
_KALEIDO_MAJOR = Version(importlib_metadata.version("kaleido")).major
|
| 88 |
+
return _KALEIDO_MAJOR
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
try:
|
| 92 |
+
if kaleido_available() and kaleido_major() < 1:
|
| 93 |
+
# Kaleido v0
|
| 94 |
+
import kaleido
|
| 95 |
+
from kaleido.scopes.plotly import PlotlyScope
|
| 96 |
+
|
| 97 |
+
# Show a deprecation warning if the old method of setting defaults is used
|
| 98 |
+
class PlotlyScopeWrapper(PlotlyScope):
|
| 99 |
+
def __setattr__(self, name, value):
|
| 100 |
+
if name in defaults.__dict__:
|
| 101 |
+
if ENABLE_KALEIDO_V0_DEPRECATION_WARNINGS:
|
| 102 |
+
warnings.warn(
|
| 103 |
+
kaleido_scope_default_warning_func(name),
|
| 104 |
+
DeprecationWarning,
|
| 105 |
+
stacklevel=2,
|
| 106 |
+
)
|
| 107 |
+
super().__setattr__(name, value)
|
| 108 |
+
|
| 109 |
+
def __getattr__(self, name):
|
| 110 |
+
if hasattr(defaults, name):
|
| 111 |
+
if ENABLE_KALEIDO_V0_DEPRECATION_WARNINGS:
|
| 112 |
+
warnings.warn(
|
| 113 |
+
kaleido_scope_default_warning_func(name),
|
| 114 |
+
DeprecationWarning,
|
| 115 |
+
stacklevel=2,
|
| 116 |
+
)
|
| 117 |
+
return super().__getattr__(name)
|
| 118 |
+
|
| 119 |
+
# Ensure the new method of setting defaults is backwards compatible with Kaleido v0
|
| 120 |
+
# DefaultsBackwardsCompatible sets the attributes on `scope` object at the same time
|
| 121 |
+
# as they are set on the `defaults` object
|
| 122 |
+
class DefaultsBackwardsCompatible(defaults.__class__):
|
| 123 |
+
def __init__(self, scope):
|
| 124 |
+
self._scope = scope
|
| 125 |
+
super().__init__()
|
| 126 |
+
|
| 127 |
+
def __setattr__(self, name, value):
|
| 128 |
+
if not name == "_scope":
|
| 129 |
+
if (
|
| 130 |
+
hasattr(self._scope, name)
|
| 131 |
+
and getattr(self._scope, name) != value
|
| 132 |
+
):
|
| 133 |
+
setattr(self._scope, name, value)
|
| 134 |
+
super().__setattr__(name, value)
|
| 135 |
+
|
| 136 |
+
scope = PlotlyScopeWrapper()
|
| 137 |
+
defaults = DefaultsBackwardsCompatible(scope)
|
| 138 |
+
# Compute absolute path to the 'plotly/package_data/' directory
|
| 139 |
+
root_dir = os.path.dirname(os.path.abspath(plotly.__file__))
|
| 140 |
+
package_dir = os.path.join(root_dir, "package_data")
|
| 141 |
+
scope.plotlyjs = os.path.join(package_dir, "plotly.min.js")
|
| 142 |
+
if scope.mathjax is None:
|
| 143 |
+
with warnings.catch_warnings():
|
| 144 |
+
warnings.filterwarnings(
|
| 145 |
+
"ignore", message=r".*scope\.mathjax.*", category=DeprecationWarning
|
| 146 |
+
)
|
| 147 |
+
scope.mathjax = (
|
| 148 |
+
"https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js"
|
| 149 |
+
)
|
| 150 |
+
else:
|
| 151 |
+
# Kaleido v1
|
| 152 |
+
import kaleido
|
| 153 |
+
|
| 154 |
+
# Show a deprecation warning if the old method of setting defaults is used
|
| 155 |
+
class DefaultsWrapper:
|
| 156 |
+
def __getattr__(self, name):
|
| 157 |
+
if hasattr(defaults, name):
|
| 158 |
+
if ENABLE_KALEIDO_V0_DEPRECATION_WARNINGS:
|
| 159 |
+
warnings.warn(
|
| 160 |
+
kaleido_scope_default_warning_func(name),
|
| 161 |
+
DeprecationWarning,
|
| 162 |
+
stacklevel=2,
|
| 163 |
+
)
|
| 164 |
+
return getattr(defaults, name)
|
| 165 |
+
else:
|
| 166 |
+
raise AttributeError(bad_attribute_error_msg_func(name))
|
| 167 |
+
|
| 168 |
+
def __setattr__(self, name, value):
|
| 169 |
+
if hasattr(defaults, name):
|
| 170 |
+
if ENABLE_KALEIDO_V0_DEPRECATION_WARNINGS:
|
| 171 |
+
warnings.warn(
|
| 172 |
+
kaleido_scope_default_warning_func(name),
|
| 173 |
+
DeprecationWarning,
|
| 174 |
+
stacklevel=2,
|
| 175 |
+
)
|
| 176 |
+
setattr(defaults, name, value)
|
| 177 |
+
else:
|
| 178 |
+
raise AttributeError(bad_attribute_error_msg_func(name))
|
| 179 |
+
|
| 180 |
+
scope = DefaultsWrapper()
|
| 181 |
+
|
| 182 |
+
except ImportError:
|
| 183 |
+
PlotlyScope = None
|
| 184 |
+
scope = None
|
| 185 |
+
|
| 186 |
+
|
| 187 |
+
def as_path_object(file: Union[str, Path]) -> Union[Path, None]:
|
| 188 |
+
"""
|
| 189 |
+
Cast the `file` argument, which may be either a string or a Path object,
|
| 190 |
+
to a Path object.
|
| 191 |
+
If `file` is neither a string nor a Path object, None will be returned.
|
| 192 |
+
"""
|
| 193 |
+
if isinstance(file, str):
|
| 194 |
+
# Use the standard Path constructor to make a pathlib object.
|
| 195 |
+
path = Path(file)
|
| 196 |
+
elif isinstance(file, Path):
|
| 197 |
+
# `file` is already a Path object.
|
| 198 |
+
path = file
|
| 199 |
+
else:
|
| 200 |
+
# We could not make a Path object out of file. Either `file` is an open file
|
| 201 |
+
# descriptor with a `write()` method or it's an invalid object.
|
| 202 |
+
path = None
|
| 203 |
+
return path
|
| 204 |
+
|
| 205 |
+
|
| 206 |
+
def infer_format(path: Union[Path, None], format: Union[str, None]) -> Union[str, None]:
|
| 207 |
+
if path is not None and format is None:
|
| 208 |
+
ext = path.suffix
|
| 209 |
+
if ext:
|
| 210 |
+
format = ext.lstrip(".")
|
| 211 |
+
else:
|
| 212 |
+
raise ValueError(
|
| 213 |
+
f"""
|
| 214 |
+
Cannot infer image type from output path '{path}'.
|
| 215 |
+
Please specify the type using the format parameter, or add a file extension.
|
| 216 |
+
For example:
|
| 217 |
+
|
| 218 |
+
>>> import plotly.io as pio
|
| 219 |
+
>>> pio.write_image(fig, file_path, format='png')
|
| 220 |
+
"""
|
| 221 |
+
)
|
| 222 |
+
return format
|
| 223 |
+
|
| 224 |
+
|
| 225 |
+
def to_image(
|
| 226 |
+
fig: Union[dict, plotly.graph_objects.Figure],
|
| 227 |
+
format: Union[str, None] = None,
|
| 228 |
+
width: Union[int, None] = None,
|
| 229 |
+
height: Union[int, None] = None,
|
| 230 |
+
scale: Union[int, float, None] = None,
|
| 231 |
+
validate: bool = True,
|
| 232 |
+
# Deprecated
|
| 233 |
+
engine: Union[str, None] = None,
|
| 234 |
+
) -> bytes:
|
| 235 |
+
"""
|
| 236 |
+
Convert a figure to a static image bytes string
|
| 237 |
+
|
| 238 |
+
Parameters
|
| 239 |
+
----------
|
| 240 |
+
fig:
|
| 241 |
+
Figure object or dict representing a figure
|
| 242 |
+
|
| 243 |
+
format: str or None
|
| 244 |
+
The desired image format. One of
|
| 245 |
+
- 'png'
|
| 246 |
+
- 'jpg' or 'jpeg'
|
| 247 |
+
- 'webp'
|
| 248 |
+
- 'svg'
|
| 249 |
+
- 'pdf'
|
| 250 |
+
- 'eps' (deprecated) (Requires the poppler library to be installed and on the PATH)
|
| 251 |
+
|
| 252 |
+
If not specified, will default to:
|
| 253 |
+
- `plotly.io.defaults.default_format` if engine is "kaleido"
|
| 254 |
+
- `plotly.io.orca.config.default_format` if engine is "orca" (deprecated)
|
| 255 |
+
|
| 256 |
+
width: int or None
|
| 257 |
+
The width of the exported image in layout pixels. If the `scale`
|
| 258 |
+
property is 1.0, this will also be the width of the exported image
|
| 259 |
+
in physical pixels.
|
| 260 |
+
|
| 261 |
+
If not specified, will default to:
|
| 262 |
+
- `plotly.io.defaults.default_width` if engine is "kaleido"
|
| 263 |
+
- `plotly.io.orca.config.default_width` if engine is "orca" (deprecated)
|
| 264 |
+
|
| 265 |
+
height: int or None
|
| 266 |
+
The height of the exported image in layout pixels. If the `scale`
|
| 267 |
+
property is 1.0, this will also be the height of the exported image
|
| 268 |
+
in physical pixels.
|
| 269 |
+
|
| 270 |
+
If not specified, will default to:
|
| 271 |
+
- `plotly.io.defaults.default_height` if engine is "kaleido"
|
| 272 |
+
- `plotly.io.orca.config.default_height` if engine is "orca" (deprecated)
|
| 273 |
+
|
| 274 |
+
scale: int or float or None
|
| 275 |
+
The scale factor to use when exporting the figure. A scale factor
|
| 276 |
+
larger than 1.0 will increase the image resolution with respect
|
| 277 |
+
to the figure's layout pixel dimensions. Whereas as scale factor of
|
| 278 |
+
less than 1.0 will decrease the image resolution.
|
| 279 |
+
|
| 280 |
+
If not specified, will default to:
|
| 281 |
+
- `plotly.io.defaults.default_scale` if engine is "kaleido"
|
| 282 |
+
- `plotly.io.orca.config.default_scale` if engine is "orca" (deprecated)
|
| 283 |
+
|
| 284 |
+
validate: bool
|
| 285 |
+
True if the figure should be validated before being converted to
|
| 286 |
+
an image, False otherwise.
|
| 287 |
+
|
| 288 |
+
engine (deprecated): str
|
| 289 |
+
Image export engine to use. This parameter is deprecated and Orca engine support will be
|
| 290 |
+
dropped in the next major Plotly version. Until then, the following values are supported:
|
| 291 |
+
- "kaleido": Use Kaleido for image export
|
| 292 |
+
- "orca": Use Orca for image export
|
| 293 |
+
- "auto" (default): Use Kaleido if installed, otherwise use Orca
|
| 294 |
+
|
| 295 |
+
Returns
|
| 296 |
+
-------
|
| 297 |
+
bytes
|
| 298 |
+
The image data
|
| 299 |
+
"""
|
| 300 |
+
|
| 301 |
+
# Handle engine
|
| 302 |
+
if engine is not None:
|
| 303 |
+
if ENABLE_KALEIDO_V0_DEPRECATION_WARNINGS:
|
| 304 |
+
warnings.warn(
|
| 305 |
+
ENGINE_PARAM_DEPRECATION_MSG, DeprecationWarning, stacklevel=2
|
| 306 |
+
)
|
| 307 |
+
else:
|
| 308 |
+
engine = "auto"
|
| 309 |
+
|
| 310 |
+
if engine == "auto":
|
| 311 |
+
if kaleido_available():
|
| 312 |
+
# Default to kaleido if available
|
| 313 |
+
engine = "kaleido"
|
| 314 |
+
else:
|
| 315 |
+
# See if orca is available
|
| 316 |
+
from ._orca import validate_executable
|
| 317 |
+
|
| 318 |
+
try:
|
| 319 |
+
validate_executable()
|
| 320 |
+
engine = "orca"
|
| 321 |
+
except Exception:
|
| 322 |
+
# If orca not configured properly, make sure we display the error
|
| 323 |
+
# message advising the installation of kaleido
|
| 324 |
+
engine = "kaleido"
|
| 325 |
+
|
| 326 |
+
if engine == "orca":
|
| 327 |
+
if ENABLE_KALEIDO_V0_DEPRECATION_WARNINGS:
|
| 328 |
+
warnings.warn(ORCA_DEPRECATION_MSG, DeprecationWarning, stacklevel=2)
|
| 329 |
+
# Fall back to legacy orca image export path
|
| 330 |
+
from ._orca import to_image as to_image_orca
|
| 331 |
+
|
| 332 |
+
return to_image_orca(
|
| 333 |
+
fig,
|
| 334 |
+
format=format,
|
| 335 |
+
width=width,
|
| 336 |
+
height=height,
|
| 337 |
+
scale=scale,
|
| 338 |
+
validate=validate,
|
| 339 |
+
)
|
| 340 |
+
elif engine != "kaleido":
|
| 341 |
+
raise ValueError(f"Invalid image export engine specified: {repr(engine)}")
|
| 342 |
+
|
| 343 |
+
# Raise informative error message if Kaleido is not installed
|
| 344 |
+
if not kaleido_available():
|
| 345 |
+
raise ValueError(
|
| 346 |
+
"""
|
| 347 |
+
Image export using the "kaleido" engine requires the Kaleido package,
|
| 348 |
+
which can be installed using pip:
|
| 349 |
+
|
| 350 |
+
$ pip install --upgrade kaleido
|
| 351 |
+
"""
|
| 352 |
+
)
|
| 353 |
+
|
| 354 |
+
# Convert figure to dict (and validate if requested)
|
| 355 |
+
fig_dict = validate_coerce_fig_to_dict(fig, validate)
|
| 356 |
+
|
| 357 |
+
# Request image bytes
|
| 358 |
+
if kaleido_major() > 0:
|
| 359 |
+
# Kaleido v1
|
| 360 |
+
# Check if trying to export to EPS format, which is not supported in Kaleido v1
|
| 361 |
+
if format == "eps":
|
| 362 |
+
raise ValueError(
|
| 363 |
+
f"""
|
| 364 |
+
EPS export is not supported by Kaleido v1. Please use SVG or PDF instead.
|
| 365 |
+
You can also downgrade to Kaleido v0, but support for Kaleido v0 will be removed after {ENGINE_SUPPORT_TIMELINE}.
|
| 366 |
+
To downgrade to Kaleido v0, run:
|
| 367 |
+
$ pip install 'kaleido<1.0.0'
|
| 368 |
+
"""
|
| 369 |
+
)
|
| 370 |
+
from kaleido.errors import ChromeNotFoundError
|
| 371 |
+
|
| 372 |
+
try:
|
| 373 |
+
kopts = {}
|
| 374 |
+
if defaults.plotlyjs:
|
| 375 |
+
kopts["plotlyjs"] = defaults.plotlyjs
|
| 376 |
+
if defaults.mathjax:
|
| 377 |
+
kopts["mathjax"] = defaults.mathjax
|
| 378 |
+
|
| 379 |
+
width = (
|
| 380 |
+
width
|
| 381 |
+
or fig_dict.get("layout", {}).get("width")
|
| 382 |
+
or fig_dict.get("layout", {})
|
| 383 |
+
.get("template", {})
|
| 384 |
+
.get("layout", {})
|
| 385 |
+
.get("width")
|
| 386 |
+
or defaults.default_width
|
| 387 |
+
)
|
| 388 |
+
height = (
|
| 389 |
+
height
|
| 390 |
+
or fig_dict.get("layout", {}).get("height")
|
| 391 |
+
or fig_dict.get("layout", {})
|
| 392 |
+
.get("template", {})
|
| 393 |
+
.get("layout", {})
|
| 394 |
+
.get("height")
|
| 395 |
+
or defaults.default_height
|
| 396 |
+
)
|
| 397 |
+
|
| 398 |
+
img_bytes = kaleido.calc_fig_sync(
|
| 399 |
+
fig_dict,
|
| 400 |
+
opts=dict(
|
| 401 |
+
format=format or defaults.default_format,
|
| 402 |
+
width=width,
|
| 403 |
+
height=height,
|
| 404 |
+
scale=scale or defaults.default_scale,
|
| 405 |
+
),
|
| 406 |
+
topojson=defaults.topojson,
|
| 407 |
+
kopts=kopts,
|
| 408 |
+
)
|
| 409 |
+
except ChromeNotFoundError:
|
| 410 |
+
raise RuntimeError(PLOTLY_GET_CHROME_ERROR_MSG)
|
| 411 |
+
|
| 412 |
+
else:
|
| 413 |
+
# Kaleido v0
|
| 414 |
+
if ENABLE_KALEIDO_V0_DEPRECATION_WARNINGS:
|
| 415 |
+
warnings.warn(KALEIDO_DEPRECATION_MSG, DeprecationWarning, stacklevel=2)
|
| 416 |
+
img_bytes = scope.transform(
|
| 417 |
+
fig_dict, format=format, width=width, height=height, scale=scale
|
| 418 |
+
)
|
| 419 |
+
|
| 420 |
+
return img_bytes
|
| 421 |
+
|
| 422 |
+
|
| 423 |
+
def write_image(
|
| 424 |
+
fig: Union[dict, plotly.graph_objects.Figure],
|
| 425 |
+
file: Union[str, Path],
|
| 426 |
+
format: Union[str, None] = None,
|
| 427 |
+
scale: Union[int, float, None] = None,
|
| 428 |
+
width: Union[int, None] = None,
|
| 429 |
+
height: Union[int, None] = None,
|
| 430 |
+
validate: bool = True,
|
| 431 |
+
# Deprecated
|
| 432 |
+
engine: Union[str, None] = "auto",
|
| 433 |
+
):
|
| 434 |
+
"""
|
| 435 |
+
Convert a figure to a static image and write it to a file or writeable
|
| 436 |
+
object
|
| 437 |
+
|
| 438 |
+
Parameters
|
| 439 |
+
----------
|
| 440 |
+
fig:
|
| 441 |
+
Figure object or dict representing a figure
|
| 442 |
+
|
| 443 |
+
file: str or writeable
|
| 444 |
+
A string representing a local file path or a writeable object
|
| 445 |
+
(e.g. a pathlib.Path object or an open file descriptor)
|
| 446 |
+
|
| 447 |
+
format: str or None
|
| 448 |
+
The desired image format. One of
|
| 449 |
+
- 'png'
|
| 450 |
+
- 'jpg' or 'jpeg'
|
| 451 |
+
- 'webp'
|
| 452 |
+
- 'svg'
|
| 453 |
+
- 'pdf'
|
| 454 |
+
- 'eps' (deprecated) (Requires the poppler library to be installed and on the PATH)
|
| 455 |
+
|
| 456 |
+
If not specified and `file` is a string then this will default to the
|
| 457 |
+
file extension. If not specified and `file` is not a string then this
|
| 458 |
+
will default to:
|
| 459 |
+
- `plotly.io.defaults.default_format` if engine is "kaleido"
|
| 460 |
+
- `plotly.io.orca.config.default_format` if engine is "orca" (deprecated)
|
| 461 |
+
|
| 462 |
+
width: int or None
|
| 463 |
+
The width of the exported image in layout pixels. If the `scale`
|
| 464 |
+
property is 1.0, this will also be the width of the exported image
|
| 465 |
+
in physical pixels.
|
| 466 |
+
|
| 467 |
+
If not specified, will default to:
|
| 468 |
+
- `plotly.io.defaults.default_width` if engine is "kaleido"
|
| 469 |
+
- `plotly.io.orca.config.default_width` if engine is "orca" (deprecated)
|
| 470 |
+
|
| 471 |
+
height: int or None
|
| 472 |
+
The height of the exported image in layout pixels. If the `scale`
|
| 473 |
+
property is 1.0, this will also be the height of the exported image
|
| 474 |
+
in physical pixels.
|
| 475 |
+
|
| 476 |
+
If not specified, will default to:
|
| 477 |
+
- `plotly.io.defaults.default_height` if engine is "kaleido"
|
| 478 |
+
- `plotly.io.orca.config.default_height` if engine is "orca" (deprecated)
|
| 479 |
+
|
| 480 |
+
scale: int or float or None
|
| 481 |
+
The scale factor to use when exporting the figure. A scale factor
|
| 482 |
+
larger than 1.0 will increase the image resolution with respect
|
| 483 |
+
to the figure's layout pixel dimensions. Whereas as scale factor of
|
| 484 |
+
less than 1.0 will decrease the image resolution.
|
| 485 |
+
|
| 486 |
+
If not specified, will default to:
|
| 487 |
+
- `plotly.io.defaults.default_scale` if engine is "kaleido"
|
| 488 |
+
- `plotly.io.orca.config.default_scale` if engine is "orca" (deprecated)
|
| 489 |
+
|
| 490 |
+
validate: bool
|
| 491 |
+
True if the figure should be validated before being converted to
|
| 492 |
+
an image, False otherwise.
|
| 493 |
+
|
| 494 |
+
engine (deprecated): str
|
| 495 |
+
Image export engine to use. This parameter is deprecated and Orca engine support will be
|
| 496 |
+
dropped in the next major Plotly version. Until then, the following values are supported:
|
| 497 |
+
- "kaleido": Use Kaleido for image export
|
| 498 |
+
- "orca": Use Orca for image export
|
| 499 |
+
- "auto" (default): Use Kaleido if installed, otherwise use Orca
|
| 500 |
+
|
| 501 |
+
Returns
|
| 502 |
+
-------
|
| 503 |
+
None
|
| 504 |
+
"""
|
| 505 |
+
# Show Kaleido deprecation warning if needed
|
| 506 |
+
if ENABLE_KALEIDO_V0_DEPRECATION_WARNINGS:
|
| 507 |
+
if (
|
| 508 |
+
engine in {None, "auto", "kaleido"}
|
| 509 |
+
and kaleido_available()
|
| 510 |
+
and kaleido_major() < 1
|
| 511 |
+
):
|
| 512 |
+
warnings.warn(KALEIDO_DEPRECATION_MSG, DeprecationWarning, stacklevel=2)
|
| 513 |
+
if engine == "orca":
|
| 514 |
+
warnings.warn(ORCA_DEPRECATION_MSG, DeprecationWarning, stacklevel=2)
|
| 515 |
+
if engine not in {None, "auto"}:
|
| 516 |
+
warnings.warn(
|
| 517 |
+
ENGINE_PARAM_DEPRECATION_MSG, DeprecationWarning, stacklevel=2
|
| 518 |
+
)
|
| 519 |
+
|
| 520 |
+
# Try to cast `file` as a pathlib object `path`.
|
| 521 |
+
path = as_path_object(file)
|
| 522 |
+
|
| 523 |
+
# Infer image format if not specified
|
| 524 |
+
format = infer_format(path, format)
|
| 525 |
+
|
| 526 |
+
# Request image
|
| 527 |
+
# Do this first so we don't create a file if image conversion fails
|
| 528 |
+
img_data = to_image(
|
| 529 |
+
fig,
|
| 530 |
+
format=format,
|
| 531 |
+
scale=scale,
|
| 532 |
+
width=width,
|
| 533 |
+
height=height,
|
| 534 |
+
validate=validate,
|
| 535 |
+
engine=engine,
|
| 536 |
+
)
|
| 537 |
+
|
| 538 |
+
# Open file
|
| 539 |
+
if path is None:
|
| 540 |
+
# We previously failed to make sense of `file` as a pathlib object.
|
| 541 |
+
# Attempt to write to `file` as an open file descriptor.
|
| 542 |
+
try:
|
| 543 |
+
file.write(img_data)
|
| 544 |
+
return
|
| 545 |
+
except AttributeError:
|
| 546 |
+
pass
|
| 547 |
+
raise ValueError(
|
| 548 |
+
f"""
|
| 549 |
+
The 'file' argument '{file}' is not a string, pathlib.Path object, or file descriptor.
|
| 550 |
+
"""
|
| 551 |
+
)
|
| 552 |
+
else:
|
| 553 |
+
# We previously succeeded in interpreting `file` as a pathlib object.
|
| 554 |
+
# Now we can use `write_bytes()`.
|
| 555 |
+
path.write_bytes(img_data)
|
| 556 |
+
|
| 557 |
+
|
| 558 |
+
def write_images(
|
| 559 |
+
fig: Union[
|
| 560 |
+
List[Union[dict, plotly.graph_objects.Figure]],
|
| 561 |
+
Union[dict, plotly.graph_objects.Figure],
|
| 562 |
+
],
|
| 563 |
+
file: Union[List[Union[str, Path]], Union[str, Path]],
|
| 564 |
+
format: Union[List[Union[str, None]], Union[str, None]] = None,
|
| 565 |
+
scale: Union[List[Union[int, float, None]], Union[int, float, None]] = None,
|
| 566 |
+
width: Union[List[Union[int, None]], Union[int, None]] = None,
|
| 567 |
+
height: Union[List[Union[int, None]], Union[int, None]] = None,
|
| 568 |
+
validate: Union[List[bool], bool] = True,
|
| 569 |
+
) -> None:
|
| 570 |
+
"""
|
| 571 |
+
Write multiple images to files or writeable objects. This is much faster than
|
| 572 |
+
calling write_image() multiple times. This function can only be used with the Kaleido
|
| 573 |
+
engine, v1.0.0 or greater.
|
| 574 |
+
|
| 575 |
+
This function accepts the same arguments as write_image() (minus the `engine` argument),
|
| 576 |
+
except that any of the arguments may be either a single value or an iterable of values.
|
| 577 |
+
If multiple arguments are iterable, they must all have the same length.
|
| 578 |
+
|
| 579 |
+
Parameters
|
| 580 |
+
----------
|
| 581 |
+
fig:
|
| 582 |
+
List of figure objects or dicts representing a figure.
|
| 583 |
+
Also accepts a single figure or dict representing a figure.
|
| 584 |
+
|
| 585 |
+
file: str, pathlib.Path, or list of (str or pathlib.Path)
|
| 586 |
+
List of str or pathlib.Path objects representing local file paths to write to.
|
| 587 |
+
Can also be a single str or pathlib.Path object if fig argument is
|
| 588 |
+
a single figure or dict representing a figure.
|
| 589 |
+
|
| 590 |
+
format: str, None, or list of (str or None)
|
| 591 |
+
The image format to use for exported images.
|
| 592 |
+
Supported formats are:
|
| 593 |
+
- 'png'
|
| 594 |
+
- 'jpg' or 'jpeg'
|
| 595 |
+
- 'webp'
|
| 596 |
+
- 'svg'
|
| 597 |
+
- 'pdf'
|
| 598 |
+
|
| 599 |
+
Use a list to specify formats for each figure or dict in the list
|
| 600 |
+
provided to the `fig` argument.
|
| 601 |
+
Specify format as a `str` to apply the same format to all exported images.
|
| 602 |
+
If not specified, and the corresponding `file` argument has a file extension, then `format` will default to the
|
| 603 |
+
file extension. Otherwise, will default to `plotly.io.defaults.default_format`.
|
| 604 |
+
|
| 605 |
+
width: int, None, or list of (int or None)
|
| 606 |
+
The width of the exported image in layout pixels. If the `scale`
|
| 607 |
+
property is 1.0, this will also be the width of the exported image
|
| 608 |
+
in physical pixels.
|
| 609 |
+
|
| 610 |
+
Use a list to specify widths for each figure or dict in the list
|
| 611 |
+
provided to the `fig` argument.
|
| 612 |
+
Specify width as an `int` to apply the same width to all exported images.
|
| 613 |
+
If not specified, will default to `plotly.io.defaults.default_width`.
|
| 614 |
+
|
| 615 |
+
height: int, None, or list of (int or None)
|
| 616 |
+
The height of the exported image in layout pixels. If the `scale`
|
| 617 |
+
property is 1.0, this will also be the height of the exported image
|
| 618 |
+
in physical pixels.
|
| 619 |
+
|
| 620 |
+
Use a list to specify heights for each figure or dict in the list
|
| 621 |
+
provided to the `fig` argument.
|
| 622 |
+
Specify height as an `int` to apply the same height to all exported images.
|
| 623 |
+
If not specified, will default to `plotly.io.defaults.default_height`.
|
| 624 |
+
|
| 625 |
+
scale: int, float, None, or list of (int, float, or None)
|
| 626 |
+
The scale factor to use when exporting the figure. A scale factor
|
| 627 |
+
larger than 1.0 will increase the image resolution with respect
|
| 628 |
+
to the figure's layout pixel dimensions. Whereas as scale factor of
|
| 629 |
+
less than 1.0 will decrease the image resolution.
|
| 630 |
+
|
| 631 |
+
Use a list to specify scale for each figure or dict in the list
|
| 632 |
+
provided to the `fig` argument.
|
| 633 |
+
Specify scale as an `int` or `float` to apply the same scale to all exported images.
|
| 634 |
+
If not specified, will default to `plotly.io.defaults.default_scale`.
|
| 635 |
+
|
| 636 |
+
validate: bool or list of bool
|
| 637 |
+
True if the figure should be validated before being converted to
|
| 638 |
+
an image, False otherwise.
|
| 639 |
+
|
| 640 |
+
Use a list to specify validation setting for each figure in the list
|
| 641 |
+
provided to the `fig` argument.
|
| 642 |
+
Specify validate as a boolean to apply the same validation setting to all figures.
|
| 643 |
+
|
| 644 |
+
Returns
|
| 645 |
+
-------
|
| 646 |
+
None
|
| 647 |
+
"""
|
| 648 |
+
|
| 649 |
+
# Raise informative error message if Kaleido v1 is not installed
|
| 650 |
+
if not kaleido_available():
|
| 651 |
+
raise ValueError(
|
| 652 |
+
"""
|
| 653 |
+
The `write_images()` function requires the Kaleido package,
|
| 654 |
+
which can be installed using pip:
|
| 655 |
+
|
| 656 |
+
$ pip install --upgrade kaleido
|
| 657 |
+
"""
|
| 658 |
+
)
|
| 659 |
+
elif kaleido_major() < 1:
|
| 660 |
+
raise ValueError(
|
| 661 |
+
f"""
|
| 662 |
+
You have Kaleido version {Version(importlib_metadata.version("kaleido"))} installed.
|
| 663 |
+
The `write_images()` function requires the Kaleido package version 1.0.0 or greater,
|
| 664 |
+
which can be installed using pip:
|
| 665 |
+
|
| 666 |
+
$ pip install 'kaleido>=1.0.0'
|
| 667 |
+
"""
|
| 668 |
+
)
|
| 669 |
+
|
| 670 |
+
# Broadcast arguments into correct format for passing to Kaleido
|
| 671 |
+
arg_dicts = broadcast_args_to_dicts(
|
| 672 |
+
fig=fig,
|
| 673 |
+
file=file,
|
| 674 |
+
format=format,
|
| 675 |
+
scale=scale,
|
| 676 |
+
width=width,
|
| 677 |
+
height=height,
|
| 678 |
+
validate=validate,
|
| 679 |
+
)
|
| 680 |
+
|
| 681 |
+
# For each dict:
|
| 682 |
+
# - convert figures to dicts (and validate if requested)
|
| 683 |
+
# - try to cast `file` as a Path object
|
| 684 |
+
for d in arg_dicts:
|
| 685 |
+
d["fig"] = validate_coerce_fig_to_dict(d["fig"], d["validate"])
|
| 686 |
+
d["file"] = as_path_object(d["file"])
|
| 687 |
+
|
| 688 |
+
# Reshape arg_dicts into correct format for passing to Kaleido
|
| 689 |
+
# We call infer_format() here rather than above so that the `file` argument
|
| 690 |
+
# has already been cast to a Path object.
|
| 691 |
+
# Also insert defaults for any missing arguments as needed
|
| 692 |
+
kaleido_specs = [
|
| 693 |
+
dict(
|
| 694 |
+
fig=d["fig"],
|
| 695 |
+
path=d["file"],
|
| 696 |
+
opts=dict(
|
| 697 |
+
format=infer_format(d["file"], d["format"]) or defaults.default_format,
|
| 698 |
+
width=d["width"] or defaults.default_width,
|
| 699 |
+
height=d["height"] or defaults.default_height,
|
| 700 |
+
scale=d["scale"] or defaults.default_scale,
|
| 701 |
+
),
|
| 702 |
+
topojson=defaults.topojson,
|
| 703 |
+
)
|
| 704 |
+
for d in arg_dicts
|
| 705 |
+
]
|
| 706 |
+
|
| 707 |
+
from kaleido.errors import ChromeNotFoundError
|
| 708 |
+
|
| 709 |
+
try:
|
| 710 |
+
kopts = {}
|
| 711 |
+
if defaults.plotlyjs:
|
| 712 |
+
kopts["plotlyjs"] = defaults.plotlyjs
|
| 713 |
+
if defaults.mathjax:
|
| 714 |
+
kopts["mathjax"] = defaults.mathjax
|
| 715 |
+
kaleido.write_fig_from_object_sync(
|
| 716 |
+
kaleido_specs,
|
| 717 |
+
kopts=kopts,
|
| 718 |
+
)
|
| 719 |
+
except ChromeNotFoundError:
|
| 720 |
+
raise RuntimeError(PLOTLY_GET_CHROME_ERROR_MSG)
|
| 721 |
+
|
| 722 |
+
|
| 723 |
+
def full_figure_for_development(
|
| 724 |
+
fig: Union[dict, plotly.graph_objects.Figure],
|
| 725 |
+
warn: bool = True,
|
| 726 |
+
as_dict: bool = False,
|
| 727 |
+
) -> Union[plotly.graph_objects.Figure, dict]:
|
| 728 |
+
"""
|
| 729 |
+
Compute default values for all attributes not specified in the input figure and
|
| 730 |
+
returns the output as a "full" figure. This function calls Plotly.js via Kaleido
|
| 731 |
+
to populate unspecified attributes. This function is intended for interactive use
|
| 732 |
+
during development to learn more about how Plotly.js computes default values and is
|
| 733 |
+
not generally necessary or recommended for production use.
|
| 734 |
+
|
| 735 |
+
Parameters
|
| 736 |
+
----------
|
| 737 |
+
fig:
|
| 738 |
+
Figure object or dict representing a figure
|
| 739 |
+
|
| 740 |
+
warn: bool
|
| 741 |
+
If False, suppress warnings about not using this in production.
|
| 742 |
+
|
| 743 |
+
as_dict: bool
|
| 744 |
+
If True, output is a dict with some keys that go.Figure can't parse.
|
| 745 |
+
If False, output is a go.Figure with unparseable keys skipped.
|
| 746 |
+
|
| 747 |
+
Returns
|
| 748 |
+
-------
|
| 749 |
+
plotly.graph_objects.Figure or dict
|
| 750 |
+
The full figure
|
| 751 |
+
"""
|
| 752 |
+
|
| 753 |
+
# Raise informative error message if Kaleido is not installed
|
| 754 |
+
if not kaleido_available():
|
| 755 |
+
raise ValueError(
|
| 756 |
+
"""
|
| 757 |
+
Full figure generation requires the Kaleido package,
|
| 758 |
+
which can be installed using pip:
|
| 759 |
+
|
| 760 |
+
$ pip install --upgrade kaleido
|
| 761 |
+
"""
|
| 762 |
+
)
|
| 763 |
+
|
| 764 |
+
if warn:
|
| 765 |
+
warnings.warn(
|
| 766 |
+
"full_figure_for_development is not recommended or necessary for "
|
| 767 |
+
"production use in most circumstances. \n"
|
| 768 |
+
"To suppress this warning, set warn=False"
|
| 769 |
+
)
|
| 770 |
+
|
| 771 |
+
if kaleido_available() and kaleido_major() > 0:
|
| 772 |
+
# Kaleido v1
|
| 773 |
+
bytes = kaleido.calc_fig_sync(
|
| 774 |
+
fig,
|
| 775 |
+
opts=dict(format="json"),
|
| 776 |
+
)
|
| 777 |
+
fig = json.loads(bytes.decode("utf-8"))
|
| 778 |
+
else:
|
| 779 |
+
# Kaleido v0
|
| 780 |
+
if ENABLE_KALEIDO_V0_DEPRECATION_WARNINGS:
|
| 781 |
+
warnings.warn(
|
| 782 |
+
f"Support for Kaleido versions less than 1.0.0 is deprecated and will be removed after {ENGINE_SUPPORT_TIMELINE}. "
|
| 783 |
+
+ "Please upgrade Kaleido to version 1.0.0 or greater (`pip install 'kaleido>=1.0.0'`).",
|
| 784 |
+
DeprecationWarning,
|
| 785 |
+
)
|
| 786 |
+
fig = json.loads(scope.transform(fig, format="json").decode("utf-8"))
|
| 787 |
+
|
| 788 |
+
if as_dict:
|
| 789 |
+
return fig
|
| 790 |
+
else:
|
| 791 |
+
import plotly.graph_objects as go
|
| 792 |
+
|
| 793 |
+
return go.Figure(fig, skip_invalid=True)
|
| 794 |
+
|
| 795 |
+
|
| 796 |
+
def plotly_get_chrome() -> None:
|
| 797 |
+
"""
|
| 798 |
+
Install Google Chrome for Kaleido (Required for Plotly image export).
|
| 799 |
+
This function is a command-line wrapper for `plotly.io.get_chrome()`.
|
| 800 |
+
|
| 801 |
+
When running from the command line, use the command `plotly_get_chrome`;
|
| 802 |
+
when calling from Python code, use `plotly.io.get_chrome()`.
|
| 803 |
+
"""
|
| 804 |
+
|
| 805 |
+
usage = """
|
| 806 |
+
Usage: plotly_get_chrome [-y] [--path PATH]
|
| 807 |
+
|
| 808 |
+
Installs Google Chrome for Plotly image export.
|
| 809 |
+
|
| 810 |
+
Options:
|
| 811 |
+
-y Skip confirmation prompt
|
| 812 |
+
--path PATH Specify the path to install Chrome. Must be a path to an existing directory.
|
| 813 |
+
--help Show this message and exit.
|
| 814 |
+
"""
|
| 815 |
+
|
| 816 |
+
if not kaleido_available() or kaleido_major() < 1:
|
| 817 |
+
raise ValueError(
|
| 818 |
+
"""
|
| 819 |
+
This command requires Kaleido v1.0.0 or greater.
|
| 820 |
+
Install it using `pip install 'kaleido>=1.0.0'` or `pip install 'plotly[kaleido]'`."
|
| 821 |
+
"""
|
| 822 |
+
)
|
| 823 |
+
|
| 824 |
+
# Handle command line arguments
|
| 825 |
+
import sys
|
| 826 |
+
|
| 827 |
+
cli_args = sys.argv
|
| 828 |
+
|
| 829 |
+
# Handle "-y" flag
|
| 830 |
+
cli_yes = "-y" in cli_args
|
| 831 |
+
if cli_yes:
|
| 832 |
+
cli_args.remove("-y")
|
| 833 |
+
|
| 834 |
+
# Handle "--path" flag
|
| 835 |
+
chrome_install_path = None
|
| 836 |
+
if "--path" in cli_args:
|
| 837 |
+
path_index = cli_args.index("--path") + 1
|
| 838 |
+
if path_index < len(cli_args):
|
| 839 |
+
chrome_install_path = cli_args[path_index]
|
| 840 |
+
cli_args.remove("--path")
|
| 841 |
+
cli_args.remove(chrome_install_path)
|
| 842 |
+
chrome_install_path = Path(chrome_install_path)
|
| 843 |
+
|
| 844 |
+
# If any arguments remain, command syntax was incorrect -- print usage and exit
|
| 845 |
+
if len(cli_args) > 1:
|
| 846 |
+
print(usage)
|
| 847 |
+
sys.exit(1)
|
| 848 |
+
|
| 849 |
+
if not cli_yes:
|
| 850 |
+
print(
|
| 851 |
+
f"""
|
| 852 |
+
Plotly will install a copy of Google Chrome to be used for generating static images of plots.
|
| 853 |
+
Chrome will be installed at: {chrome_install_path}"""
|
| 854 |
+
)
|
| 855 |
+
response = input("Do you want to proceed? [y/n] ")
|
| 856 |
+
if not response or response[0].lower() != "y":
|
| 857 |
+
print("Cancelled")
|
| 858 |
+
return
|
| 859 |
+
print("Installing Chrome for Plotly...")
|
| 860 |
+
exe_path = get_chrome(chrome_install_path)
|
| 861 |
+
print("Chrome installed successfully.")
|
| 862 |
+
print(f"The Chrome executable is now located at: {exe_path}")
|
| 863 |
+
|
| 864 |
+
|
| 865 |
+
def get_chrome(path: Union[str, Path, None] = None) -> Path:
|
| 866 |
+
"""
|
| 867 |
+
Get the path to the Chrome executable for Kaleido.
|
| 868 |
+
This function is used by the `plotly_get_chrome` command line utility.
|
| 869 |
+
|
| 870 |
+
Parameters
|
| 871 |
+
----------
|
| 872 |
+
path: str or Path or None
|
| 873 |
+
The path to the directory where Chrome should be installed.
|
| 874 |
+
If None, the default download path will be used.
|
| 875 |
+
"""
|
| 876 |
+
if not kaleido_available() or kaleido_major() < 1:
|
| 877 |
+
raise ValueError(
|
| 878 |
+
"""
|
| 879 |
+
This command requires Kaleido v1.0.0 or greater.
|
| 880 |
+
Install it using `pip install 'kaleido>=1.0.0'` or `pip install 'plotly[kaleido]'`."
|
| 881 |
+
"""
|
| 882 |
+
)
|
| 883 |
+
|
| 884 |
+
# Use default download path if no path was specified
|
| 885 |
+
if path:
|
| 886 |
+
user_specified_path = True
|
| 887 |
+
chrome_install_path = Path(path) # Ensure it's a Path object
|
| 888 |
+
else:
|
| 889 |
+
user_specified_path = False
|
| 890 |
+
from choreographer.cli.defaults import default_download_path
|
| 891 |
+
|
| 892 |
+
chrome_install_path = default_download_path
|
| 893 |
+
|
| 894 |
+
# If install path was chosen by user, make sure there is an existing directory
|
| 895 |
+
# located at chrome_install_path; otherwise fail
|
| 896 |
+
if user_specified_path:
|
| 897 |
+
if not chrome_install_path.exists():
|
| 898 |
+
raise ValueError(
|
| 899 |
+
f"""
|
| 900 |
+
The specified install path '{chrome_install_path}' does not exist.
|
| 901 |
+
Please specify a path to an existing directory using the --path argument,
|
| 902 |
+
or omit the --path argument to use the default download path.
|
| 903 |
+
"""
|
| 904 |
+
)
|
| 905 |
+
# Make sure the path is a directory
|
| 906 |
+
if not chrome_install_path.is_dir():
|
| 907 |
+
raise ValueError(
|
| 908 |
+
f"""
|
| 909 |
+
The specified install path '{chrome_install_path}' already exists but is not a directory.
|
| 910 |
+
Please specify a path to an existing directory using the --path argument,
|
| 911 |
+
or omit the --path argument to use the default download path.
|
| 912 |
+
"""
|
| 913 |
+
)
|
| 914 |
+
|
| 915 |
+
return kaleido.get_chrome_sync(path=chrome_install_path)
|
| 916 |
+
|
| 917 |
+
|
| 918 |
+
__all__ = ["to_image", "write_image", "scope", "full_figure_for_development"]
|
.venv_haddock/lib/python3.12/site-packages/plotly/io/_orca.py
ADDED
|
@@ -0,0 +1,1670 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import atexit
|
| 2 |
+
import functools
|
| 3 |
+
import json
|
| 4 |
+
import os
|
| 5 |
+
import random
|
| 6 |
+
import socket
|
| 7 |
+
import subprocess
|
| 8 |
+
import sys
|
| 9 |
+
import threading
|
| 10 |
+
import time
|
| 11 |
+
import warnings
|
| 12 |
+
from contextlib import contextmanager
|
| 13 |
+
from copy import copy
|
| 14 |
+
from pathlib import Path
|
| 15 |
+
from shutil import which
|
| 16 |
+
|
| 17 |
+
import plotly
|
| 18 |
+
from plotly.files import PLOTLY_DIR, ensure_writable_plotly_dir
|
| 19 |
+
from plotly.io._utils import validate_coerce_fig_to_dict
|
| 20 |
+
from plotly.optional_imports import get_module
|
| 21 |
+
|
| 22 |
+
psutil = get_module("psutil")
|
| 23 |
+
|
| 24 |
+
# Valid image format constants
|
| 25 |
+
# ----------------------------
|
| 26 |
+
valid_formats = ("png", "jpeg", "webp", "svg", "pdf", "eps")
|
| 27 |
+
format_conversions = {fmt: fmt for fmt in valid_formats}
|
| 28 |
+
format_conversions.update({"jpg": "jpeg"})
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
# Utility functions
|
| 32 |
+
# -----------------
|
| 33 |
+
def raise_format_value_error(val):
|
| 34 |
+
raise ValueError(
|
| 35 |
+
"""
|
| 36 |
+
Invalid value of type {typ} receive as an image format specification.
|
| 37 |
+
Received value: {v}
|
| 38 |
+
|
| 39 |
+
An image format must be specified as one of the following string values:
|
| 40 |
+
{valid_formats}""".format(
|
| 41 |
+
typ=type(val), v=val, valid_formats=sorted(format_conversions.keys())
|
| 42 |
+
)
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
def validate_coerce_format(fmt):
|
| 47 |
+
"""
|
| 48 |
+
Validate / coerce a user specified image format, and raise an informative
|
| 49 |
+
exception if format is invalid.
|
| 50 |
+
|
| 51 |
+
Parameters
|
| 52 |
+
----------
|
| 53 |
+
fmt
|
| 54 |
+
A value that may or may not be a valid image format string.
|
| 55 |
+
|
| 56 |
+
Returns
|
| 57 |
+
-------
|
| 58 |
+
str or None
|
| 59 |
+
A valid image format string as supported by orca. This may not
|
| 60 |
+
be identical to the input image designation. For example,
|
| 61 |
+
the resulting string will always be lower case and 'jpg' is
|
| 62 |
+
converted to 'jpeg'.
|
| 63 |
+
|
| 64 |
+
If the input format value is None, then no exception is raised and
|
| 65 |
+
None is returned.
|
| 66 |
+
|
| 67 |
+
Raises
|
| 68 |
+
------
|
| 69 |
+
ValueError
|
| 70 |
+
if the input `fmt` cannot be interpreted as a valid image format.
|
| 71 |
+
"""
|
| 72 |
+
|
| 73 |
+
# Let None pass through
|
| 74 |
+
if fmt is None:
|
| 75 |
+
return None
|
| 76 |
+
|
| 77 |
+
# Check format type
|
| 78 |
+
if not isinstance(fmt, str) or not fmt:
|
| 79 |
+
raise_format_value_error(fmt)
|
| 80 |
+
|
| 81 |
+
# Make lower case
|
| 82 |
+
fmt = fmt.lower()
|
| 83 |
+
|
| 84 |
+
# Remove leading period, if any.
|
| 85 |
+
# For example '.png' is accepted and converted to 'png'
|
| 86 |
+
if fmt[0] == ".":
|
| 87 |
+
fmt = fmt[1:]
|
| 88 |
+
|
| 89 |
+
# Check string value
|
| 90 |
+
if fmt not in format_conversions:
|
| 91 |
+
raise_format_value_error(fmt)
|
| 92 |
+
|
| 93 |
+
# Return converted string specification
|
| 94 |
+
return format_conversions[fmt]
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
def find_open_port():
|
| 98 |
+
"""
|
| 99 |
+
Use the socket module to find an open port.
|
| 100 |
+
|
| 101 |
+
Returns
|
| 102 |
+
-------
|
| 103 |
+
int
|
| 104 |
+
An open port
|
| 105 |
+
"""
|
| 106 |
+
s = socket.socket()
|
| 107 |
+
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
| 108 |
+
s.bind(("", 0))
|
| 109 |
+
_, port = s.getsockname()
|
| 110 |
+
s.close()
|
| 111 |
+
|
| 112 |
+
return port
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
def retry(min_wait=5, max_wait=10, max_delay=60000):
|
| 116 |
+
def decorator(func):
|
| 117 |
+
@functools.wraps(func)
|
| 118 |
+
def wrapper(*args, **kwargs):
|
| 119 |
+
start_time = time.time()
|
| 120 |
+
|
| 121 |
+
while True:
|
| 122 |
+
try:
|
| 123 |
+
return func(*args, **kwargs)
|
| 124 |
+
except Exception as e:
|
| 125 |
+
elapsed_time = time.time() - start_time
|
| 126 |
+
if elapsed_time * 1000 >= max_delay:
|
| 127 |
+
raise TimeoutError(
|
| 128 |
+
f"Retry limit of {max_delay} milliseconds reached."
|
| 129 |
+
) from e
|
| 130 |
+
|
| 131 |
+
wait_time = random.uniform(min_wait, max_wait)
|
| 132 |
+
print(f"Retrying in {wait_time:.2f} seconds due to {e}...")
|
| 133 |
+
time.sleep(wait_time)
|
| 134 |
+
|
| 135 |
+
return wrapper
|
| 136 |
+
|
| 137 |
+
return decorator
|
| 138 |
+
|
| 139 |
+
|
| 140 |
+
# Orca configuration class
|
| 141 |
+
# ------------------------
|
| 142 |
+
class OrcaConfig(object):
|
| 143 |
+
"""
|
| 144 |
+
Singleton object containing the current user defined configuration
|
| 145 |
+
properties for orca.
|
| 146 |
+
|
| 147 |
+
These parameters may optionally be saved to the user's ~/.plotly
|
| 148 |
+
directory using the `save` method, in which case they are automatically
|
| 149 |
+
restored in future sessions.
|
| 150 |
+
"""
|
| 151 |
+
|
| 152 |
+
def __init__(self):
|
| 153 |
+
# Initialize properties dict
|
| 154 |
+
self._props = {}
|
| 155 |
+
|
| 156 |
+
# Compute absolute path to the 'plotly/package_data/' directory
|
| 157 |
+
root_dir = os.path.dirname(os.path.abspath(plotly.__file__))
|
| 158 |
+
self.package_dir = os.path.join(root_dir, "package_data")
|
| 159 |
+
|
| 160 |
+
# Load pre-existing configuration
|
| 161 |
+
self.reload(warn=False)
|
| 162 |
+
|
| 163 |
+
# Compute constants
|
| 164 |
+
plotlyjs = os.path.join(self.package_dir, "plotly.min.js")
|
| 165 |
+
self._constants = {
|
| 166 |
+
"plotlyjs": plotlyjs,
|
| 167 |
+
"config_file": os.path.join(PLOTLY_DIR, ".orca"),
|
| 168 |
+
}
|
| 169 |
+
|
| 170 |
+
def restore_defaults(self, reset_server=True):
|
| 171 |
+
"""
|
| 172 |
+
Reset all orca configuration properties to their default values
|
| 173 |
+
"""
|
| 174 |
+
self._props = {}
|
| 175 |
+
|
| 176 |
+
if reset_server:
|
| 177 |
+
# Server must restart before setting is active
|
| 178 |
+
reset_status()
|
| 179 |
+
|
| 180 |
+
def update(self, d={}, **kwargs):
|
| 181 |
+
"""
|
| 182 |
+
Update one or more properties from a dict or from input keyword
|
| 183 |
+
arguments.
|
| 184 |
+
|
| 185 |
+
Parameters
|
| 186 |
+
----------
|
| 187 |
+
d: dict
|
| 188 |
+
Dictionary from property names to new property values.
|
| 189 |
+
|
| 190 |
+
kwargs
|
| 191 |
+
Named argument value pairs where the name is a configuration
|
| 192 |
+
property name and the value is the new property value.
|
| 193 |
+
|
| 194 |
+
Returns
|
| 195 |
+
-------
|
| 196 |
+
None
|
| 197 |
+
|
| 198 |
+
Examples
|
| 199 |
+
--------
|
| 200 |
+
Update configuration properties using a dictionary
|
| 201 |
+
|
| 202 |
+
>>> import plotly.io as pio
|
| 203 |
+
>>> pio.orca.config.update({'timeout': 30, 'default_format': 'svg'})
|
| 204 |
+
|
| 205 |
+
Update configuration properties using keyword arguments
|
| 206 |
+
|
| 207 |
+
>>> pio.orca.config.update(timeout=30, default_format='svg'})
|
| 208 |
+
"""
|
| 209 |
+
# Combine d and kwargs
|
| 210 |
+
if not isinstance(d, dict):
|
| 211 |
+
raise ValueError(
|
| 212 |
+
"""
|
| 213 |
+
The first argument to update must be a dict, \
|
| 214 |
+
but received value of type {typ}l
|
| 215 |
+
Received value: {val}""".format(typ=type(d), val=d)
|
| 216 |
+
)
|
| 217 |
+
|
| 218 |
+
updates = copy(d)
|
| 219 |
+
updates.update(kwargs)
|
| 220 |
+
|
| 221 |
+
# Validate keys
|
| 222 |
+
for k in updates:
|
| 223 |
+
if k not in self._props:
|
| 224 |
+
raise ValueError("Invalid property name: {k}".format(k=k))
|
| 225 |
+
|
| 226 |
+
# Apply keys
|
| 227 |
+
for k, v in updates.items():
|
| 228 |
+
setattr(self, k, v)
|
| 229 |
+
|
| 230 |
+
def reload(self, warn=True):
|
| 231 |
+
"""
|
| 232 |
+
Reload orca settings from ~/.plotly/.orca, if any.
|
| 233 |
+
|
| 234 |
+
Note: Settings are loaded automatically when plotly is imported.
|
| 235 |
+
This method is only needed if the setting are changed by some outside
|
| 236 |
+
process (e.g. a text editor) during an interactive session.
|
| 237 |
+
|
| 238 |
+
Parameters
|
| 239 |
+
----------
|
| 240 |
+
warn: bool
|
| 241 |
+
If True, raise informative warnings if settings cannot be restored.
|
| 242 |
+
If False, do not raise warnings if setting cannot be restored.
|
| 243 |
+
|
| 244 |
+
Returns
|
| 245 |
+
-------
|
| 246 |
+
None
|
| 247 |
+
"""
|
| 248 |
+
if os.path.exists(self.config_file):
|
| 249 |
+
# ### Load file into a string ###
|
| 250 |
+
try:
|
| 251 |
+
with open(self.config_file, "r") as f:
|
| 252 |
+
orca_str = f.read()
|
| 253 |
+
except Exception:
|
| 254 |
+
if warn:
|
| 255 |
+
warnings.warn(
|
| 256 |
+
"""\
|
| 257 |
+
Unable to read orca configuration file at {path}""".format(path=self.config_file)
|
| 258 |
+
)
|
| 259 |
+
return
|
| 260 |
+
|
| 261 |
+
# ### Parse as JSON ###
|
| 262 |
+
try:
|
| 263 |
+
orca_props = json.loads(orca_str)
|
| 264 |
+
except ValueError:
|
| 265 |
+
if warn:
|
| 266 |
+
warnings.warn(
|
| 267 |
+
"""\
|
| 268 |
+
Orca configuration file at {path} is not valid JSON""".format(path=self.config_file)
|
| 269 |
+
)
|
| 270 |
+
return
|
| 271 |
+
|
| 272 |
+
# ### Update _props ###
|
| 273 |
+
for k, v in orca_props.items():
|
| 274 |
+
self._props[k] = v
|
| 275 |
+
|
| 276 |
+
elif warn:
|
| 277 |
+
warnings.warn(
|
| 278 |
+
"""\
|
| 279 |
+
Orca configuration file at {path} not found""".format(path=self.config_file)
|
| 280 |
+
)
|
| 281 |
+
|
| 282 |
+
def save(self):
|
| 283 |
+
"""
|
| 284 |
+
Attempt to save current settings to disk, so that they are
|
| 285 |
+
automatically restored for future sessions.
|
| 286 |
+
|
| 287 |
+
This operation requires write access to the path returned by
|
| 288 |
+
in the `config_file` property.
|
| 289 |
+
|
| 290 |
+
Returns
|
| 291 |
+
-------
|
| 292 |
+
None
|
| 293 |
+
"""
|
| 294 |
+
if ensure_writable_plotly_dir():
|
| 295 |
+
with open(self.config_file, "w") as f:
|
| 296 |
+
json.dump(self._props, f, indent=4)
|
| 297 |
+
else:
|
| 298 |
+
warnings.warn(
|
| 299 |
+
"""\
|
| 300 |
+
Failed to write orca configuration file at '{path}'""".format(path=self.config_file)
|
| 301 |
+
)
|
| 302 |
+
|
| 303 |
+
@property
|
| 304 |
+
def server_url(self):
|
| 305 |
+
"""
|
| 306 |
+
The server URL to use for an external orca server, or None if orca
|
| 307 |
+
should be managed locally
|
| 308 |
+
|
| 309 |
+
Overrides executable, port, timeout, mathjax, topojson,
|
| 310 |
+
and mapbox_access_token
|
| 311 |
+
|
| 312 |
+
Returns
|
| 313 |
+
-------
|
| 314 |
+
str or None
|
| 315 |
+
"""
|
| 316 |
+
return self._props.get("server_url", None)
|
| 317 |
+
|
| 318 |
+
@server_url.setter
|
| 319 |
+
def server_url(self, val):
|
| 320 |
+
if val is None:
|
| 321 |
+
self._props.pop("server_url", None)
|
| 322 |
+
return
|
| 323 |
+
if not isinstance(val, str):
|
| 324 |
+
raise ValueError(
|
| 325 |
+
"""
|
| 326 |
+
The server_url property must be a string, but received value of type {typ}.
|
| 327 |
+
Received value: {val}""".format(typ=type(val), val=val)
|
| 328 |
+
)
|
| 329 |
+
|
| 330 |
+
if not val.startswith("http://") and not val.startswith("https://"):
|
| 331 |
+
val = "http://" + val
|
| 332 |
+
|
| 333 |
+
shutdown_server()
|
| 334 |
+
self.executable = None
|
| 335 |
+
self.port = None
|
| 336 |
+
self.timeout = None
|
| 337 |
+
self.mathjax = None
|
| 338 |
+
self.topojson = None
|
| 339 |
+
self.mapbox_access_token = None
|
| 340 |
+
self._props["server_url"] = val
|
| 341 |
+
|
| 342 |
+
@property
|
| 343 |
+
def port(self):
|
| 344 |
+
"""
|
| 345 |
+
The specific port to use to communicate with the orca server, or
|
| 346 |
+
None if the port is to be chosen automatically.
|
| 347 |
+
|
| 348 |
+
If an orca server is active, the port in use is stored in the
|
| 349 |
+
plotly.io.orca.status.port property.
|
| 350 |
+
|
| 351 |
+
Returns
|
| 352 |
+
-------
|
| 353 |
+
int or None
|
| 354 |
+
"""
|
| 355 |
+
return self._props.get("port", None)
|
| 356 |
+
|
| 357 |
+
@port.setter
|
| 358 |
+
def port(self, val):
|
| 359 |
+
if val is None:
|
| 360 |
+
self._props.pop("port", None)
|
| 361 |
+
return
|
| 362 |
+
if not isinstance(val, int):
|
| 363 |
+
raise ValueError(
|
| 364 |
+
"""
|
| 365 |
+
The port property must be an integer, but received value of type {typ}.
|
| 366 |
+
Received value: {val}""".format(typ=type(val), val=val)
|
| 367 |
+
)
|
| 368 |
+
|
| 369 |
+
self._props["port"] = val
|
| 370 |
+
|
| 371 |
+
@property
|
| 372 |
+
def executable(self):
|
| 373 |
+
"""
|
| 374 |
+
The name or full path of the orca executable.
|
| 375 |
+
|
| 376 |
+
- If a name (e.g. 'orca'), then it should be the name of an orca
|
| 377 |
+
executable on the PATH. The directories on the PATH can be
|
| 378 |
+
displayed by running the following command:
|
| 379 |
+
|
| 380 |
+
>>> import os
|
| 381 |
+
>>> print(os.environ.get('PATH').replace(os.pathsep, os.linesep))
|
| 382 |
+
|
| 383 |
+
- If a full path (e.g. '/path/to/orca'), then
|
| 384 |
+
it should be the full path to an orca executable. In this case
|
| 385 |
+
the executable does not need to reside on the PATH.
|
| 386 |
+
|
| 387 |
+
If an orca server has been validated, then the full path to the
|
| 388 |
+
validated orca executable is stored in the
|
| 389 |
+
plotly.io.orca.status.executable property.
|
| 390 |
+
|
| 391 |
+
Returns
|
| 392 |
+
-------
|
| 393 |
+
str
|
| 394 |
+
"""
|
| 395 |
+
executable_list = self._props.get("executable_list", ["orca"])
|
| 396 |
+
if executable_list is None:
|
| 397 |
+
return None
|
| 398 |
+
else:
|
| 399 |
+
return " ".join(executable_list)
|
| 400 |
+
|
| 401 |
+
@executable.setter
|
| 402 |
+
def executable(self, val):
|
| 403 |
+
if val is None:
|
| 404 |
+
self._props.pop("executable", None)
|
| 405 |
+
else:
|
| 406 |
+
if not isinstance(val, str):
|
| 407 |
+
raise ValueError(
|
| 408 |
+
"""
|
| 409 |
+
The executable property must be a string, but received value of type {typ}.
|
| 410 |
+
Received value: {val}""".format(typ=type(val), val=val)
|
| 411 |
+
)
|
| 412 |
+
if isinstance(val, str):
|
| 413 |
+
val = [val]
|
| 414 |
+
self._props["executable_list"] = val
|
| 415 |
+
|
| 416 |
+
# Server and validation must restart before setting is active
|
| 417 |
+
reset_status()
|
| 418 |
+
|
| 419 |
+
@property
|
| 420 |
+
def timeout(self):
|
| 421 |
+
"""
|
| 422 |
+
The number of seconds of inactivity required before the orca server
|
| 423 |
+
is shut down.
|
| 424 |
+
|
| 425 |
+
For example, if timeout is set to 20, then the orca
|
| 426 |
+
server will shutdown once is has not been used for at least
|
| 427 |
+
20 seconds. If timeout is set to None, then the server will not be
|
| 428 |
+
automatically shut down due to inactivity.
|
| 429 |
+
|
| 430 |
+
Regardless of the value of timeout, a running orca server may be
|
| 431 |
+
manually shut down like this:
|
| 432 |
+
|
| 433 |
+
>>> import plotly.io as pio
|
| 434 |
+
>>> pio.orca.shutdown_server()
|
| 435 |
+
|
| 436 |
+
Returns
|
| 437 |
+
-------
|
| 438 |
+
int or float or None
|
| 439 |
+
"""
|
| 440 |
+
return self._props.get("timeout", None)
|
| 441 |
+
|
| 442 |
+
@timeout.setter
|
| 443 |
+
def timeout(self, val):
|
| 444 |
+
if val is None:
|
| 445 |
+
self._props.pop("timeout", None)
|
| 446 |
+
else:
|
| 447 |
+
if not isinstance(val, (int, float)):
|
| 448 |
+
raise ValueError(
|
| 449 |
+
"""
|
| 450 |
+
The timeout property must be a number, but received value of type {typ}.
|
| 451 |
+
Received value: {val}""".format(typ=type(val), val=val)
|
| 452 |
+
)
|
| 453 |
+
self._props["timeout"] = val
|
| 454 |
+
|
| 455 |
+
# Server must restart before setting is active
|
| 456 |
+
shutdown_server()
|
| 457 |
+
|
| 458 |
+
@property
|
| 459 |
+
def default_width(self):
|
| 460 |
+
"""
|
| 461 |
+
The default width to use on image export. This value is only
|
| 462 |
+
applied if no width value is supplied to the plotly.io
|
| 463 |
+
to_image or write_image functions.
|
| 464 |
+
|
| 465 |
+
Returns
|
| 466 |
+
-------
|
| 467 |
+
int or None
|
| 468 |
+
"""
|
| 469 |
+
return self._props.get("default_width", None)
|
| 470 |
+
|
| 471 |
+
@default_width.setter
|
| 472 |
+
def default_width(self, val):
|
| 473 |
+
if val is None:
|
| 474 |
+
self._props.pop("default_width", None)
|
| 475 |
+
return
|
| 476 |
+
if not isinstance(val, int):
|
| 477 |
+
raise ValueError(
|
| 478 |
+
"""
|
| 479 |
+
The default_width property must be an int, but received value of type {typ}.
|
| 480 |
+
Received value: {val}""".format(typ=type(val), val=val)
|
| 481 |
+
)
|
| 482 |
+
self._props["default_width"] = val
|
| 483 |
+
|
| 484 |
+
@property
|
| 485 |
+
def default_height(self):
|
| 486 |
+
"""
|
| 487 |
+
The default height to use on image export. This value is only
|
| 488 |
+
applied if no height value is supplied to the plotly.io
|
| 489 |
+
to_image or write_image functions.
|
| 490 |
+
|
| 491 |
+
Returns
|
| 492 |
+
-------
|
| 493 |
+
int or None
|
| 494 |
+
"""
|
| 495 |
+
return self._props.get("default_height", None)
|
| 496 |
+
|
| 497 |
+
@default_height.setter
|
| 498 |
+
def default_height(self, val):
|
| 499 |
+
if val is None:
|
| 500 |
+
self._props.pop("default_height", None)
|
| 501 |
+
return
|
| 502 |
+
if not isinstance(val, int):
|
| 503 |
+
raise ValueError(
|
| 504 |
+
"""
|
| 505 |
+
The default_height property must be an int, but received value of type {typ}.
|
| 506 |
+
Received value: {val}""".format(typ=type(val), val=val)
|
| 507 |
+
)
|
| 508 |
+
self._props["default_height"] = val
|
| 509 |
+
|
| 510 |
+
@property
|
| 511 |
+
def default_format(self):
|
| 512 |
+
"""
|
| 513 |
+
The default image format to use on image export.
|
| 514 |
+
|
| 515 |
+
Valid image formats strings are:
|
| 516 |
+
- 'png'
|
| 517 |
+
- 'jpg' or 'jpeg'
|
| 518 |
+
- 'webp'
|
| 519 |
+
- 'svg'
|
| 520 |
+
- 'pdf'
|
| 521 |
+
- 'eps' (Requires the poppler library to be installed)
|
| 522 |
+
|
| 523 |
+
This value is only applied if no format value is supplied to the
|
| 524 |
+
plotly.io to_image or write_image functions.
|
| 525 |
+
|
| 526 |
+
Returns
|
| 527 |
+
-------
|
| 528 |
+
str or None
|
| 529 |
+
"""
|
| 530 |
+
return self._props.get("default_format", "png")
|
| 531 |
+
|
| 532 |
+
@default_format.setter
|
| 533 |
+
def default_format(self, val):
|
| 534 |
+
if val is None:
|
| 535 |
+
self._props.pop("default_format", None)
|
| 536 |
+
return
|
| 537 |
+
|
| 538 |
+
val = validate_coerce_format(val)
|
| 539 |
+
self._props["default_format"] = val
|
| 540 |
+
|
| 541 |
+
@property
|
| 542 |
+
def default_scale(self):
|
| 543 |
+
"""
|
| 544 |
+
The default image scaling factor to use on image export.
|
| 545 |
+
This value is only applied if no scale value is supplied to the
|
| 546 |
+
plotly.io to_image or write_image functions.
|
| 547 |
+
|
| 548 |
+
Returns
|
| 549 |
+
-------
|
| 550 |
+
int or None
|
| 551 |
+
"""
|
| 552 |
+
return self._props.get("default_scale", 1)
|
| 553 |
+
|
| 554 |
+
@default_scale.setter
|
| 555 |
+
def default_scale(self, val):
|
| 556 |
+
if val is None:
|
| 557 |
+
self._props.pop("default_scale", None)
|
| 558 |
+
return
|
| 559 |
+
if not isinstance(val, (int, float)):
|
| 560 |
+
raise ValueError(
|
| 561 |
+
"""
|
| 562 |
+
The default_scale property must be a number, but received value of type {typ}.
|
| 563 |
+
Received value: {val}""".format(typ=type(val), val=val)
|
| 564 |
+
)
|
| 565 |
+
self._props["default_scale"] = val
|
| 566 |
+
|
| 567 |
+
@property
|
| 568 |
+
def topojson(self):
|
| 569 |
+
"""
|
| 570 |
+
Path to the topojson files needed to render choropleth traces.
|
| 571 |
+
|
| 572 |
+
If None, topojson files from the plot.ly CDN are used.
|
| 573 |
+
|
| 574 |
+
Returns
|
| 575 |
+
-------
|
| 576 |
+
str
|
| 577 |
+
"""
|
| 578 |
+
return self._props.get("topojson", None)
|
| 579 |
+
|
| 580 |
+
@topojson.setter
|
| 581 |
+
def topojson(self, val):
|
| 582 |
+
if val is None:
|
| 583 |
+
self._props.pop("topojson", None)
|
| 584 |
+
else:
|
| 585 |
+
if not isinstance(val, str):
|
| 586 |
+
raise ValueError(
|
| 587 |
+
"""
|
| 588 |
+
The topojson property must be a string, but received value of type {typ}.
|
| 589 |
+
Received value: {val}""".format(typ=type(val), val=val)
|
| 590 |
+
)
|
| 591 |
+
self._props["topojson"] = val
|
| 592 |
+
|
| 593 |
+
# Server must restart before setting is active
|
| 594 |
+
shutdown_server()
|
| 595 |
+
|
| 596 |
+
@property
|
| 597 |
+
def mathjax(self):
|
| 598 |
+
"""
|
| 599 |
+
Path to the MathJax bundle needed to render LaTeX characters
|
| 600 |
+
|
| 601 |
+
Returns
|
| 602 |
+
-------
|
| 603 |
+
str
|
| 604 |
+
"""
|
| 605 |
+
return self._props.get(
|
| 606 |
+
"mathjax",
|
| 607 |
+
"https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js",
|
| 608 |
+
)
|
| 609 |
+
|
| 610 |
+
@mathjax.setter
|
| 611 |
+
def mathjax(self, val):
|
| 612 |
+
if val is None:
|
| 613 |
+
self._props.pop("mathjax", None)
|
| 614 |
+
else:
|
| 615 |
+
if not isinstance(val, str):
|
| 616 |
+
raise ValueError(
|
| 617 |
+
"""
|
| 618 |
+
The mathjax property must be a string, but received value of type {typ}.
|
| 619 |
+
Received value: {val}""".format(typ=type(val), val=val)
|
| 620 |
+
)
|
| 621 |
+
self._props["mathjax"] = val
|
| 622 |
+
|
| 623 |
+
# Server must restart before setting is active
|
| 624 |
+
shutdown_server()
|
| 625 |
+
|
| 626 |
+
@property
|
| 627 |
+
def mapbox_access_token(self):
|
| 628 |
+
"""
|
| 629 |
+
Mapbox access token required to render mapbox traces.
|
| 630 |
+
|
| 631 |
+
Returns
|
| 632 |
+
-------
|
| 633 |
+
str
|
| 634 |
+
"""
|
| 635 |
+
return self._props.get("mapbox_access_token", None)
|
| 636 |
+
|
| 637 |
+
@mapbox_access_token.setter
|
| 638 |
+
def mapbox_access_token(self, val):
|
| 639 |
+
if val is None:
|
| 640 |
+
self._props.pop("mapbox_access_token", None)
|
| 641 |
+
else:
|
| 642 |
+
if not isinstance(val, str):
|
| 643 |
+
raise ValueError(
|
| 644 |
+
"""
|
| 645 |
+
The mapbox_access_token property must be a string, \
|
| 646 |
+
but received value of type {typ}.
|
| 647 |
+
Received value: {val}""".format(typ=type(val), val=val)
|
| 648 |
+
)
|
| 649 |
+
self._props["mapbox_access_token"] = val
|
| 650 |
+
|
| 651 |
+
# Server must restart before setting is active
|
| 652 |
+
shutdown_server()
|
| 653 |
+
|
| 654 |
+
@property
|
| 655 |
+
def use_xvfb(self):
|
| 656 |
+
dflt = "auto"
|
| 657 |
+
return self._props.get("use_xvfb", dflt)
|
| 658 |
+
|
| 659 |
+
@use_xvfb.setter
|
| 660 |
+
def use_xvfb(self, val):
|
| 661 |
+
valid_vals = [True, False, "auto"]
|
| 662 |
+
if val is None:
|
| 663 |
+
self._props.pop("use_xvfb", None)
|
| 664 |
+
else:
|
| 665 |
+
if val not in valid_vals:
|
| 666 |
+
raise ValueError(
|
| 667 |
+
"""
|
| 668 |
+
The use_xvfb property must be one of {valid_vals}
|
| 669 |
+
Received value of type {typ}: {val}""".format(
|
| 670 |
+
valid_vals=valid_vals, typ=type(val), val=repr(val)
|
| 671 |
+
)
|
| 672 |
+
)
|
| 673 |
+
|
| 674 |
+
self._props["use_xvfb"] = val
|
| 675 |
+
|
| 676 |
+
# Server and validation must restart before setting is active
|
| 677 |
+
reset_status()
|
| 678 |
+
|
| 679 |
+
@property
|
| 680 |
+
def plotlyjs(self):
|
| 681 |
+
"""
|
| 682 |
+
The plotly.js bundle being used for image rendering.
|
| 683 |
+
|
| 684 |
+
Returns
|
| 685 |
+
-------
|
| 686 |
+
str
|
| 687 |
+
"""
|
| 688 |
+
return self._constants.get("plotlyjs", None)
|
| 689 |
+
|
| 690 |
+
@property
|
| 691 |
+
def config_file(self):
|
| 692 |
+
"""
|
| 693 |
+
Path to orca configuration file
|
| 694 |
+
|
| 695 |
+
Using the `plotly.io.config.save()` method will save the current
|
| 696 |
+
configuration settings to this file. Settings in this file are
|
| 697 |
+
restored at the beginning of each sessions.
|
| 698 |
+
|
| 699 |
+
Returns
|
| 700 |
+
-------
|
| 701 |
+
str
|
| 702 |
+
"""
|
| 703 |
+
return os.path.join(PLOTLY_DIR, ".orca")
|
| 704 |
+
|
| 705 |
+
def __repr__(self):
|
| 706 |
+
"""
|
| 707 |
+
Display a nice representation of the current orca configuration.
|
| 708 |
+
"""
|
| 709 |
+
return """\
|
| 710 |
+
orca configuration
|
| 711 |
+
------------------
|
| 712 |
+
server_url: {server_url}
|
| 713 |
+
executable: {executable}
|
| 714 |
+
port: {port}
|
| 715 |
+
timeout: {timeout}
|
| 716 |
+
default_width: {default_width}
|
| 717 |
+
default_height: {default_height}
|
| 718 |
+
default_scale: {default_scale}
|
| 719 |
+
default_format: {default_format}
|
| 720 |
+
mathjax: {mathjax}
|
| 721 |
+
topojson: {topojson}
|
| 722 |
+
mapbox_access_token: {mapbox_access_token}
|
| 723 |
+
use_xvfb: {use_xvfb}
|
| 724 |
+
|
| 725 |
+
constants
|
| 726 |
+
---------
|
| 727 |
+
plotlyjs: {plotlyjs}
|
| 728 |
+
config_file: {config_file}
|
| 729 |
+
|
| 730 |
+
""".format(
|
| 731 |
+
server_url=self.server_url,
|
| 732 |
+
port=self.port,
|
| 733 |
+
executable=self.executable,
|
| 734 |
+
timeout=self.timeout,
|
| 735 |
+
default_width=self.default_width,
|
| 736 |
+
default_height=self.default_height,
|
| 737 |
+
default_scale=self.default_scale,
|
| 738 |
+
default_format=self.default_format,
|
| 739 |
+
mathjax=self.mathjax,
|
| 740 |
+
topojson=self.topojson,
|
| 741 |
+
mapbox_access_token=self.mapbox_access_token,
|
| 742 |
+
plotlyjs=self.plotlyjs,
|
| 743 |
+
config_file=self.config_file,
|
| 744 |
+
use_xvfb=self.use_xvfb,
|
| 745 |
+
)
|
| 746 |
+
|
| 747 |
+
|
| 748 |
+
# Make config a singleton object
|
| 749 |
+
# ------------------------------
|
| 750 |
+
config = OrcaConfig()
|
| 751 |
+
del OrcaConfig
|
| 752 |
+
|
| 753 |
+
|
| 754 |
+
# Orca status class
|
| 755 |
+
# ------------------------
|
| 756 |
+
class OrcaStatus(object):
|
| 757 |
+
"""
|
| 758 |
+
Class to store information about the current status of the orca server.
|
| 759 |
+
"""
|
| 760 |
+
|
| 761 |
+
_props = {
|
| 762 |
+
"state": "unvalidated", # or 'validated' or 'running'
|
| 763 |
+
"executable_list": None,
|
| 764 |
+
"version": None,
|
| 765 |
+
"pid": None,
|
| 766 |
+
"port": None,
|
| 767 |
+
"command": None,
|
| 768 |
+
}
|
| 769 |
+
|
| 770 |
+
@property
|
| 771 |
+
def state(self):
|
| 772 |
+
"""
|
| 773 |
+
A string representing the state of the orca server process
|
| 774 |
+
|
| 775 |
+
One of:
|
| 776 |
+
- unvalidated: The orca executable has not yet been searched for or
|
| 777 |
+
tested to make sure its valid.
|
| 778 |
+
- validated: The orca executable has been located and tested for
|
| 779 |
+
validity, but it is not running.
|
| 780 |
+
- running: The orca server process is currently running.
|
| 781 |
+
"""
|
| 782 |
+
return self._props["state"]
|
| 783 |
+
|
| 784 |
+
@property
|
| 785 |
+
def executable(self):
|
| 786 |
+
"""
|
| 787 |
+
If the `state` property is 'validated' or 'running', this property
|
| 788 |
+
contains the full path to the orca executable.
|
| 789 |
+
|
| 790 |
+
This path can be specified explicitly by setting the `executable`
|
| 791 |
+
property of the `plotly.io.orca.config` object.
|
| 792 |
+
|
| 793 |
+
This property will be None if the `state` is 'unvalidated'.
|
| 794 |
+
"""
|
| 795 |
+
executable_list = self._props["executable_list"]
|
| 796 |
+
if executable_list is None:
|
| 797 |
+
return None
|
| 798 |
+
else:
|
| 799 |
+
return " ".join(executable_list)
|
| 800 |
+
|
| 801 |
+
@property
|
| 802 |
+
def version(self):
|
| 803 |
+
"""
|
| 804 |
+
If the `state` property is 'validated' or 'running', this property
|
| 805 |
+
contains the version of the validated orca executable.
|
| 806 |
+
|
| 807 |
+
This property will be None if the `state` is 'unvalidated'.
|
| 808 |
+
"""
|
| 809 |
+
return self._props["version"]
|
| 810 |
+
|
| 811 |
+
@property
|
| 812 |
+
def pid(self):
|
| 813 |
+
"""
|
| 814 |
+
The process id of the orca server process, if any. This property
|
| 815 |
+
will be None if the `state` is not 'running'.
|
| 816 |
+
"""
|
| 817 |
+
return self._props["pid"]
|
| 818 |
+
|
| 819 |
+
@property
|
| 820 |
+
def port(self):
|
| 821 |
+
"""
|
| 822 |
+
The port number that the orca server process is listening to, if any.
|
| 823 |
+
This property will be None if the `state` is not 'running'.
|
| 824 |
+
|
| 825 |
+
This port can be specified explicitly by setting the `port`
|
| 826 |
+
property of the `plotly.io.orca.config` object.
|
| 827 |
+
"""
|
| 828 |
+
return self._props["port"]
|
| 829 |
+
|
| 830 |
+
@property
|
| 831 |
+
def command(self):
|
| 832 |
+
"""
|
| 833 |
+
The command arguments used to launch the running orca server, if any.
|
| 834 |
+
This property will be None if the `state` is not 'running'.
|
| 835 |
+
"""
|
| 836 |
+
return self._props["command"]
|
| 837 |
+
|
| 838 |
+
def __repr__(self):
|
| 839 |
+
"""
|
| 840 |
+
Display a nice representation of the current orca server status.
|
| 841 |
+
"""
|
| 842 |
+
return """\
|
| 843 |
+
orca status
|
| 844 |
+
-----------
|
| 845 |
+
state: {state}
|
| 846 |
+
executable: {executable}
|
| 847 |
+
version: {version}
|
| 848 |
+
port: {port}
|
| 849 |
+
pid: {pid}
|
| 850 |
+
command: {command}
|
| 851 |
+
|
| 852 |
+
""".format(
|
| 853 |
+
executable=self.executable,
|
| 854 |
+
version=self.version,
|
| 855 |
+
port=self.port,
|
| 856 |
+
pid=self.pid,
|
| 857 |
+
state=self.state,
|
| 858 |
+
command=self.command,
|
| 859 |
+
)
|
| 860 |
+
|
| 861 |
+
|
| 862 |
+
# Make status a singleton object
|
| 863 |
+
# ------------------------------
|
| 864 |
+
status = OrcaStatus()
|
| 865 |
+
del OrcaStatus
|
| 866 |
+
|
| 867 |
+
|
| 868 |
+
@contextmanager
|
| 869 |
+
def orca_env():
|
| 870 |
+
"""
|
| 871 |
+
Context manager to clear and restore environment variables that are
|
| 872 |
+
problematic for orca to function properly
|
| 873 |
+
|
| 874 |
+
NODE_OPTIONS: When this variable is set, orca <v1.2 will have a
|
| 875 |
+
segmentation fault due to an electron bug.
|
| 876 |
+
See: https://github.com/electron/electron/issues/12695
|
| 877 |
+
|
| 878 |
+
ELECTRON_RUN_AS_NODE: When this environment variable is set the call
|
| 879 |
+
to orca is transformed into a call to nodejs.
|
| 880 |
+
See https://github.com/plotly/orca/issues/149#issuecomment-443506732
|
| 881 |
+
"""
|
| 882 |
+
clear_env_vars = ["NODE_OPTIONS", "ELECTRON_RUN_AS_NODE", "LD_PRELOAD"]
|
| 883 |
+
orig_env_vars = {}
|
| 884 |
+
|
| 885 |
+
try:
|
| 886 |
+
# Clear and save
|
| 887 |
+
orig_env_vars.update(
|
| 888 |
+
{var: os.environ.pop(var) for var in clear_env_vars if var in os.environ}
|
| 889 |
+
)
|
| 890 |
+
yield
|
| 891 |
+
finally:
|
| 892 |
+
# Restore
|
| 893 |
+
for var, val in orig_env_vars.items():
|
| 894 |
+
os.environ[var] = val
|
| 895 |
+
|
| 896 |
+
|
| 897 |
+
# Public orca server interaction functions
|
| 898 |
+
# ----------------------------------------
|
| 899 |
+
def validate_executable():
|
| 900 |
+
"""
|
| 901 |
+
Attempt to find and validate the orca executable specified by the
|
| 902 |
+
`plotly.io.orca.config.executable` property.
|
| 903 |
+
|
| 904 |
+
If the `plotly.io.orca.status.state` property is 'validated' or 'running'
|
| 905 |
+
then this function does nothing.
|
| 906 |
+
|
| 907 |
+
How it works:
|
| 908 |
+
- First, it searches the system PATH for an executable that matches the
|
| 909 |
+
name or path specified in the `plotly.io.orca.config.executable`
|
| 910 |
+
property.
|
| 911 |
+
- Then it runs the executable with the `--help` flag to make sure
|
| 912 |
+
it's the plotly orca executable
|
| 913 |
+
- Then it runs the executable with the `--version` flag to check the
|
| 914 |
+
orca version.
|
| 915 |
+
|
| 916 |
+
If all of these steps are successful then the `status.state` property
|
| 917 |
+
is set to 'validated' and the `status.executable` and `status.version`
|
| 918 |
+
properties are populated
|
| 919 |
+
|
| 920 |
+
Returns
|
| 921 |
+
-------
|
| 922 |
+
None
|
| 923 |
+
"""
|
| 924 |
+
# Check state
|
| 925 |
+
# -----------
|
| 926 |
+
if status.state != "unvalidated":
|
| 927 |
+
# Nothing more to do
|
| 928 |
+
return
|
| 929 |
+
|
| 930 |
+
# Initialize error messages
|
| 931 |
+
# -------------------------
|
| 932 |
+
install_location_instructions = """\
|
| 933 |
+
If you haven't installed orca yet, you can do so using conda as follows:
|
| 934 |
+
|
| 935 |
+
$ conda install -c plotly plotly-orca
|
| 936 |
+
|
| 937 |
+
Alternatively, see other installation methods in the orca project README at
|
| 938 |
+
https://github.com/plotly/orca
|
| 939 |
+
|
| 940 |
+
After installation is complete, no further configuration should be needed.
|
| 941 |
+
|
| 942 |
+
If you have installed orca, then for some reason plotly.py was unable to
|
| 943 |
+
locate it. In this case, set the `plotly.io.orca.config.executable`
|
| 944 |
+
property to the full path of your orca executable. For example:
|
| 945 |
+
|
| 946 |
+
>>> plotly.io.orca.config.executable = '/path/to/orca'
|
| 947 |
+
|
| 948 |
+
After updating this executable property, try the export operation again.
|
| 949 |
+
If it is successful then you may want to save this configuration so that it
|
| 950 |
+
will be applied automatically in future sessions. You can do this as follows:
|
| 951 |
+
|
| 952 |
+
>>> plotly.io.orca.config.save()
|
| 953 |
+
|
| 954 |
+
If you're still having trouble, feel free to ask for help on the forums at
|
| 955 |
+
https://community.plot.ly/c/api/python
|
| 956 |
+
"""
|
| 957 |
+
|
| 958 |
+
# Try to find an executable
|
| 959 |
+
# -------------------------
|
| 960 |
+
# Search for executable name or path in config.executable
|
| 961 |
+
executable = which(config.executable)
|
| 962 |
+
path = os.environ.get("PATH", os.defpath)
|
| 963 |
+
formatted_path = path.replace(os.pathsep, "\n ")
|
| 964 |
+
|
| 965 |
+
if executable is None:
|
| 966 |
+
raise ValueError(
|
| 967 |
+
"""
|
| 968 |
+
The orca executable is required to export figures as static images,
|
| 969 |
+
but it could not be found on the system path.
|
| 970 |
+
|
| 971 |
+
Searched for executable '{executable}' on the following path:
|
| 972 |
+
{formatted_path}
|
| 973 |
+
|
| 974 |
+
{instructions}""".format(
|
| 975 |
+
executable=config.executable,
|
| 976 |
+
formatted_path=formatted_path,
|
| 977 |
+
instructions=install_location_instructions,
|
| 978 |
+
)
|
| 979 |
+
)
|
| 980 |
+
|
| 981 |
+
# Check if we should run with Xvfb
|
| 982 |
+
# --------------------------------
|
| 983 |
+
xvfb_args = [
|
| 984 |
+
"--auto-servernum",
|
| 985 |
+
"--server-args",
|
| 986 |
+
"-screen 0 640x480x24 +extension RANDR +extension GLX",
|
| 987 |
+
executable,
|
| 988 |
+
]
|
| 989 |
+
|
| 990 |
+
if config.use_xvfb:
|
| 991 |
+
# Use xvfb
|
| 992 |
+
xvfb_run_executable = which("xvfb-run")
|
| 993 |
+
if not xvfb_run_executable:
|
| 994 |
+
raise ValueError(
|
| 995 |
+
"""
|
| 996 |
+
The plotly.io.orca.config.use_xvfb property is set to True, but the
|
| 997 |
+
xvfb-run executable could not be found on the system path.
|
| 998 |
+
|
| 999 |
+
Searched for the executable 'xvfb-run' on the following path:
|
| 1000 |
+
{formatted_path}""".format(formatted_path=formatted_path)
|
| 1001 |
+
)
|
| 1002 |
+
|
| 1003 |
+
executable_list = [xvfb_run_executable] + xvfb_args
|
| 1004 |
+
elif (
|
| 1005 |
+
config.use_xvfb == "auto"
|
| 1006 |
+
and sys.platform.startswith("linux")
|
| 1007 |
+
and not os.environ.get("DISPLAY")
|
| 1008 |
+
and which("xvfb-run")
|
| 1009 |
+
):
|
| 1010 |
+
# use_xvfb is 'auto', we're on linux without a display server,
|
| 1011 |
+
# and xvfb-run is available. Use it.
|
| 1012 |
+
xvfb_run_executable = which("xvfb-run")
|
| 1013 |
+
executable_list = [xvfb_run_executable] + xvfb_args
|
| 1014 |
+
else:
|
| 1015 |
+
# Do not use xvfb
|
| 1016 |
+
executable_list = [executable]
|
| 1017 |
+
|
| 1018 |
+
# Run executable with --help and see if it's our orca
|
| 1019 |
+
# ---------------------------------------------------
|
| 1020 |
+
invalid_executable_msg = """
|
| 1021 |
+
The orca executable is required in order to export figures as static images,
|
| 1022 |
+
but the executable that was found at '{executable}'
|
| 1023 |
+
does not seem to be a valid plotly orca executable. Please refer to the end of
|
| 1024 |
+
this message for details on what went wrong.
|
| 1025 |
+
|
| 1026 |
+
{instructions}""".format(
|
| 1027 |
+
executable=executable, instructions=install_location_instructions
|
| 1028 |
+
)
|
| 1029 |
+
|
| 1030 |
+
# ### Run with Popen so we get access to stdout and stderr
|
| 1031 |
+
with orca_env():
|
| 1032 |
+
p = subprocess.Popen(
|
| 1033 |
+
executable_list + ["--help"], stdout=subprocess.PIPE, stderr=subprocess.PIPE
|
| 1034 |
+
)
|
| 1035 |
+
|
| 1036 |
+
help_result, help_error = p.communicate()
|
| 1037 |
+
|
| 1038 |
+
if p.returncode != 0:
|
| 1039 |
+
err_msg = (
|
| 1040 |
+
invalid_executable_msg
|
| 1041 |
+
+ """
|
| 1042 |
+
Here is the error that was returned by the command
|
| 1043 |
+
$ {executable} --help
|
| 1044 |
+
|
| 1045 |
+
[Return code: {returncode}]
|
| 1046 |
+
{err_msg}
|
| 1047 |
+
""".format(
|
| 1048 |
+
executable=" ".join(executable_list),
|
| 1049 |
+
err_msg=help_error.decode("utf-8"),
|
| 1050 |
+
returncode=p.returncode,
|
| 1051 |
+
)
|
| 1052 |
+
)
|
| 1053 |
+
|
| 1054 |
+
# Check for Linux without X installed.
|
| 1055 |
+
if sys.platform.startswith("linux") and not os.environ.get("DISPLAY"):
|
| 1056 |
+
err_msg += """\
|
| 1057 |
+
Note: When used on Linux, orca requires an X11 display server, but none was
|
| 1058 |
+
detected. Please install Xvfb and configure plotly.py to run orca using Xvfb
|
| 1059 |
+
as follows:
|
| 1060 |
+
|
| 1061 |
+
>>> import plotly.io as pio
|
| 1062 |
+
>>> pio.orca.config.use_xvfb = True
|
| 1063 |
+
|
| 1064 |
+
You can save this configuration for use in future sessions as follows:
|
| 1065 |
+
|
| 1066 |
+
>>> pio.orca.config.save()
|
| 1067 |
+
|
| 1068 |
+
See https://www.x.org/releases/X11R7.6/doc/man/man1/Xvfb.1.xhtml
|
| 1069 |
+
for more info on Xvfb
|
| 1070 |
+
"""
|
| 1071 |
+
raise ValueError(err_msg)
|
| 1072 |
+
|
| 1073 |
+
if not help_result:
|
| 1074 |
+
raise ValueError(
|
| 1075 |
+
invalid_executable_msg
|
| 1076 |
+
+ """
|
| 1077 |
+
The error encountered is that no output was returned by the command
|
| 1078 |
+
$ {executable} --help
|
| 1079 |
+
""".format(executable=" ".join(executable_list))
|
| 1080 |
+
)
|
| 1081 |
+
|
| 1082 |
+
if "Plotly's image-exporting utilities" not in help_result.decode("utf-8"):
|
| 1083 |
+
raise ValueError(
|
| 1084 |
+
invalid_executable_msg
|
| 1085 |
+
+ """
|
| 1086 |
+
The error encountered is that unexpected output was returned by the command
|
| 1087 |
+
$ {executable} --help
|
| 1088 |
+
|
| 1089 |
+
{help_result}
|
| 1090 |
+
""".format(executable=" ".join(executable_list), help_result=help_result)
|
| 1091 |
+
)
|
| 1092 |
+
|
| 1093 |
+
# Get orca version
|
| 1094 |
+
# ----------------
|
| 1095 |
+
# ### Run with Popen so we get access to stdout and stderr
|
| 1096 |
+
with orca_env():
|
| 1097 |
+
p = subprocess.Popen(
|
| 1098 |
+
executable_list + ["--version"],
|
| 1099 |
+
stdout=subprocess.PIPE,
|
| 1100 |
+
stderr=subprocess.PIPE,
|
| 1101 |
+
)
|
| 1102 |
+
|
| 1103 |
+
version_result, version_error = p.communicate()
|
| 1104 |
+
|
| 1105 |
+
if p.returncode != 0:
|
| 1106 |
+
raise ValueError(
|
| 1107 |
+
invalid_executable_msg
|
| 1108 |
+
+ """
|
| 1109 |
+
An error occurred while trying to get the version of the orca executable.
|
| 1110 |
+
Here is the command that plotly.py ran to request the version
|
| 1111 |
+
$ {executable} --version
|
| 1112 |
+
|
| 1113 |
+
This command returned the following error:
|
| 1114 |
+
|
| 1115 |
+
[Return code: {returncode}]
|
| 1116 |
+
{err_msg}
|
| 1117 |
+
""".format(
|
| 1118 |
+
executable=" ".join(executable_list),
|
| 1119 |
+
err_msg=version_error.decode("utf-8"),
|
| 1120 |
+
returncode=p.returncode,
|
| 1121 |
+
)
|
| 1122 |
+
)
|
| 1123 |
+
|
| 1124 |
+
if not version_result:
|
| 1125 |
+
raise ValueError(
|
| 1126 |
+
invalid_executable_msg
|
| 1127 |
+
+ """
|
| 1128 |
+
The error encountered is that no version was reported by the orca executable.
|
| 1129 |
+
Here is the command that plotly.py ran to request the version:
|
| 1130 |
+
|
| 1131 |
+
$ {executable} --version
|
| 1132 |
+
""".format(executable=" ".join(executable_list))
|
| 1133 |
+
)
|
| 1134 |
+
else:
|
| 1135 |
+
version_result = version_result.decode()
|
| 1136 |
+
|
| 1137 |
+
status._props["executable_list"] = executable_list
|
| 1138 |
+
status._props["version"] = version_result.strip()
|
| 1139 |
+
status._props["state"] = "validated"
|
| 1140 |
+
|
| 1141 |
+
|
| 1142 |
+
def reset_status():
|
| 1143 |
+
"""
|
| 1144 |
+
Shutdown the running orca server, if any, and reset the orca status
|
| 1145 |
+
to unvalidated.
|
| 1146 |
+
|
| 1147 |
+
This command is only needed if the desired orca executable is changed
|
| 1148 |
+
during an interactive session.
|
| 1149 |
+
|
| 1150 |
+
Returns
|
| 1151 |
+
-------
|
| 1152 |
+
None
|
| 1153 |
+
"""
|
| 1154 |
+
shutdown_server()
|
| 1155 |
+
status._props["executable_list"] = None
|
| 1156 |
+
status._props["version"] = None
|
| 1157 |
+
status._props["state"] = "unvalidated"
|
| 1158 |
+
|
| 1159 |
+
|
| 1160 |
+
# Initialze process control variables
|
| 1161 |
+
# -----------------------------------
|
| 1162 |
+
orca_lock = threading.Lock()
|
| 1163 |
+
orca_state = {"proc": None, "shutdown_timer": None}
|
| 1164 |
+
|
| 1165 |
+
|
| 1166 |
+
# Shutdown
|
| 1167 |
+
# --------
|
| 1168 |
+
# The @atexit.register annotation ensures that the shutdown function is
|
| 1169 |
+
# is run when the Python process is terminated
|
| 1170 |
+
@atexit.register
|
| 1171 |
+
def cleanup():
|
| 1172 |
+
shutdown_server()
|
| 1173 |
+
|
| 1174 |
+
|
| 1175 |
+
def shutdown_server():
|
| 1176 |
+
"""
|
| 1177 |
+
Shutdown the running orca server process, if any
|
| 1178 |
+
|
| 1179 |
+
Returns
|
| 1180 |
+
-------
|
| 1181 |
+
None
|
| 1182 |
+
"""
|
| 1183 |
+
# Use double-check locking to make sure the properties of orca_state
|
| 1184 |
+
# are updated consistently across threads.
|
| 1185 |
+
if orca_state["proc"] is not None:
|
| 1186 |
+
with orca_lock:
|
| 1187 |
+
if orca_state["proc"] is not None:
|
| 1188 |
+
# We use psutil to kill all child processes of the main orca
|
| 1189 |
+
# process. This prevents any zombie processes from being
|
| 1190 |
+
# left over, and it saves us from needing to write
|
| 1191 |
+
# OS-specific process management code here.
|
| 1192 |
+
|
| 1193 |
+
parent = psutil.Process(orca_state["proc"].pid)
|
| 1194 |
+
for child in parent.children(recursive=True):
|
| 1195 |
+
try:
|
| 1196 |
+
child.terminate()
|
| 1197 |
+
except Exception:
|
| 1198 |
+
# We tried, move on
|
| 1199 |
+
pass
|
| 1200 |
+
|
| 1201 |
+
try:
|
| 1202 |
+
# Kill parent process
|
| 1203 |
+
orca_state["proc"].terminate()
|
| 1204 |
+
|
| 1205 |
+
# Wait for the process to shutdown
|
| 1206 |
+
orca_state["proc"].wait()
|
| 1207 |
+
except Exception:
|
| 1208 |
+
# We tried, move on
|
| 1209 |
+
pass
|
| 1210 |
+
|
| 1211 |
+
# Update our internal process management state
|
| 1212 |
+
orca_state["proc"] = None
|
| 1213 |
+
|
| 1214 |
+
if orca_state["shutdown_timer"] is not None:
|
| 1215 |
+
orca_state["shutdown_timer"].cancel()
|
| 1216 |
+
orca_state["shutdown_timer"] = None
|
| 1217 |
+
|
| 1218 |
+
orca_state["port"] = None
|
| 1219 |
+
|
| 1220 |
+
# Update orca.status so the user has an accurate view
|
| 1221 |
+
# of the state of the orca server
|
| 1222 |
+
status._props["state"] = "validated"
|
| 1223 |
+
status._props["pid"] = None
|
| 1224 |
+
status._props["port"] = None
|
| 1225 |
+
status._props["command"] = None
|
| 1226 |
+
|
| 1227 |
+
|
| 1228 |
+
# Launch or get server
|
| 1229 |
+
def ensure_server():
|
| 1230 |
+
"""
|
| 1231 |
+
Start an orca server if none is running. If a server is already running,
|
| 1232 |
+
then reset the timeout countdown
|
| 1233 |
+
|
| 1234 |
+
Returns
|
| 1235 |
+
-------
|
| 1236 |
+
None
|
| 1237 |
+
"""
|
| 1238 |
+
|
| 1239 |
+
# Validate psutil
|
| 1240 |
+
if psutil is None:
|
| 1241 |
+
raise ValueError(
|
| 1242 |
+
"""\
|
| 1243 |
+
Image generation requires the psutil package.
|
| 1244 |
+
|
| 1245 |
+
Install using pip:
|
| 1246 |
+
$ pip install psutil
|
| 1247 |
+
|
| 1248 |
+
Install using conda:
|
| 1249 |
+
$ conda install psutil
|
| 1250 |
+
"""
|
| 1251 |
+
)
|
| 1252 |
+
|
| 1253 |
+
# Validate requests
|
| 1254 |
+
if not get_module("requests"):
|
| 1255 |
+
raise ValueError(
|
| 1256 |
+
"""\
|
| 1257 |
+
Image generation requires the requests package.
|
| 1258 |
+
|
| 1259 |
+
Install using pip:
|
| 1260 |
+
$ pip install requests
|
| 1261 |
+
|
| 1262 |
+
Install using conda:
|
| 1263 |
+
$ conda install requests
|
| 1264 |
+
"""
|
| 1265 |
+
)
|
| 1266 |
+
|
| 1267 |
+
if not config.server_url:
|
| 1268 |
+
# Validate orca executable only if server_url is not provided
|
| 1269 |
+
if status.state == "unvalidated":
|
| 1270 |
+
validate_executable()
|
| 1271 |
+
# Acquire lock to make sure that we keep the properties of orca_state
|
| 1272 |
+
# consistent across threads
|
| 1273 |
+
with orca_lock:
|
| 1274 |
+
# Cancel the current shutdown timer, if any
|
| 1275 |
+
if orca_state["shutdown_timer"] is not None:
|
| 1276 |
+
orca_state["shutdown_timer"].cancel()
|
| 1277 |
+
|
| 1278 |
+
# Start a new server process if none is active
|
| 1279 |
+
if orca_state["proc"] is None:
|
| 1280 |
+
# Determine server port
|
| 1281 |
+
if config.port is None:
|
| 1282 |
+
orca_state["port"] = find_open_port()
|
| 1283 |
+
else:
|
| 1284 |
+
orca_state["port"] = config.port
|
| 1285 |
+
|
| 1286 |
+
# Build orca command list
|
| 1287 |
+
cmd_list = status._props["executable_list"] + [
|
| 1288 |
+
"serve",
|
| 1289 |
+
"-p",
|
| 1290 |
+
str(orca_state["port"]),
|
| 1291 |
+
"--plotly",
|
| 1292 |
+
config.plotlyjs,
|
| 1293 |
+
"--graph-only",
|
| 1294 |
+
]
|
| 1295 |
+
|
| 1296 |
+
if config.topojson:
|
| 1297 |
+
cmd_list.extend(["--topojson", config.topojson])
|
| 1298 |
+
|
| 1299 |
+
if config.mathjax:
|
| 1300 |
+
cmd_list.extend(["--mathjax", config.mathjax])
|
| 1301 |
+
|
| 1302 |
+
if config.mapbox_access_token:
|
| 1303 |
+
cmd_list.extend(
|
| 1304 |
+
["--mapbox-access-token", config.mapbox_access_token]
|
| 1305 |
+
)
|
| 1306 |
+
|
| 1307 |
+
# Create subprocess that launches the orca server on the
|
| 1308 |
+
# specified port.
|
| 1309 |
+
DEVNULL = open(os.devnull, "wb")
|
| 1310 |
+
with orca_env():
|
| 1311 |
+
stderr = DEVNULL if "CI" in os.environ else None # fix for CI
|
| 1312 |
+
orca_state["proc"] = subprocess.Popen(
|
| 1313 |
+
cmd_list, stdout=DEVNULL, stderr=stderr
|
| 1314 |
+
)
|
| 1315 |
+
|
| 1316 |
+
# Update orca.status so the user has an accurate view
|
| 1317 |
+
# of the state of the orca server
|
| 1318 |
+
status._props["state"] = "running"
|
| 1319 |
+
status._props["pid"] = orca_state["proc"].pid
|
| 1320 |
+
status._props["port"] = orca_state["port"]
|
| 1321 |
+
status._props["command"] = cmd_list
|
| 1322 |
+
|
| 1323 |
+
# Create new shutdown timer if a timeout was specified
|
| 1324 |
+
if config.timeout is not None:
|
| 1325 |
+
t = threading.Timer(config.timeout, shutdown_server)
|
| 1326 |
+
# Make it a daemon thread so that exit won't wait for timer to
|
| 1327 |
+
# complete
|
| 1328 |
+
t.daemon = True
|
| 1329 |
+
t.start()
|
| 1330 |
+
orca_state["shutdown_timer"] = t
|
| 1331 |
+
|
| 1332 |
+
|
| 1333 |
+
@retry(min_wait=5, max_wait=10, max_delay=60000)
|
| 1334 |
+
def request_image_with_retrying(**kwargs):
|
| 1335 |
+
"""
|
| 1336 |
+
Helper method to perform an image request to a running orca server process
|
| 1337 |
+
with retrying logic.
|
| 1338 |
+
"""
|
| 1339 |
+
from requests import post
|
| 1340 |
+
from plotly.io.json import to_json_plotly
|
| 1341 |
+
|
| 1342 |
+
if config.server_url:
|
| 1343 |
+
server_url = config.server_url
|
| 1344 |
+
else:
|
| 1345 |
+
server_url = "http://{hostname}:{port}".format(
|
| 1346 |
+
hostname="localhost", port=orca_state["port"]
|
| 1347 |
+
)
|
| 1348 |
+
|
| 1349 |
+
request_params = {k: v for k, v in kwargs.items() if v is not None}
|
| 1350 |
+
json_str = to_json_plotly(request_params)
|
| 1351 |
+
response = post(server_url + "/", data=json_str)
|
| 1352 |
+
|
| 1353 |
+
if response.status_code == 522:
|
| 1354 |
+
# On "522: client socket timeout", return server and keep trying
|
| 1355 |
+
shutdown_server()
|
| 1356 |
+
ensure_server()
|
| 1357 |
+
raise OSError("522: client socket timeout")
|
| 1358 |
+
|
| 1359 |
+
return response
|
| 1360 |
+
|
| 1361 |
+
|
| 1362 |
+
def to_image(fig, format=None, width=None, height=None, scale=None, validate=True):
|
| 1363 |
+
"""
|
| 1364 |
+
Convert a figure to a static image bytes string
|
| 1365 |
+
|
| 1366 |
+
Parameters
|
| 1367 |
+
----------
|
| 1368 |
+
fig:
|
| 1369 |
+
Figure object or dict representing a figure
|
| 1370 |
+
|
| 1371 |
+
format: str or None
|
| 1372 |
+
The desired image format. One of
|
| 1373 |
+
- 'png'
|
| 1374 |
+
- 'jpg' or 'jpeg'
|
| 1375 |
+
- 'webp'
|
| 1376 |
+
- 'svg'
|
| 1377 |
+
- 'pdf'
|
| 1378 |
+
- 'eps' (Requires the poppler library to be installed)
|
| 1379 |
+
|
| 1380 |
+
If not specified, will default to `plotly.io.config.default_format`
|
| 1381 |
+
|
| 1382 |
+
width: int or None
|
| 1383 |
+
The width of the exported image in layout pixels. If the `scale`
|
| 1384 |
+
property is 1.0, this will also be the width of the exported image
|
| 1385 |
+
in physical pixels.
|
| 1386 |
+
|
| 1387 |
+
If not specified, will default to `plotly.io.config.default_width`
|
| 1388 |
+
|
| 1389 |
+
height: int or None
|
| 1390 |
+
The height of the exported image in layout pixels. If the `scale`
|
| 1391 |
+
property is 1.0, this will also be the height of the exported image
|
| 1392 |
+
in physical pixels.
|
| 1393 |
+
|
| 1394 |
+
If not specified, will default to `plotly.io.config.default_height`
|
| 1395 |
+
|
| 1396 |
+
scale: int or float or None
|
| 1397 |
+
The scale factor to use when exporting the figure. A scale factor
|
| 1398 |
+
larger than 1.0 will increase the image resolution with respect
|
| 1399 |
+
to the figure's layout pixel dimensions. Whereas as scale factor of
|
| 1400 |
+
less than 1.0 will decrease the image resolution.
|
| 1401 |
+
|
| 1402 |
+
If not specified, will default to `plotly.io.config.default_scale`
|
| 1403 |
+
|
| 1404 |
+
validate: bool
|
| 1405 |
+
True if the figure should be validated before being converted to
|
| 1406 |
+
an image, False otherwise.
|
| 1407 |
+
|
| 1408 |
+
Returns
|
| 1409 |
+
-------
|
| 1410 |
+
bytes
|
| 1411 |
+
The image data
|
| 1412 |
+
"""
|
| 1413 |
+
# Make sure orca sever is running
|
| 1414 |
+
# -------------------------------
|
| 1415 |
+
ensure_server()
|
| 1416 |
+
|
| 1417 |
+
# Handle defaults
|
| 1418 |
+
# ---------------
|
| 1419 |
+
# Apply configuration defaults to unspecified arguments
|
| 1420 |
+
if format is None:
|
| 1421 |
+
format = config.default_format
|
| 1422 |
+
|
| 1423 |
+
format = validate_coerce_format(format)
|
| 1424 |
+
|
| 1425 |
+
if scale is None:
|
| 1426 |
+
scale = config.default_scale
|
| 1427 |
+
|
| 1428 |
+
if width is None:
|
| 1429 |
+
width = config.default_width
|
| 1430 |
+
|
| 1431 |
+
if height is None:
|
| 1432 |
+
height = config.default_height
|
| 1433 |
+
|
| 1434 |
+
# Validate figure
|
| 1435 |
+
# ---------------
|
| 1436 |
+
fig_dict = validate_coerce_fig_to_dict(fig, validate)
|
| 1437 |
+
|
| 1438 |
+
# Request image from server
|
| 1439 |
+
# -------------------------
|
| 1440 |
+
try:
|
| 1441 |
+
response = request_image_with_retrying(
|
| 1442 |
+
figure=fig_dict, format=format, scale=scale, width=width, height=height
|
| 1443 |
+
)
|
| 1444 |
+
except OSError:
|
| 1445 |
+
# Get current status string
|
| 1446 |
+
status_str = repr(status)
|
| 1447 |
+
|
| 1448 |
+
if config.server_url:
|
| 1449 |
+
raise ValueError(
|
| 1450 |
+
"""
|
| 1451 |
+
Plotly.py was unable to communicate with the orca server at {server_url}
|
| 1452 |
+
|
| 1453 |
+
Please check that the server is running and accessible.
|
| 1454 |
+
""".format(server_url=config.server_url)
|
| 1455 |
+
)
|
| 1456 |
+
|
| 1457 |
+
else:
|
| 1458 |
+
# Check if the orca server process exists
|
| 1459 |
+
pid_exists = psutil.pid_exists(status.pid)
|
| 1460 |
+
|
| 1461 |
+
# Raise error message based on whether the server process existed
|
| 1462 |
+
if pid_exists:
|
| 1463 |
+
raise ValueError(
|
| 1464 |
+
"""
|
| 1465 |
+
For some reason plotly.py was unable to communicate with the
|
| 1466 |
+
local orca server process, even though the server process seems to be running.
|
| 1467 |
+
|
| 1468 |
+
Please review the process and connection information below:
|
| 1469 |
+
|
| 1470 |
+
{info}
|
| 1471 |
+
""".format(info=status_str)
|
| 1472 |
+
)
|
| 1473 |
+
else:
|
| 1474 |
+
# Reset the status so that if the user tries again, we'll try to
|
| 1475 |
+
# start the server again
|
| 1476 |
+
reset_status()
|
| 1477 |
+
raise ValueError(
|
| 1478 |
+
"""
|
| 1479 |
+
For some reason the orca server process is no longer running.
|
| 1480 |
+
|
| 1481 |
+
Please review the process and connection information below:
|
| 1482 |
+
|
| 1483 |
+
{info}
|
| 1484 |
+
plotly.py will attempt to start the local server process again the next time
|
| 1485 |
+
an image export operation is performed.
|
| 1486 |
+
""".format(info=status_str)
|
| 1487 |
+
)
|
| 1488 |
+
|
| 1489 |
+
# Check response
|
| 1490 |
+
# --------------
|
| 1491 |
+
if response.status_code == 200:
|
| 1492 |
+
# All good
|
| 1493 |
+
return response.content
|
| 1494 |
+
else:
|
| 1495 |
+
# ### Something went wrong ###
|
| 1496 |
+
err_message = """
|
| 1497 |
+
The image request was rejected by the orca conversion utility
|
| 1498 |
+
with the following error:
|
| 1499 |
+
{status}: {msg}
|
| 1500 |
+
""".format(status=response.status_code, msg=response.content.decode("utf-8"))
|
| 1501 |
+
|
| 1502 |
+
# ### Try to be helpful ###
|
| 1503 |
+
# Status codes from /src/component/plotly-graph/constants.js in the
|
| 1504 |
+
# orca code base.
|
| 1505 |
+
# statusMsg: {
|
| 1506 |
+
# 400: 'invalid or malformed request syntax',
|
| 1507 |
+
# 522: client socket timeout
|
| 1508 |
+
# 525: 'plotly.js error',
|
| 1509 |
+
# 526: 'plotly.js version 1.11.0 or up required',
|
| 1510 |
+
# 530: 'image conversion error'
|
| 1511 |
+
# }
|
| 1512 |
+
if response.status_code == 400 and isinstance(fig, dict) and not validate:
|
| 1513 |
+
err_message += """
|
| 1514 |
+
Try setting the `validate` argument to True to check for errors in the
|
| 1515 |
+
figure specification"""
|
| 1516 |
+
elif response.status_code == 525:
|
| 1517 |
+
any_mapbox = any(
|
| 1518 |
+
[
|
| 1519 |
+
trace.get("type", None) == "scattermapbox"
|
| 1520 |
+
for trace in fig_dict.get("data", [])
|
| 1521 |
+
]
|
| 1522 |
+
)
|
| 1523 |
+
if any_mapbox and config.mapbox_access_token is None:
|
| 1524 |
+
err_message += """
|
| 1525 |
+
Exporting scattermapbox traces requires a mapbox access token.
|
| 1526 |
+
Create a token in your mapbox account and then set it using:
|
| 1527 |
+
|
| 1528 |
+
>>> plotly.io.orca.config.mapbox_access_token = 'pk.abc...'
|
| 1529 |
+
|
| 1530 |
+
If you would like this token to be applied automatically in
|
| 1531 |
+
future sessions, then save your orca configuration as follows:
|
| 1532 |
+
|
| 1533 |
+
>>> plotly.io.orca.config.save()
|
| 1534 |
+
"""
|
| 1535 |
+
elif response.status_code == 530 and format == "eps":
|
| 1536 |
+
err_message += """
|
| 1537 |
+
Exporting to EPS format requires the poppler library. You can install
|
| 1538 |
+
poppler on MacOS or Linux with:
|
| 1539 |
+
|
| 1540 |
+
$ conda install poppler
|
| 1541 |
+
|
| 1542 |
+
Or, you can install it on MacOS using homebrew with:
|
| 1543 |
+
|
| 1544 |
+
$ brew install poppler
|
| 1545 |
+
|
| 1546 |
+
Or, you can install it on Linux using your distribution's package manager to
|
| 1547 |
+
install the 'poppler-utils' package.
|
| 1548 |
+
|
| 1549 |
+
Unfortunately, we don't yet know of an easy way to install poppler on Windows.
|
| 1550 |
+
"""
|
| 1551 |
+
raise ValueError(err_message)
|
| 1552 |
+
|
| 1553 |
+
|
| 1554 |
+
def write_image(
|
| 1555 |
+
fig, file, format=None, scale=None, width=None, height=None, validate=True
|
| 1556 |
+
):
|
| 1557 |
+
"""
|
| 1558 |
+
Convert a figure to a static image and write it to a file or writeable
|
| 1559 |
+
object
|
| 1560 |
+
|
| 1561 |
+
Parameters
|
| 1562 |
+
----------
|
| 1563 |
+
fig:
|
| 1564 |
+
Figure object or dict representing a figure
|
| 1565 |
+
|
| 1566 |
+
file: str or writeable
|
| 1567 |
+
A string representing a local file path or a writeable object
|
| 1568 |
+
(e.g. a pathlib.Path object or an open file descriptor)
|
| 1569 |
+
|
| 1570 |
+
format: str or None
|
| 1571 |
+
The desired image format. One of
|
| 1572 |
+
- 'png'
|
| 1573 |
+
- 'jpg' or 'jpeg'
|
| 1574 |
+
- 'webp'
|
| 1575 |
+
- 'svg'
|
| 1576 |
+
- 'pdf'
|
| 1577 |
+
- 'eps' (Requires the poppler library to be installed)
|
| 1578 |
+
|
| 1579 |
+
If not specified and `file` is a string then this will default to the
|
| 1580 |
+
file extension. If not specified and `file` is not a string then this
|
| 1581 |
+
will default to `plotly.io.config.default_format`
|
| 1582 |
+
|
| 1583 |
+
width: int or None
|
| 1584 |
+
The width of the exported image in layout pixels. If the `scale`
|
| 1585 |
+
property is 1.0, this will also be the width of the exported image
|
| 1586 |
+
in physical pixels.
|
| 1587 |
+
|
| 1588 |
+
If not specified, will default to `plotly.io.config.default_width`
|
| 1589 |
+
|
| 1590 |
+
height: int or None
|
| 1591 |
+
The height of the exported image in layout pixels. If the `scale`
|
| 1592 |
+
property is 1.0, this will also be the height of the exported image
|
| 1593 |
+
in physical pixels.
|
| 1594 |
+
|
| 1595 |
+
If not specified, will default to `plotly.io.config.default_height`
|
| 1596 |
+
|
| 1597 |
+
scale: int or float or None
|
| 1598 |
+
The scale factor to use when exporting the figure. A scale factor
|
| 1599 |
+
larger than 1.0 will increase the image resolution with respect
|
| 1600 |
+
to the figure's layout pixel dimensions. Whereas as scale factor of
|
| 1601 |
+
less than 1.0 will decrease the image resolution.
|
| 1602 |
+
|
| 1603 |
+
If not specified, will default to `plotly.io.config.default_scale`
|
| 1604 |
+
|
| 1605 |
+
validate: bool
|
| 1606 |
+
True if the figure should be validated before being converted to
|
| 1607 |
+
an image, False otherwise.
|
| 1608 |
+
|
| 1609 |
+
Returns
|
| 1610 |
+
-------
|
| 1611 |
+
None
|
| 1612 |
+
"""
|
| 1613 |
+
|
| 1614 |
+
# Try to cast `file` as a pathlib object `path`.
|
| 1615 |
+
# ----------------------------------------------
|
| 1616 |
+
if isinstance(file, str):
|
| 1617 |
+
# Use the standard Path constructor to make a pathlib object.
|
| 1618 |
+
path = Path(file)
|
| 1619 |
+
elif isinstance(file, Path):
|
| 1620 |
+
# `file` is already a Path object.
|
| 1621 |
+
path = file
|
| 1622 |
+
else:
|
| 1623 |
+
# We could not make a Path object out of file. Either `file` is an open file
|
| 1624 |
+
# descriptor with a `write()` method or it's an invalid object.
|
| 1625 |
+
path = None
|
| 1626 |
+
|
| 1627 |
+
# Infer format if not specified
|
| 1628 |
+
# -----------------------------
|
| 1629 |
+
if path is not None and format is None:
|
| 1630 |
+
ext = path.suffix
|
| 1631 |
+
if ext:
|
| 1632 |
+
format = ext.lstrip(".")
|
| 1633 |
+
else:
|
| 1634 |
+
raise ValueError(
|
| 1635 |
+
"""
|
| 1636 |
+
Cannot infer image type from output path '{file}'.
|
| 1637 |
+
Please add a file extension or specify the type using the format parameter.
|
| 1638 |
+
For example:
|
| 1639 |
+
|
| 1640 |
+
>>> import plotly.io as pio
|
| 1641 |
+
>>> pio.write_image(fig, file_path, format='png')
|
| 1642 |
+
""".format(file=file)
|
| 1643 |
+
)
|
| 1644 |
+
|
| 1645 |
+
# Request image
|
| 1646 |
+
# -------------
|
| 1647 |
+
# Do this first so we don't create a file if image conversion fails
|
| 1648 |
+
img_data = to_image(
|
| 1649 |
+
fig, format=format, scale=scale, width=width, height=height, validate=validate
|
| 1650 |
+
)
|
| 1651 |
+
|
| 1652 |
+
# Open file
|
| 1653 |
+
# ---------
|
| 1654 |
+
if path is None:
|
| 1655 |
+
# We previously failed to make sense of `file` as a pathlib object.
|
| 1656 |
+
# Attempt to write to `file` as an open file descriptor.
|
| 1657 |
+
try:
|
| 1658 |
+
file.write(img_data)
|
| 1659 |
+
return
|
| 1660 |
+
except AttributeError:
|
| 1661 |
+
pass
|
| 1662 |
+
raise ValueError(
|
| 1663 |
+
"""
|
| 1664 |
+
The 'file' argument '{file}' is not a string, pathlib.Path object, or file descriptor.
|
| 1665 |
+
""".format(file=file)
|
| 1666 |
+
)
|
| 1667 |
+
else:
|
| 1668 |
+
# We previously succeeded in interpreting `file` as a pathlib object.
|
| 1669 |
+
# Now we can use `write_bytes()`.
|
| 1670 |
+
path.write_bytes(img_data)
|
.venv_haddock/lib/python3.12/site-packages/plotly/io/_renderers.py
ADDED
|
@@ -0,0 +1,567 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import textwrap
|
| 2 |
+
from copy import copy
|
| 3 |
+
import os
|
| 4 |
+
from packaging.version import Version
|
| 5 |
+
import warnings
|
| 6 |
+
|
| 7 |
+
from plotly import optional_imports
|
| 8 |
+
|
| 9 |
+
from plotly.io._base_renderers import (
|
| 10 |
+
MimetypeRenderer,
|
| 11 |
+
ExternalRenderer,
|
| 12 |
+
PlotlyRenderer,
|
| 13 |
+
NotebookRenderer,
|
| 14 |
+
KaggleRenderer,
|
| 15 |
+
AzureRenderer,
|
| 16 |
+
ColabRenderer,
|
| 17 |
+
JsonRenderer,
|
| 18 |
+
PngRenderer,
|
| 19 |
+
JpegRenderer,
|
| 20 |
+
SvgRenderer,
|
| 21 |
+
PdfRenderer,
|
| 22 |
+
BrowserRenderer,
|
| 23 |
+
IFrameRenderer,
|
| 24 |
+
SphinxGalleryHtmlRenderer,
|
| 25 |
+
SphinxGalleryOrcaRenderer,
|
| 26 |
+
CoCalcRenderer,
|
| 27 |
+
DatabricksRenderer,
|
| 28 |
+
)
|
| 29 |
+
from plotly.io._utils import validate_coerce_fig_to_dict
|
| 30 |
+
|
| 31 |
+
ipython = optional_imports.get_module("IPython")
|
| 32 |
+
ipython_display = optional_imports.get_module("IPython.display")
|
| 33 |
+
nbformat = optional_imports.get_module("nbformat")
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
def display_jupyter_version_warnings():
|
| 37 |
+
parent_process = None
|
| 38 |
+
try:
|
| 39 |
+
psutil = optional_imports.get_module("psutil")
|
| 40 |
+
if psutil is not None:
|
| 41 |
+
parent_process = psutil.Process().parent().cmdline()[-1]
|
| 42 |
+
except Exception:
|
| 43 |
+
pass
|
| 44 |
+
|
| 45 |
+
if parent_process is None:
|
| 46 |
+
return
|
| 47 |
+
elif "jupyter-notebook" in parent_process:
|
| 48 |
+
jupyter_notebook = optional_imports.get_module("notebook")
|
| 49 |
+
if jupyter_notebook is not None and jupyter_notebook.__version__ < "7":
|
| 50 |
+
# Add warning about upgrading notebook
|
| 51 |
+
warnings.warn(
|
| 52 |
+
f"Plotly version >= 6 requires Jupyter Notebook >= 7 but you have {jupyter_notebook.__version__} installed.\n To upgrade Jupyter Notebook, please run `pip install notebook --upgrade`."
|
| 53 |
+
)
|
| 54 |
+
elif "jupyter-lab" in parent_process:
|
| 55 |
+
jupyter_lab = optional_imports.get_module("jupyterlab")
|
| 56 |
+
if jupyter_lab is not None and jupyter_lab.__version__ < "3":
|
| 57 |
+
# Add warning about upgrading jupyterlab
|
| 58 |
+
warnings.warn(
|
| 59 |
+
f"Plotly version >= 6 requires JupyterLab >= 3 but you have {jupyter_lab.__version__} installed. To upgrade JupyterLab, please run `pip install jupyterlab --upgrade`."
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
# Renderer configuration class
|
| 64 |
+
# -----------------------------
|
| 65 |
+
class RenderersConfig(object):
|
| 66 |
+
"""
|
| 67 |
+
Singleton object containing the current renderer configurations
|
| 68 |
+
"""
|
| 69 |
+
|
| 70 |
+
def __init__(self):
|
| 71 |
+
self._renderers = {}
|
| 72 |
+
self._default_name = None
|
| 73 |
+
self._default_renderers = []
|
| 74 |
+
self._render_on_display = False
|
| 75 |
+
self._to_activate = []
|
| 76 |
+
|
| 77 |
+
# ### Magic methods ###
|
| 78 |
+
# Make this act as a dict of renderers
|
| 79 |
+
def __len__(self):
|
| 80 |
+
return len(self._renderers)
|
| 81 |
+
|
| 82 |
+
def __contains__(self, item):
|
| 83 |
+
return item in self._renderers
|
| 84 |
+
|
| 85 |
+
def __iter__(self):
|
| 86 |
+
return iter(self._renderers)
|
| 87 |
+
|
| 88 |
+
def __getitem__(self, item):
|
| 89 |
+
renderer = self._renderers[item]
|
| 90 |
+
return renderer
|
| 91 |
+
|
| 92 |
+
def __setitem__(self, key, value):
|
| 93 |
+
if not isinstance(value, (MimetypeRenderer, ExternalRenderer)):
|
| 94 |
+
raise ValueError(
|
| 95 |
+
"""\
|
| 96 |
+
Renderer must be a subclass of MimetypeRenderer or ExternalRenderer.
|
| 97 |
+
Received value with type: {typ}""".format(typ=type(value))
|
| 98 |
+
)
|
| 99 |
+
|
| 100 |
+
self._renderers[key] = value
|
| 101 |
+
|
| 102 |
+
def __delitem__(self, key):
|
| 103 |
+
# Remove template
|
| 104 |
+
del self._renderers[key]
|
| 105 |
+
|
| 106 |
+
# Check if we need to remove it as the default
|
| 107 |
+
if self._default == key:
|
| 108 |
+
self._default = None
|
| 109 |
+
|
| 110 |
+
def keys(self):
|
| 111 |
+
return self._renderers.keys()
|
| 112 |
+
|
| 113 |
+
def items(self):
|
| 114 |
+
return self._renderers.items()
|
| 115 |
+
|
| 116 |
+
def update(self, d={}, **kwargs):
|
| 117 |
+
"""
|
| 118 |
+
Update one or more renderers from a dict or from input keyword
|
| 119 |
+
arguments.
|
| 120 |
+
|
| 121 |
+
Parameters
|
| 122 |
+
----------
|
| 123 |
+
d: dict
|
| 124 |
+
Dictionary from renderer names to new renderer objects.
|
| 125 |
+
|
| 126 |
+
kwargs
|
| 127 |
+
Named argument value pairs where the name is a renderer name
|
| 128 |
+
and the value is a new renderer object
|
| 129 |
+
"""
|
| 130 |
+
for k, v in dict(d, **kwargs).items():
|
| 131 |
+
self[k] = v
|
| 132 |
+
|
| 133 |
+
# ### Properties ###
|
| 134 |
+
@property
|
| 135 |
+
def default(self):
|
| 136 |
+
"""
|
| 137 |
+
The default renderer, or None if no there is no default
|
| 138 |
+
|
| 139 |
+
If not None, the default renderer is used to render
|
| 140 |
+
figures when the `plotly.io.show` function is called on a Figure.
|
| 141 |
+
|
| 142 |
+
If `plotly.io.renderers.render_on_display` is True, then the default
|
| 143 |
+
renderer will also be used to display Figures automatically when
|
| 144 |
+
displayed in the Jupyter Notebook
|
| 145 |
+
|
| 146 |
+
Multiple renderers may be registered by separating their names with
|
| 147 |
+
'+' characters. For example, to specify rendering compatible with
|
| 148 |
+
the classic Jupyter Notebook, JupyterLab, and PDF export:
|
| 149 |
+
|
| 150 |
+
>>> import plotly.io as pio
|
| 151 |
+
>>> pio.renderers.default = 'notebook+jupyterlab+pdf'
|
| 152 |
+
|
| 153 |
+
The names of available renderers may be retrieved with:
|
| 154 |
+
|
| 155 |
+
>>> import plotly.io as pio
|
| 156 |
+
>>> list(pio.renderers)
|
| 157 |
+
|
| 158 |
+
Returns
|
| 159 |
+
-------
|
| 160 |
+
str
|
| 161 |
+
"""
|
| 162 |
+
return self._default_name
|
| 163 |
+
|
| 164 |
+
@default.setter
|
| 165 |
+
def default(self, value):
|
| 166 |
+
# Handle None
|
| 167 |
+
if not value:
|
| 168 |
+
# _default_name should always be a string so we can do
|
| 169 |
+
# pio.renderers.default.split('+')
|
| 170 |
+
self._default_name = ""
|
| 171 |
+
self._default_renderers = []
|
| 172 |
+
return
|
| 173 |
+
|
| 174 |
+
# Store defaults name and list of renderer(s)
|
| 175 |
+
renderer_names = self._validate_coerce_renderers(value)
|
| 176 |
+
self._default_name = value
|
| 177 |
+
self._default_renderers = [self[name] for name in renderer_names]
|
| 178 |
+
|
| 179 |
+
# Register renderers for activation before their next use
|
| 180 |
+
self._to_activate = list(self._default_renderers)
|
| 181 |
+
|
| 182 |
+
@property
|
| 183 |
+
def render_on_display(self):
|
| 184 |
+
"""
|
| 185 |
+
If True, the default mimetype renderers will be used to render
|
| 186 |
+
figures when they are displayed in an IPython context.
|
| 187 |
+
|
| 188 |
+
Returns
|
| 189 |
+
-------
|
| 190 |
+
bool
|
| 191 |
+
"""
|
| 192 |
+
return self._render_on_display
|
| 193 |
+
|
| 194 |
+
@render_on_display.setter
|
| 195 |
+
def render_on_display(self, val):
|
| 196 |
+
self._render_on_display = bool(val)
|
| 197 |
+
|
| 198 |
+
def _activate_pending_renderers(self, cls=object):
|
| 199 |
+
"""
|
| 200 |
+
Activate all renderers that are waiting in the _to_activate list
|
| 201 |
+
|
| 202 |
+
Parameters
|
| 203 |
+
----------
|
| 204 |
+
cls
|
| 205 |
+
Only activate renders that are subclasses of this class
|
| 206 |
+
"""
|
| 207 |
+
to_activate_with_cls = [
|
| 208 |
+
r for r in self._to_activate if cls and isinstance(r, cls)
|
| 209 |
+
]
|
| 210 |
+
|
| 211 |
+
while to_activate_with_cls:
|
| 212 |
+
# Activate renderers from left to right so that right-most
|
| 213 |
+
# renderers take precedence
|
| 214 |
+
renderer = to_activate_with_cls.pop(0)
|
| 215 |
+
renderer.activate()
|
| 216 |
+
|
| 217 |
+
self._to_activate = [
|
| 218 |
+
r for r in self._to_activate if not (cls and isinstance(r, cls))
|
| 219 |
+
]
|
| 220 |
+
|
| 221 |
+
def _validate_coerce_renderers(self, renderers_string):
|
| 222 |
+
"""
|
| 223 |
+
Input a string and validate that it contains the names of one or more
|
| 224 |
+
valid renderers separated on '+' characters. If valid, return
|
| 225 |
+
a list of the renderer names
|
| 226 |
+
|
| 227 |
+
Parameters
|
| 228 |
+
----------
|
| 229 |
+
renderers_string: str
|
| 230 |
+
|
| 231 |
+
Returns
|
| 232 |
+
-------
|
| 233 |
+
list of str
|
| 234 |
+
"""
|
| 235 |
+
# Validate value
|
| 236 |
+
if not isinstance(renderers_string, str):
|
| 237 |
+
raise ValueError("Renderer must be specified as a string")
|
| 238 |
+
|
| 239 |
+
renderer_names = renderers_string.split("+")
|
| 240 |
+
invalid = [name for name in renderer_names if name not in self]
|
| 241 |
+
if invalid:
|
| 242 |
+
raise ValueError(
|
| 243 |
+
"""
|
| 244 |
+
Invalid named renderer(s) received: {}""".format(str(invalid))
|
| 245 |
+
)
|
| 246 |
+
|
| 247 |
+
return renderer_names
|
| 248 |
+
|
| 249 |
+
def __repr__(self):
|
| 250 |
+
return """\
|
| 251 |
+
Renderers configuration
|
| 252 |
+
-----------------------
|
| 253 |
+
Default renderer: {default}
|
| 254 |
+
Available renderers:
|
| 255 |
+
{available}
|
| 256 |
+
""".format(default=repr(self.default), available=self._available_renderers_str())
|
| 257 |
+
|
| 258 |
+
def _available_renderers_str(self):
|
| 259 |
+
"""
|
| 260 |
+
Return nicely wrapped string representation of all
|
| 261 |
+
available renderer names
|
| 262 |
+
"""
|
| 263 |
+
available = "\n".join(
|
| 264 |
+
textwrap.wrap(
|
| 265 |
+
repr(list(self)),
|
| 266 |
+
width=79 - 8,
|
| 267 |
+
initial_indent=" " * 8,
|
| 268 |
+
subsequent_indent=" " * 9,
|
| 269 |
+
)
|
| 270 |
+
)
|
| 271 |
+
return available
|
| 272 |
+
|
| 273 |
+
def _build_mime_bundle(self, fig_dict, renderers_string=None, **kwargs):
|
| 274 |
+
"""
|
| 275 |
+
Build a mime bundle dict containing a kev/value pair for each
|
| 276 |
+
MimetypeRenderer specified in either the default renderer string,
|
| 277 |
+
or in the supplied renderers_string argument.
|
| 278 |
+
|
| 279 |
+
Note that this method skips any renderers that are not subclasses
|
| 280 |
+
of MimetypeRenderer.
|
| 281 |
+
|
| 282 |
+
Parameters
|
| 283 |
+
----------
|
| 284 |
+
fig_dict: dict
|
| 285 |
+
Figure dictionary
|
| 286 |
+
renderers_string: str or None (default None)
|
| 287 |
+
Renderer string to process rather than the current default
|
| 288 |
+
renderer string
|
| 289 |
+
|
| 290 |
+
Returns
|
| 291 |
+
-------
|
| 292 |
+
dict
|
| 293 |
+
"""
|
| 294 |
+
if renderers_string:
|
| 295 |
+
renderer_names = self._validate_coerce_renderers(renderers_string)
|
| 296 |
+
renderers_list = [self[name] for name in renderer_names]
|
| 297 |
+
|
| 298 |
+
# Activate these non-default renderers
|
| 299 |
+
for renderer in renderers_list:
|
| 300 |
+
if isinstance(renderer, MimetypeRenderer):
|
| 301 |
+
renderer.activate()
|
| 302 |
+
else:
|
| 303 |
+
# Activate any pending default renderers
|
| 304 |
+
self._activate_pending_renderers(cls=MimetypeRenderer)
|
| 305 |
+
renderers_list = self._default_renderers
|
| 306 |
+
|
| 307 |
+
bundle = {}
|
| 308 |
+
for renderer in renderers_list:
|
| 309 |
+
if isinstance(renderer, MimetypeRenderer):
|
| 310 |
+
renderer = copy(renderer)
|
| 311 |
+
for k, v in kwargs.items():
|
| 312 |
+
if hasattr(renderer, k):
|
| 313 |
+
setattr(renderer, k, v)
|
| 314 |
+
|
| 315 |
+
bundle.update(renderer.to_mimebundle(fig_dict))
|
| 316 |
+
|
| 317 |
+
return bundle
|
| 318 |
+
|
| 319 |
+
def _perform_external_rendering(self, fig_dict, renderers_string=None, **kwargs):
|
| 320 |
+
"""
|
| 321 |
+
Perform external rendering for each ExternalRenderer specified
|
| 322 |
+
in either the default renderer string, or in the supplied
|
| 323 |
+
renderers_string argument.
|
| 324 |
+
|
| 325 |
+
Note that this method skips any renderers that are not subclasses
|
| 326 |
+
of ExternalRenderer.
|
| 327 |
+
|
| 328 |
+
Parameters
|
| 329 |
+
----------
|
| 330 |
+
fig_dict: dict
|
| 331 |
+
Figure dictionary
|
| 332 |
+
renderers_string: str or None (default None)
|
| 333 |
+
Renderer string to process rather than the current default
|
| 334 |
+
renderer string
|
| 335 |
+
|
| 336 |
+
Returns
|
| 337 |
+
-------
|
| 338 |
+
None
|
| 339 |
+
"""
|
| 340 |
+
if renderers_string:
|
| 341 |
+
renderer_names = self._validate_coerce_renderers(renderers_string)
|
| 342 |
+
renderers_list = [self[name] for name in renderer_names]
|
| 343 |
+
|
| 344 |
+
# Activate these non-default renderers
|
| 345 |
+
for renderer in renderers_list:
|
| 346 |
+
if isinstance(renderer, ExternalRenderer):
|
| 347 |
+
renderer.activate()
|
| 348 |
+
else:
|
| 349 |
+
self._activate_pending_renderers(cls=ExternalRenderer)
|
| 350 |
+
renderers_list = self._default_renderers
|
| 351 |
+
|
| 352 |
+
for renderer in renderers_list:
|
| 353 |
+
if isinstance(renderer, ExternalRenderer):
|
| 354 |
+
renderer = copy(renderer)
|
| 355 |
+
for k, v in kwargs.items():
|
| 356 |
+
if hasattr(renderer, k):
|
| 357 |
+
setattr(renderer, k, v)
|
| 358 |
+
|
| 359 |
+
renderer.render(fig_dict)
|
| 360 |
+
|
| 361 |
+
|
| 362 |
+
# Make renderers a singleton object
|
| 363 |
+
# ---------------------------------
|
| 364 |
+
renderers = RenderersConfig()
|
| 365 |
+
del RenderersConfig
|
| 366 |
+
|
| 367 |
+
|
| 368 |
+
# Show
|
| 369 |
+
def show(fig, renderer=None, validate=True, **kwargs):
|
| 370 |
+
"""
|
| 371 |
+
Show a figure using either the default renderer(s) or the renderer(s)
|
| 372 |
+
specified by the renderer argument
|
| 373 |
+
|
| 374 |
+
Parameters
|
| 375 |
+
----------
|
| 376 |
+
fig: dict of Figure
|
| 377 |
+
The Figure object or figure dict to display
|
| 378 |
+
|
| 379 |
+
renderer: str or None (default None)
|
| 380 |
+
A string containing the names of one or more registered renderers
|
| 381 |
+
(separated by '+' characters) or None. If None, then the default
|
| 382 |
+
renderers specified in plotly.io.renderers.default are used.
|
| 383 |
+
|
| 384 |
+
validate: bool (default True)
|
| 385 |
+
True if the figure should be validated before being shown,
|
| 386 |
+
False otherwise.
|
| 387 |
+
|
| 388 |
+
width: int or float
|
| 389 |
+
An integer or float that determines the number of pixels wide the
|
| 390 |
+
plot is. The default is set in plotly.js.
|
| 391 |
+
|
| 392 |
+
height: int or float
|
| 393 |
+
An integer or float specifying the height of the plot in pixels.
|
| 394 |
+
The default is set in plotly.js.
|
| 395 |
+
|
| 396 |
+
config: dict
|
| 397 |
+
A dict of parameters to configure the figure. The defaults are set
|
| 398 |
+
in plotly.js.
|
| 399 |
+
|
| 400 |
+
Returns
|
| 401 |
+
-------
|
| 402 |
+
None
|
| 403 |
+
"""
|
| 404 |
+
fig_dict = validate_coerce_fig_to_dict(fig, validate)
|
| 405 |
+
|
| 406 |
+
# Mimetype renderers
|
| 407 |
+
bundle = renderers._build_mime_bundle(fig_dict, renderers_string=renderer, **kwargs)
|
| 408 |
+
if bundle:
|
| 409 |
+
if not ipython_display:
|
| 410 |
+
raise ValueError(
|
| 411 |
+
"Mime type rendering requires ipython but it is not installed"
|
| 412 |
+
)
|
| 413 |
+
|
| 414 |
+
if not nbformat or Version(nbformat.__version__) < Version("4.2.0"):
|
| 415 |
+
raise ValueError(
|
| 416 |
+
"Mime type rendering requires nbformat>=4.2.0 but it is not installed"
|
| 417 |
+
)
|
| 418 |
+
|
| 419 |
+
display_jupyter_version_warnings()
|
| 420 |
+
|
| 421 |
+
ipython_display.display(bundle, raw=True)
|
| 422 |
+
|
| 423 |
+
# external renderers
|
| 424 |
+
renderers._perform_external_rendering(fig_dict, renderers_string=renderer, **kwargs)
|
| 425 |
+
|
| 426 |
+
|
| 427 |
+
# Register renderers
|
| 428 |
+
# ------------------
|
| 429 |
+
|
| 430 |
+
# Plotly mime type
|
| 431 |
+
plotly_renderer = PlotlyRenderer()
|
| 432 |
+
renderers["plotly_mimetype"] = plotly_renderer
|
| 433 |
+
renderers["jupyterlab"] = plotly_renderer
|
| 434 |
+
renderers["nteract"] = plotly_renderer
|
| 435 |
+
renderers["vscode"] = plotly_renderer
|
| 436 |
+
|
| 437 |
+
# HTML-based
|
| 438 |
+
config = {}
|
| 439 |
+
renderers["notebook"] = NotebookRenderer(config=config)
|
| 440 |
+
renderers["notebook_connected"] = NotebookRenderer(config=config, connected=True)
|
| 441 |
+
renderers["kaggle"] = KaggleRenderer(config=config)
|
| 442 |
+
renderers["azure"] = AzureRenderer(config=config)
|
| 443 |
+
renderers["colab"] = ColabRenderer(config=config)
|
| 444 |
+
renderers["cocalc"] = CoCalcRenderer()
|
| 445 |
+
renderers["databricks"] = DatabricksRenderer()
|
| 446 |
+
|
| 447 |
+
# JSON
|
| 448 |
+
renderers["json"] = JsonRenderer()
|
| 449 |
+
|
| 450 |
+
# Static Image
|
| 451 |
+
renderers["png"] = PngRenderer()
|
| 452 |
+
jpeg_renderer = JpegRenderer()
|
| 453 |
+
renderers["jpeg"] = jpeg_renderer
|
| 454 |
+
renderers["jpg"] = jpeg_renderer
|
| 455 |
+
renderers["svg"] = SvgRenderer()
|
| 456 |
+
renderers["pdf"] = PdfRenderer()
|
| 457 |
+
|
| 458 |
+
# External
|
| 459 |
+
renderers["browser"] = BrowserRenderer(config=config)
|
| 460 |
+
renderers["firefox"] = BrowserRenderer(config=config, using=("firefox"))
|
| 461 |
+
renderers["chrome"] = BrowserRenderer(config=config, using=("chrome", "google-chrome"))
|
| 462 |
+
renderers["chromium"] = BrowserRenderer(
|
| 463 |
+
config=config, using=("chromium", "chromium-browser")
|
| 464 |
+
)
|
| 465 |
+
renderers["iframe"] = IFrameRenderer(config=config, include_plotlyjs=True)
|
| 466 |
+
renderers["iframe_connected"] = IFrameRenderer(config=config, include_plotlyjs="cdn")
|
| 467 |
+
renderers["sphinx_gallery"] = SphinxGalleryHtmlRenderer()
|
| 468 |
+
renderers["sphinx_gallery_png"] = SphinxGalleryOrcaRenderer()
|
| 469 |
+
|
| 470 |
+
# Set default renderer
|
| 471 |
+
# --------------------
|
| 472 |
+
# Version 4 renderer configuration
|
| 473 |
+
default_renderer = None
|
| 474 |
+
|
| 475 |
+
# Handle the PLOTLY_RENDERER environment variable
|
| 476 |
+
env_renderer = os.environ.get("PLOTLY_RENDERER", None)
|
| 477 |
+
if env_renderer:
|
| 478 |
+
try:
|
| 479 |
+
renderers._validate_coerce_renderers(env_renderer)
|
| 480 |
+
except ValueError:
|
| 481 |
+
raise ValueError(
|
| 482 |
+
"""
|
| 483 |
+
Invalid named renderer(s) specified in the 'PLOTLY_RENDERER'
|
| 484 |
+
environment variable: {env_renderer}""".format(env_renderer=env_renderer)
|
| 485 |
+
)
|
| 486 |
+
|
| 487 |
+
default_renderer = env_renderer
|
| 488 |
+
elif ipython and ipython.get_ipython():
|
| 489 |
+
# Try to detect environment so that we can enable a useful
|
| 490 |
+
# default renderer
|
| 491 |
+
if not default_renderer:
|
| 492 |
+
try:
|
| 493 |
+
import google.colab # noqa: F401
|
| 494 |
+
|
| 495 |
+
default_renderer = "colab"
|
| 496 |
+
except ImportError:
|
| 497 |
+
pass
|
| 498 |
+
|
| 499 |
+
# Check if we're running in a Kaggle notebook
|
| 500 |
+
if not default_renderer and os.path.exists("/kaggle/input"):
|
| 501 |
+
default_renderer = "kaggle"
|
| 502 |
+
|
| 503 |
+
# Check if we're running in an Azure Notebook
|
| 504 |
+
if not default_renderer and "AZURE_NOTEBOOKS_HOST" in os.environ:
|
| 505 |
+
default_renderer = "azure"
|
| 506 |
+
|
| 507 |
+
# Check if we're running in VSCode
|
| 508 |
+
if not default_renderer and "VSCODE_PID" in os.environ:
|
| 509 |
+
default_renderer = "vscode"
|
| 510 |
+
|
| 511 |
+
# Check if we're running in nteract
|
| 512 |
+
if not default_renderer and "NTERACT_EXE" in os.environ:
|
| 513 |
+
default_renderer = "nteract"
|
| 514 |
+
|
| 515 |
+
# Check if we're running in CoCalc
|
| 516 |
+
if not default_renderer and "COCALC_PROJECT_ID" in os.environ:
|
| 517 |
+
default_renderer = "cocalc"
|
| 518 |
+
|
| 519 |
+
if not default_renderer and "DATABRICKS_RUNTIME_VERSION" in os.environ:
|
| 520 |
+
default_renderer = "databricks"
|
| 521 |
+
|
| 522 |
+
# Check if we're running in spyder and orca is installed
|
| 523 |
+
if not default_renderer and "SPYDER_ARGS" in os.environ:
|
| 524 |
+
try:
|
| 525 |
+
from plotly.io.orca import validate_executable
|
| 526 |
+
|
| 527 |
+
validate_executable()
|
| 528 |
+
default_renderer = "svg"
|
| 529 |
+
except ValueError:
|
| 530 |
+
# orca not found
|
| 531 |
+
pass
|
| 532 |
+
|
| 533 |
+
# Check if we're running in ipython terminal
|
| 534 |
+
ipython_info = ipython.get_ipython()
|
| 535 |
+
shell = ipython_info.__class__.__name__
|
| 536 |
+
if not default_renderer and (shell == "TerminalInteractiveShell"):
|
| 537 |
+
default_renderer = "browser"
|
| 538 |
+
|
| 539 |
+
# Check if we're running in a Jupyter notebook or JupyterLab
|
| 540 |
+
if (
|
| 541 |
+
not default_renderer
|
| 542 |
+
and (shell == "ZMQInteractiveShell")
|
| 543 |
+
and (type(ipython_info).__module__.startswith("ipykernel."))
|
| 544 |
+
):
|
| 545 |
+
default_renderer = "plotly_mimetype"
|
| 546 |
+
|
| 547 |
+
# Fallback to renderer combination that will work automatically
|
| 548 |
+
# in the jupyter notebook, jupyterlab, nteract, vscode, and
|
| 549 |
+
# nbconvert HTML export.
|
| 550 |
+
if not default_renderer:
|
| 551 |
+
default_renderer = "plotly_mimetype+notebook"
|
| 552 |
+
else:
|
| 553 |
+
# If ipython isn't available, try to display figures in the default
|
| 554 |
+
# browser
|
| 555 |
+
try:
|
| 556 |
+
import webbrowser
|
| 557 |
+
|
| 558 |
+
webbrowser.get()
|
| 559 |
+
default_renderer = "browser"
|
| 560 |
+
except Exception:
|
| 561 |
+
# Many things could have gone wrong
|
| 562 |
+
# There could not be a webbrowser Python module,
|
| 563 |
+
# or the module may be a dumb placeholder
|
| 564 |
+
pass
|
| 565 |
+
|
| 566 |
+
renderers.render_on_display = True
|
| 567 |
+
renderers.default = default_renderer
|
.venv_haddock/lib/python3.12/site-packages/plotly/io/_sg_scraper.py
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# This module defines an image scraper for sphinx-gallery
|
| 2 |
+
# https://sphinx-gallery.github.io/
|
| 3 |
+
# which can be used by projects using plotly in their documentation.
|
| 4 |
+
from glob import glob
|
| 5 |
+
import os
|
| 6 |
+
import shutil
|
| 7 |
+
|
| 8 |
+
import plotly
|
| 9 |
+
|
| 10 |
+
plotly.io.renderers.default = "sphinx_gallery_png"
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def plotly_sg_scraper(block, block_vars, gallery_conf, **kwargs):
|
| 14 |
+
"""Scrape Plotly figures for galleries of examples using
|
| 15 |
+
sphinx-gallery.
|
| 16 |
+
|
| 17 |
+
Examples should use ``plotly.io.show()`` to display the figure with
|
| 18 |
+
the custom sphinx_gallery renderer.
|
| 19 |
+
|
| 20 |
+
Since the sphinx_gallery renderer generates both html and static png
|
| 21 |
+
files, we simply crawl these files and give them the appropriate path.
|
| 22 |
+
|
| 23 |
+
Parameters
|
| 24 |
+
----------
|
| 25 |
+
block : tuple
|
| 26 |
+
A tuple containing the (label, content, line_number) of the block.
|
| 27 |
+
block_vars : dict
|
| 28 |
+
Dict of block variables.
|
| 29 |
+
gallery_conf : dict
|
| 30 |
+
Contains the configuration of Sphinx-Gallery
|
| 31 |
+
**kwargs : dict
|
| 32 |
+
Additional keyword arguments to pass to
|
| 33 |
+
:meth:`~matplotlib.figure.Figure.savefig`, e.g. ``format='svg'``.
|
| 34 |
+
The ``format`` kwarg in particular is used to set the file extension
|
| 35 |
+
of the output file (currently only 'png' and 'svg' are supported).
|
| 36 |
+
|
| 37 |
+
Returns
|
| 38 |
+
-------
|
| 39 |
+
rst : str
|
| 40 |
+
The ReSTructuredText that will be rendered to HTML containing
|
| 41 |
+
the images.
|
| 42 |
+
|
| 43 |
+
Notes
|
| 44 |
+
-----
|
| 45 |
+
Add this function to the image scrapers
|
| 46 |
+
"""
|
| 47 |
+
examples_dir = os.path.dirname(block_vars["src_file"])
|
| 48 |
+
pngs = sorted(glob(os.path.join(examples_dir, "*.png")))
|
| 49 |
+
htmls = sorted(glob(os.path.join(examples_dir, "*.html")))
|
| 50 |
+
image_path_iterator = block_vars["image_path_iterator"]
|
| 51 |
+
image_names = list()
|
| 52 |
+
seen = set()
|
| 53 |
+
for html, png in zip(htmls, pngs):
|
| 54 |
+
if png not in seen:
|
| 55 |
+
seen |= set(png)
|
| 56 |
+
this_image_path_png = next(image_path_iterator)
|
| 57 |
+
this_image_path_html = os.path.splitext(this_image_path_png)[0] + ".html"
|
| 58 |
+
image_names.append(this_image_path_html)
|
| 59 |
+
shutil.move(png, this_image_path_png)
|
| 60 |
+
shutil.move(html, this_image_path_html)
|
| 61 |
+
# Use the `figure_rst` helper function to generate rST for image files
|
| 62 |
+
return figure_rst(image_names, gallery_conf["src_dir"])
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
def figure_rst(figure_list, sources_dir):
|
| 66 |
+
"""Generate RST for a list of PNG filenames.
|
| 67 |
+
|
| 68 |
+
Depending on whether we have one or more figures, we use a
|
| 69 |
+
single rst call to 'image' or a horizontal list.
|
| 70 |
+
|
| 71 |
+
Parameters
|
| 72 |
+
----------
|
| 73 |
+
figure_list : list
|
| 74 |
+
List of strings of the figures' absolute paths.
|
| 75 |
+
sources_dir : str
|
| 76 |
+
absolute path of Sphinx documentation sources
|
| 77 |
+
|
| 78 |
+
Returns
|
| 79 |
+
-------
|
| 80 |
+
images_rst : str
|
| 81 |
+
rst code to embed the images in the document
|
| 82 |
+
"""
|
| 83 |
+
|
| 84 |
+
figure_paths = [
|
| 85 |
+
os.path.relpath(figure_path, sources_dir).replace(os.sep, "/").lstrip("/")
|
| 86 |
+
for figure_path in figure_list
|
| 87 |
+
]
|
| 88 |
+
images_rst = ""
|
| 89 |
+
if not figure_paths:
|
| 90 |
+
return images_rst
|
| 91 |
+
figure_name = figure_paths[0]
|
| 92 |
+
figure_path = os.path.join("images", os.path.basename(figure_name))
|
| 93 |
+
images_rst = SINGLE_HTML % figure_path
|
| 94 |
+
return images_rst
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
SINGLE_HTML = """
|
| 98 |
+
.. raw:: html
|
| 99 |
+
:file: %s
|
| 100 |
+
"""
|
.venv_haddock/lib/python3.12/site-packages/plotly/io/_templates.py
ADDED
|
@@ -0,0 +1,492 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import textwrap
|
| 2 |
+
import pkgutil
|
| 3 |
+
|
| 4 |
+
import copy
|
| 5 |
+
import os
|
| 6 |
+
import json
|
| 7 |
+
from functools import reduce
|
| 8 |
+
|
| 9 |
+
try:
|
| 10 |
+
from math import gcd
|
| 11 |
+
except ImportError:
|
| 12 |
+
# Python 2
|
| 13 |
+
from fractions import gcd
|
| 14 |
+
|
| 15 |
+
# Create Lazy sentinal object to indicate that a template should be loaded
|
| 16 |
+
# on-demand from package_data
|
| 17 |
+
Lazy = object()
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
# Templates configuration class
|
| 21 |
+
# -----------------------------
|
| 22 |
+
class TemplatesConfig(object):
|
| 23 |
+
"""
|
| 24 |
+
Singleton object containing the current figure templates (aka themes)
|
| 25 |
+
"""
|
| 26 |
+
|
| 27 |
+
def __init__(self):
|
| 28 |
+
# Initialize properties dict
|
| 29 |
+
self._templates = {}
|
| 30 |
+
|
| 31 |
+
# Initialize built-in templates
|
| 32 |
+
default_templates = [
|
| 33 |
+
"ggplot2",
|
| 34 |
+
"seaborn",
|
| 35 |
+
"simple_white",
|
| 36 |
+
"plotly",
|
| 37 |
+
"plotly_white",
|
| 38 |
+
"plotly_dark",
|
| 39 |
+
"presentation",
|
| 40 |
+
"xgridoff",
|
| 41 |
+
"ygridoff",
|
| 42 |
+
"gridon",
|
| 43 |
+
"none",
|
| 44 |
+
]
|
| 45 |
+
|
| 46 |
+
for template_name in default_templates:
|
| 47 |
+
self._templates[template_name] = Lazy
|
| 48 |
+
|
| 49 |
+
self._validator = None
|
| 50 |
+
self._default = None
|
| 51 |
+
|
| 52 |
+
# ### Magic methods ###
|
| 53 |
+
# Make this act as a dict of templates
|
| 54 |
+
def __len__(self):
|
| 55 |
+
return len(self._templates)
|
| 56 |
+
|
| 57 |
+
def __contains__(self, item):
|
| 58 |
+
return item in self._templates
|
| 59 |
+
|
| 60 |
+
def __iter__(self):
|
| 61 |
+
return iter(self._templates)
|
| 62 |
+
|
| 63 |
+
def __getitem__(self, item):
|
| 64 |
+
if isinstance(item, str):
|
| 65 |
+
template_names = item.split("+")
|
| 66 |
+
else:
|
| 67 |
+
template_names = [item]
|
| 68 |
+
|
| 69 |
+
templates = []
|
| 70 |
+
for template_name in template_names:
|
| 71 |
+
template = self._templates[template_name]
|
| 72 |
+
if template is Lazy:
|
| 73 |
+
from plotly.graph_objs.layout import Template
|
| 74 |
+
|
| 75 |
+
if template_name == "none":
|
| 76 |
+
# "none" is a special built-in named template that applied no defaults
|
| 77 |
+
template = Template(data_scatter=[{}])
|
| 78 |
+
self._templates[template_name] = template
|
| 79 |
+
else:
|
| 80 |
+
# Load template from package data
|
| 81 |
+
path = os.path.join(
|
| 82 |
+
"package_data", "templates", template_name + ".json"
|
| 83 |
+
)
|
| 84 |
+
template_str = pkgutil.get_data("plotly", path).decode("utf-8")
|
| 85 |
+
template_dict = json.loads(template_str)
|
| 86 |
+
template = Template(template_dict, _validate=False)
|
| 87 |
+
|
| 88 |
+
self._templates[template_name] = template
|
| 89 |
+
templates.append(self._templates[template_name])
|
| 90 |
+
|
| 91 |
+
return self.merge_templates(*templates)
|
| 92 |
+
|
| 93 |
+
def __setitem__(self, key, value):
|
| 94 |
+
self._templates[key] = self._validate(value)
|
| 95 |
+
|
| 96 |
+
def __delitem__(self, key):
|
| 97 |
+
# Remove template
|
| 98 |
+
del self._templates[key]
|
| 99 |
+
|
| 100 |
+
# Check if we need to remove it as the default
|
| 101 |
+
if self._default == key:
|
| 102 |
+
self._default = None
|
| 103 |
+
|
| 104 |
+
def _validate(self, value):
|
| 105 |
+
if not self._validator:
|
| 106 |
+
from plotly.validator_cache import ValidatorCache
|
| 107 |
+
|
| 108 |
+
self._validator = ValidatorCache.get_validator("layout", "template")
|
| 109 |
+
|
| 110 |
+
return self._validator.validate_coerce(value)
|
| 111 |
+
|
| 112 |
+
def keys(self):
|
| 113 |
+
return self._templates.keys()
|
| 114 |
+
|
| 115 |
+
def items(self):
|
| 116 |
+
return self._templates.items()
|
| 117 |
+
|
| 118 |
+
def update(self, d={}, **kwargs):
|
| 119 |
+
"""
|
| 120 |
+
Update one or more templates from a dict or from input keyword
|
| 121 |
+
arguments.
|
| 122 |
+
|
| 123 |
+
Parameters
|
| 124 |
+
----------
|
| 125 |
+
d: dict
|
| 126 |
+
Dictionary from template names to new template values.
|
| 127 |
+
|
| 128 |
+
kwargs
|
| 129 |
+
Named argument value pairs where the name is a template name
|
| 130 |
+
and the value is a new template value.
|
| 131 |
+
"""
|
| 132 |
+
for k, v in dict(d, **kwargs).items():
|
| 133 |
+
self[k] = v
|
| 134 |
+
|
| 135 |
+
# ### Properties ###
|
| 136 |
+
@property
|
| 137 |
+
def default(self):
|
| 138 |
+
"""
|
| 139 |
+
The name of the default template, or None if no there is no default
|
| 140 |
+
|
| 141 |
+
If not None, the default template is automatically applied to all
|
| 142 |
+
figures during figure construction if no explicit template is
|
| 143 |
+
specified.
|
| 144 |
+
|
| 145 |
+
The names of available templates may be retrieved with:
|
| 146 |
+
|
| 147 |
+
>>> import plotly.io as pio
|
| 148 |
+
>>> list(pio.templates)
|
| 149 |
+
|
| 150 |
+
Returns
|
| 151 |
+
-------
|
| 152 |
+
str
|
| 153 |
+
"""
|
| 154 |
+
return self._default
|
| 155 |
+
|
| 156 |
+
@default.setter
|
| 157 |
+
def default(self, value):
|
| 158 |
+
# Validate value
|
| 159 |
+
# Could be a Template object, the key of a registered template,
|
| 160 |
+
# Or a string containing the names of multiple templates joined on
|
| 161 |
+
# '+' characters
|
| 162 |
+
self._validate(value)
|
| 163 |
+
self._default = value
|
| 164 |
+
|
| 165 |
+
def __repr__(self):
|
| 166 |
+
return """\
|
| 167 |
+
Templates configuration
|
| 168 |
+
-----------------------
|
| 169 |
+
Default template: {default}
|
| 170 |
+
Available templates:
|
| 171 |
+
{available}
|
| 172 |
+
""".format(default=repr(self.default), available=self._available_templates_str())
|
| 173 |
+
|
| 174 |
+
def _available_templates_str(self):
|
| 175 |
+
"""
|
| 176 |
+
Return nicely wrapped string representation of all
|
| 177 |
+
available template names
|
| 178 |
+
"""
|
| 179 |
+
available = "\n".join(
|
| 180 |
+
textwrap.wrap(
|
| 181 |
+
repr(list(self)),
|
| 182 |
+
width=79 - 8,
|
| 183 |
+
initial_indent=" " * 8,
|
| 184 |
+
subsequent_indent=" " * 9,
|
| 185 |
+
)
|
| 186 |
+
)
|
| 187 |
+
return available
|
| 188 |
+
|
| 189 |
+
def merge_templates(self, *args):
|
| 190 |
+
"""
|
| 191 |
+
Merge a collection of templates into a single combined template.
|
| 192 |
+
Templates are process from left to right so if multiple templates
|
| 193 |
+
specify the same propery, the right-most template will take
|
| 194 |
+
precedence.
|
| 195 |
+
|
| 196 |
+
Parameters
|
| 197 |
+
----------
|
| 198 |
+
args: list of Template
|
| 199 |
+
Zero or more template objects (or dicts with compatible properties)
|
| 200 |
+
|
| 201 |
+
Returns
|
| 202 |
+
-------
|
| 203 |
+
template:
|
| 204 |
+
A combined template object
|
| 205 |
+
|
| 206 |
+
Examples
|
| 207 |
+
--------
|
| 208 |
+
|
| 209 |
+
>>> pio.templates.merge_templates(
|
| 210 |
+
... go.layout.Template(layout={'font': {'size': 20}}),
|
| 211 |
+
... go.layout.Template(data={'scatter': [{'mode': 'markers'}]}),
|
| 212 |
+
... go.layout.Template(layout={'font': {'family': 'Courier'}}))
|
| 213 |
+
layout.Template({
|
| 214 |
+
'data': {'scatter': [{'mode': 'markers', 'type': 'scatter'}]},
|
| 215 |
+
'layout': {'font': {'family': 'Courier', 'size': 20}}
|
| 216 |
+
})
|
| 217 |
+
"""
|
| 218 |
+
if args:
|
| 219 |
+
return reduce(self._merge_2_templates, args)
|
| 220 |
+
else:
|
| 221 |
+
from plotly.graph_objs.layout import Template
|
| 222 |
+
|
| 223 |
+
return Template()
|
| 224 |
+
|
| 225 |
+
def _merge_2_templates(self, template1, template2):
|
| 226 |
+
"""
|
| 227 |
+
Helper function for merge_templates that merges exactly two templates
|
| 228 |
+
|
| 229 |
+
Parameters
|
| 230 |
+
----------
|
| 231 |
+
template1: Template
|
| 232 |
+
template2: Template
|
| 233 |
+
|
| 234 |
+
Returns
|
| 235 |
+
-------
|
| 236 |
+
Template:
|
| 237 |
+
merged template
|
| 238 |
+
"""
|
| 239 |
+
# Validate/copy input templates
|
| 240 |
+
result = self._validate(template1)
|
| 241 |
+
other = self._validate(template2)
|
| 242 |
+
|
| 243 |
+
# Cycle traces
|
| 244 |
+
for trace_type in result.data:
|
| 245 |
+
result_traces = result.data[trace_type]
|
| 246 |
+
other_traces = other.data[trace_type]
|
| 247 |
+
|
| 248 |
+
if result_traces and other_traces:
|
| 249 |
+
lcm = (
|
| 250 |
+
len(result_traces)
|
| 251 |
+
* len(other_traces)
|
| 252 |
+
// gcd(len(result_traces), len(other_traces))
|
| 253 |
+
)
|
| 254 |
+
|
| 255 |
+
# Cycle result traces
|
| 256 |
+
result.data[trace_type] = result_traces * (lcm // len(result_traces))
|
| 257 |
+
|
| 258 |
+
# Cycle other traces
|
| 259 |
+
other.data[trace_type] = other_traces * (lcm // len(other_traces))
|
| 260 |
+
|
| 261 |
+
# Perform update
|
| 262 |
+
result.update(other)
|
| 263 |
+
|
| 264 |
+
return result
|
| 265 |
+
|
| 266 |
+
|
| 267 |
+
# Make config a singleton object
|
| 268 |
+
# ------------------------------
|
| 269 |
+
templates = TemplatesConfig()
|
| 270 |
+
del TemplatesConfig
|
| 271 |
+
|
| 272 |
+
|
| 273 |
+
# Template utilities
|
| 274 |
+
# ------------------
|
| 275 |
+
def walk_push_to_template(fig_obj, template_obj, skip):
|
| 276 |
+
"""
|
| 277 |
+
Move style properties from fig_obj to template_obj.
|
| 278 |
+
|
| 279 |
+
Parameters
|
| 280 |
+
----------
|
| 281 |
+
fig_obj: plotly.basedatatypes.BasePlotlyType
|
| 282 |
+
template_obj: plotly.basedatatypes.BasePlotlyType
|
| 283 |
+
skip: set of str
|
| 284 |
+
Set of names of properties to skip
|
| 285 |
+
"""
|
| 286 |
+
from _plotly_utils.basevalidators import (
|
| 287 |
+
CompoundValidator,
|
| 288 |
+
CompoundArrayValidator,
|
| 289 |
+
is_array,
|
| 290 |
+
)
|
| 291 |
+
|
| 292 |
+
for prop in list(fig_obj._props):
|
| 293 |
+
if prop == "template" or prop in skip:
|
| 294 |
+
# Avoid infinite recursion
|
| 295 |
+
continue
|
| 296 |
+
|
| 297 |
+
fig_val = fig_obj[prop]
|
| 298 |
+
template_val = template_obj[prop]
|
| 299 |
+
|
| 300 |
+
validator = fig_obj._get_validator(prop)
|
| 301 |
+
|
| 302 |
+
if isinstance(validator, CompoundValidator):
|
| 303 |
+
walk_push_to_template(fig_val, template_val, skip)
|
| 304 |
+
if not fig_val._props:
|
| 305 |
+
# Check if we can remove prop itself
|
| 306 |
+
fig_obj[prop] = None
|
| 307 |
+
elif isinstance(validator, CompoundArrayValidator) and fig_val:
|
| 308 |
+
template_elements = list(template_val)
|
| 309 |
+
template_element_names = [el.name for el in template_elements]
|
| 310 |
+
template_propdefaults = template_obj[prop[:-1] + "defaults"]
|
| 311 |
+
|
| 312 |
+
for fig_el in fig_val:
|
| 313 |
+
element_name = fig_el.name
|
| 314 |
+
if element_name:
|
| 315 |
+
# No properties are skipped inside a named array element
|
| 316 |
+
skip = set()
|
| 317 |
+
if fig_el.name in template_element_names:
|
| 318 |
+
item_index = template_element_names.index(fig_el.name)
|
| 319 |
+
template_el = template_elements[item_index]
|
| 320 |
+
walk_push_to_template(fig_el, template_el, skip)
|
| 321 |
+
else:
|
| 322 |
+
template_el = fig_el.__class__()
|
| 323 |
+
walk_push_to_template(fig_el, template_el, skip)
|
| 324 |
+
template_elements.append(template_el)
|
| 325 |
+
template_element_names.append(fig_el.name)
|
| 326 |
+
|
| 327 |
+
# Restore element name
|
| 328 |
+
# since it was pushed to template above
|
| 329 |
+
fig_el.name = element_name
|
| 330 |
+
else:
|
| 331 |
+
walk_push_to_template(fig_el, template_propdefaults, skip)
|
| 332 |
+
|
| 333 |
+
template_obj[prop] = template_elements
|
| 334 |
+
|
| 335 |
+
elif not validator.array_ok or not is_array(fig_val):
|
| 336 |
+
# Move property value from figure to template
|
| 337 |
+
template_obj[prop] = fig_val
|
| 338 |
+
try:
|
| 339 |
+
fig_obj[prop] = None
|
| 340 |
+
except ValueError:
|
| 341 |
+
# Property cannot be set to None, move on.
|
| 342 |
+
pass
|
| 343 |
+
|
| 344 |
+
|
| 345 |
+
def to_templated(fig, skip=("title", "text")):
|
| 346 |
+
"""
|
| 347 |
+
Return a copy of a figure where all styling properties have been moved
|
| 348 |
+
into the figure's template. The template property of the resulting figure
|
| 349 |
+
may then be used to set the default styling of other figures.
|
| 350 |
+
|
| 351 |
+
Parameters
|
| 352 |
+
----------
|
| 353 |
+
fig
|
| 354 |
+
Figure object or dict representing a figure
|
| 355 |
+
skip
|
| 356 |
+
A collection of names of properties to skip when moving properties to
|
| 357 |
+
the template. Defaults to ('title', 'text') so that the text
|
| 358 |
+
of figure titles, axis titles, and annotations does not become part of
|
| 359 |
+
the template
|
| 360 |
+
|
| 361 |
+
Examples
|
| 362 |
+
--------
|
| 363 |
+
Imports
|
| 364 |
+
|
| 365 |
+
>>> import plotly.graph_objs as go
|
| 366 |
+
>>> import plotly.io as pio
|
| 367 |
+
|
| 368 |
+
Construct a figure with large courier text
|
| 369 |
+
|
| 370 |
+
>>> fig = go.Figure(layout={'title': 'Figure Title',
|
| 371 |
+
... 'font': {'size': 20, 'family': 'Courier'},
|
| 372 |
+
... 'template':"none"})
|
| 373 |
+
>>> fig # doctest: +NORMALIZE_WHITESPACE
|
| 374 |
+
Figure({
|
| 375 |
+
'data': [],
|
| 376 |
+
'layout': {'font': {'family': 'Courier', 'size': 20},
|
| 377 |
+
'template': '...', 'title': {'text': 'Figure Title'}}
|
| 378 |
+
})
|
| 379 |
+
|
| 380 |
+
Convert to a figure with a template. Note how the 'font' properties have
|
| 381 |
+
been moved into the template property.
|
| 382 |
+
|
| 383 |
+
>>> templated_fig = pio.to_templated(fig)
|
| 384 |
+
>>> templated_fig.layout.template
|
| 385 |
+
layout.Template({
|
| 386 |
+
'layout': {'font': {'family': 'Courier', 'size': 20}}
|
| 387 |
+
})
|
| 388 |
+
>>> templated_fig
|
| 389 |
+
Figure({
|
| 390 |
+
'data': [], 'layout': {'template': '...', 'title': {'text': 'Figure Title'}}
|
| 391 |
+
})
|
| 392 |
+
|
| 393 |
+
|
| 394 |
+
Next create a new figure with this template
|
| 395 |
+
|
| 396 |
+
>>> fig2 = go.Figure(layout={
|
| 397 |
+
... 'title': 'Figure 2 Title',
|
| 398 |
+
... 'template': templated_fig.layout.template})
|
| 399 |
+
>>> fig2.layout.template
|
| 400 |
+
layout.Template({
|
| 401 |
+
'layout': {'font': {'family': 'Courier', 'size': 20}}
|
| 402 |
+
})
|
| 403 |
+
|
| 404 |
+
The default font in fig2 will now be size 20 Courier.
|
| 405 |
+
|
| 406 |
+
Next, register as a named template...
|
| 407 |
+
|
| 408 |
+
>>> pio.templates['large_courier'] = templated_fig.layout.template
|
| 409 |
+
|
| 410 |
+
and specify this template by name when constructing a figure.
|
| 411 |
+
|
| 412 |
+
>>> go.Figure(layout={
|
| 413 |
+
... 'title': 'Figure 3 Title',
|
| 414 |
+
... 'template': 'large_courier'}) # doctest: +ELLIPSIS
|
| 415 |
+
Figure(...)
|
| 416 |
+
|
| 417 |
+
Finally, set this as the default template to be applied to all new figures
|
| 418 |
+
|
| 419 |
+
>>> pio.templates.default = 'large_courier'
|
| 420 |
+
>>> fig = go.Figure(layout={'title': 'Figure 4 Title'})
|
| 421 |
+
>>> fig.layout.template
|
| 422 |
+
layout.Template({
|
| 423 |
+
'layout': {'font': {'family': 'Courier', 'size': 20}}
|
| 424 |
+
})
|
| 425 |
+
|
| 426 |
+
Returns
|
| 427 |
+
-------
|
| 428 |
+
go.Figure
|
| 429 |
+
"""
|
| 430 |
+
|
| 431 |
+
# process fig
|
| 432 |
+
from plotly.basedatatypes import BaseFigure
|
| 433 |
+
from plotly.graph_objs import Figure
|
| 434 |
+
|
| 435 |
+
if not isinstance(fig, BaseFigure):
|
| 436 |
+
fig = Figure(fig)
|
| 437 |
+
|
| 438 |
+
# Process skip
|
| 439 |
+
if not skip:
|
| 440 |
+
skip = set()
|
| 441 |
+
else:
|
| 442 |
+
skip = set(skip)
|
| 443 |
+
|
| 444 |
+
# Always skip uids
|
| 445 |
+
skip.add("uid")
|
| 446 |
+
|
| 447 |
+
# Initialize templated figure with deep copy of input figure
|
| 448 |
+
templated_fig = copy.deepcopy(fig)
|
| 449 |
+
|
| 450 |
+
# Handle layout
|
| 451 |
+
walk_push_to_template(
|
| 452 |
+
templated_fig.layout, templated_fig.layout.template.layout, skip=skip
|
| 453 |
+
)
|
| 454 |
+
|
| 455 |
+
# Handle traces
|
| 456 |
+
trace_type_indexes = {}
|
| 457 |
+
for trace in list(templated_fig.data):
|
| 458 |
+
template_index = trace_type_indexes.get(trace.type, 0)
|
| 459 |
+
|
| 460 |
+
# Extend template traces if necessary
|
| 461 |
+
template_traces = list(templated_fig.layout.template.data[trace.type])
|
| 462 |
+
while len(template_traces) <= template_index:
|
| 463 |
+
# Append empty trace
|
| 464 |
+
template_traces.append(trace.__class__())
|
| 465 |
+
|
| 466 |
+
# Get corresponding template trace
|
| 467 |
+
template_trace = template_traces[template_index]
|
| 468 |
+
|
| 469 |
+
# Perform push properties to template
|
| 470 |
+
walk_push_to_template(trace, template_trace, skip=skip)
|
| 471 |
+
|
| 472 |
+
# Update template traces in templated_fig
|
| 473 |
+
templated_fig.layout.template.data[trace.type] = template_traces
|
| 474 |
+
|
| 475 |
+
# Update trace_type_indexes
|
| 476 |
+
trace_type_indexes[trace.type] = template_index + 1
|
| 477 |
+
|
| 478 |
+
# Remove useless trace arrays
|
| 479 |
+
any_non_empty = False
|
| 480 |
+
for trace_type in templated_fig.layout.template.data:
|
| 481 |
+
traces = templated_fig.layout.template.data[trace_type]
|
| 482 |
+
is_empty = [trace.to_plotly_json() == {"type": trace_type} for trace in traces]
|
| 483 |
+
if all(is_empty):
|
| 484 |
+
templated_fig.layout.template.data[trace_type] = None
|
| 485 |
+
else:
|
| 486 |
+
any_non_empty = True
|
| 487 |
+
|
| 488 |
+
# Check if we can remove the data altogether key
|
| 489 |
+
if not any_non_empty:
|
| 490 |
+
templated_fig.layout.template.data = None
|
| 491 |
+
|
| 492 |
+
return templated_fig
|
.venv_haddock/lib/python3.12/site-packages/plotly/io/_utils.py
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import List
|
| 2 |
+
|
| 3 |
+
import plotly
|
| 4 |
+
import plotly.graph_objs as go
|
| 5 |
+
from plotly.offline import get_plotlyjs_version
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def validate_coerce_fig_to_dict(fig, validate):
|
| 9 |
+
from plotly.basedatatypes import BaseFigure
|
| 10 |
+
|
| 11 |
+
if isinstance(fig, BaseFigure):
|
| 12 |
+
fig_dict = fig.to_dict()
|
| 13 |
+
elif isinstance(fig, dict):
|
| 14 |
+
if validate:
|
| 15 |
+
# This will raise an exception if fig is not a valid plotly figure
|
| 16 |
+
fig_dict = plotly.graph_objs.Figure(fig).to_plotly_json()
|
| 17 |
+
else:
|
| 18 |
+
fig_dict = fig
|
| 19 |
+
elif hasattr(fig, "to_plotly_json"):
|
| 20 |
+
fig_dict = fig.to_plotly_json()
|
| 21 |
+
else:
|
| 22 |
+
raise ValueError(
|
| 23 |
+
"""
|
| 24 |
+
The fig parameter must be a dict or Figure.
|
| 25 |
+
Received value of type {typ}: {v}""".format(typ=type(fig), v=fig)
|
| 26 |
+
)
|
| 27 |
+
return fig_dict
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def validate_coerce_output_type(output_type):
|
| 31 |
+
if output_type == "Figure" or output_type == go.Figure:
|
| 32 |
+
cls = go.Figure
|
| 33 |
+
elif output_type == "FigureWidget" or (
|
| 34 |
+
hasattr(go, "FigureWidget") and output_type == go.FigureWidget
|
| 35 |
+
):
|
| 36 |
+
cls = go.FigureWidget
|
| 37 |
+
else:
|
| 38 |
+
raise ValueError(
|
| 39 |
+
"""
|
| 40 |
+
Invalid output type: {output_type}
|
| 41 |
+
Must be one of: 'Figure', 'FigureWidget'"""
|
| 42 |
+
)
|
| 43 |
+
return cls
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
def broadcast_args_to_dicts(**kwargs: dict) -> List[dict]:
|
| 47 |
+
"""
|
| 48 |
+
Given one or more keyword arguments which may be either a single value or a list of values,
|
| 49 |
+
return a list of keyword dictionaries by broadcasting the single valuesacross all the dicts.
|
| 50 |
+
If more than one item in the input is a list, all lists must be the same length.
|
| 51 |
+
|
| 52 |
+
Parameters
|
| 53 |
+
----------
|
| 54 |
+
**kwargs: dict
|
| 55 |
+
The keyword arguments
|
| 56 |
+
|
| 57 |
+
Returns
|
| 58 |
+
-------
|
| 59 |
+
list of dicts
|
| 60 |
+
A list of dictionaries
|
| 61 |
+
|
| 62 |
+
Raises
|
| 63 |
+
------
|
| 64 |
+
ValueError
|
| 65 |
+
If any of the input lists are not the same length
|
| 66 |
+
"""
|
| 67 |
+
# Check that all list arguments have the same length,
|
| 68 |
+
# and find out what that length is
|
| 69 |
+
# If there are no list arguments, length is 1
|
| 70 |
+
list_lengths = [len(v) for v in tuple(kwargs.values()) if isinstance(v, list)]
|
| 71 |
+
if list_lengths and len(set(list_lengths)) > 1:
|
| 72 |
+
raise ValueError("All list arguments must have the same length.")
|
| 73 |
+
list_length = list_lengths[0] if list_lengths else 1
|
| 74 |
+
|
| 75 |
+
# Expand all arguments to lists of the same length
|
| 76 |
+
expanded_kwargs = {
|
| 77 |
+
k: [v] * list_length if not isinstance(v, list) else v
|
| 78 |
+
for k, v in kwargs.items()
|
| 79 |
+
}
|
| 80 |
+
# Reshape into a list of dictionaries
|
| 81 |
+
# Each dictionary represents the keyword arguments for a single function call
|
| 82 |
+
list_of_kwargs = [
|
| 83 |
+
{k: v[i] for k, v in expanded_kwargs.items()} for i in range(list_length)
|
| 84 |
+
]
|
| 85 |
+
|
| 86 |
+
return list_of_kwargs
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
def plotly_cdn_url(cdn_ver=get_plotlyjs_version()):
|
| 90 |
+
"""Return a valid plotly CDN url."""
|
| 91 |
+
return "https://cdn.plot.ly/plotly-{cdn_ver}.min.js".format(
|
| 92 |
+
cdn_ver=cdn_ver,
|
| 93 |
+
)
|
.venv_haddock/lib/python3.12/site-packages/plotly/io/base_renderers.py
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# ruff: noqa: F401
|
| 2 |
+
from ._base_renderers import (
|
| 3 |
+
MimetypeRenderer,
|
| 4 |
+
PlotlyRenderer,
|
| 5 |
+
JsonRenderer,
|
| 6 |
+
ImageRenderer,
|
| 7 |
+
PngRenderer,
|
| 8 |
+
SvgRenderer,
|
| 9 |
+
PdfRenderer,
|
| 10 |
+
JpegRenderer,
|
| 11 |
+
HtmlRenderer,
|
| 12 |
+
ColabRenderer,
|
| 13 |
+
KaggleRenderer,
|
| 14 |
+
NotebookRenderer,
|
| 15 |
+
ExternalRenderer,
|
| 16 |
+
BrowserRenderer,
|
| 17 |
+
)
|
.venv_haddock/lib/python3.12/site-packages/plotly/io/json.py
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# ruff: noqa: F401
|
| 2 |
+
from ._json import (
|
| 3 |
+
to_json,
|
| 4 |
+
write_json,
|
| 5 |
+
from_json,
|
| 6 |
+
read_json,
|
| 7 |
+
config,
|
| 8 |
+
to_json_plotly,
|
| 9 |
+
from_json_plotly,
|
| 10 |
+
)
|
.venv_haddock/lib/python3.12/site-packages/plotly/io/kaleido.py
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# ruff: noqa: F401
|
| 2 |
+
from ._kaleido import (
|
| 3 |
+
to_image,
|
| 4 |
+
write_image,
|
| 5 |
+
scope,
|
| 6 |
+
kaleido_available,
|
| 7 |
+
kaleido_major,
|
| 8 |
+
ENABLE_KALEIDO_V0_DEPRECATION_WARNINGS,
|
| 9 |
+
KALEIDO_DEPRECATION_MSG,
|
| 10 |
+
ORCA_DEPRECATION_MSG,
|
| 11 |
+
ENGINE_PARAM_DEPRECATION_MSG,
|
| 12 |
+
)
|
.venv_haddock/lib/python3.12/site-packages/plotly/io/orca.py
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# ruff: noqa: F401
|
| 2 |
+
from ._orca import (
|
| 3 |
+
ensure_server,
|
| 4 |
+
shutdown_server,
|
| 5 |
+
validate_executable,
|
| 6 |
+
reset_status,
|
| 7 |
+
config,
|
| 8 |
+
status,
|
| 9 |
+
)
|
.venv_haddock/lib/python3.12/site-packages/plotly/labextension/._package.json
ADDED
|
Binary file (4.1 kB). View file
|
|
|
.venv_haddock/lib/python3.12/site-packages/plotly/labextension/._static
ADDED
|
Binary file (4.1 kB). View file
|
|
|
.venv_haddock/lib/python3.12/site-packages/plotly/labextension/package.json
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "jupyterlab-plotly",
|
| 3 |
+
"main": "lib/mimeExtension.js",
|
| 4 |
+
"version": "6.0.1",
|
| 5 |
+
"repository": {
|
| 6 |
+
"type": "git",
|
| 7 |
+
"url": "https://github.com/plotly/plotly.py"
|
| 8 |
+
},
|
| 9 |
+
"description": "The plotly Jupyter extension",
|
| 10 |
+
"author": "The plotly.py team",
|
| 11 |
+
"license": "MIT",
|
| 12 |
+
"scripts": {
|
| 13 |
+
"build:widget": "esbuild --bundle --alias:plotly.js=plotly.js/dist/plotly --format=esm --minify --outfile=../plotly/package_data/widgetbundle.js src/widget.ts",
|
| 14 |
+
"build:mimerenderer": "esbuild --bundle --alias:plotly.js=plotly.js/dist/plotly --format=esm --minify --outfile=lib/mimeExtension.js src/mimeExtension.ts",
|
| 15 |
+
"build:labextension": "jupyter labextension build .",
|
| 16 |
+
"build": "npm run build:widget && npm run build:mimerenderer && npm run build:labextension",
|
| 17 |
+
"watch": "npm run build -- --watch --sourcemap=inline",
|
| 18 |
+
"typecheck": "tsc --noEmit"
|
| 19 |
+
},
|
| 20 |
+
"dependencies": {
|
| 21 |
+
"lodash-es": "^4.17.21",
|
| 22 |
+
"plotly.js": "3.3.1",
|
| 23 |
+
"@lumino/widgets": "~2.4.0"
|
| 24 |
+
},
|
| 25 |
+
"devDependencies": {
|
| 26 |
+
"@jupyterlab/builder": "^4.3.6 || ^3.6.8",
|
| 27 |
+
"@types/plotly.js": "^2.33.4",
|
| 28 |
+
"esbuild": "^0.23.1",
|
| 29 |
+
"typescript": "^5.6.2"
|
| 30 |
+
},
|
| 31 |
+
"jupyterlab": {
|
| 32 |
+
"mimeExtension": true,
|
| 33 |
+
"outputDir": "../plotly/labextension",
|
| 34 |
+
"_build": {
|
| 35 |
+
"load": "static/remoteEntry.a1fdd5414cdd3506ffce.js",
|
| 36 |
+
"mimeExtension": "./mimeExtension"
|
| 37 |
+
}
|
| 38 |
+
}
|
| 39 |
+
}
|
.venv_haddock/lib/python3.12/site-packages/plotly/labextension/static/._340.b079a9b51d0cddaf4ab3.js
ADDED
|
Binary file (4.1 kB). View file
|
|
|
.venv_haddock/lib/python3.12/site-packages/plotly/labextension/static/._remoteEntry.a1fdd5414cdd3506ffce.js
ADDED
|
Binary file (4.1 kB). View file
|
|
|
.venv_haddock/lib/python3.12/site-packages/plotly/labextension/static/._style.js
ADDED
|
Binary file (4.1 kB). View file
|
|
|
.venv_haddock/lib/python3.12/site-packages/plotly/labextension/static/340.b079a9b51d0cddaf4ab3.js
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
.venv_haddock/lib/python3.12/site-packages/plotly/labextension/static/remoteEntry.a1fdd5414cdd3506ffce.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
var _JUPYTERLAB;(()=>{"use strict";var e,r,t={987:(e,r,t)=>{var o={"./index":()=>t.e(340).then((()=>()=>t(340))),"./mimeExtension":()=>t.e(340).then((()=>()=>t(340)))},a=(e,r)=>(t.R=r,r=t.o(o,e)?o[e]():Promise.resolve().then((()=>{throw new Error('Module "'+e+'" does not exist in container.')})),t.R=void 0,r),n=(e,r)=>{if(t.S){var o="default",a=t.S[o];if(a&&a!==e)throw new Error("Container initialization failed as it has already been initialized with a different share scope");return t.S[o]=e,t.I(o,r)}};t.d(r,{get:()=>a,init:()=>n})}},o={};function a(e){var r=o[e];if(void 0!==r)return r.exports;var n=o[e]={exports:{}};return t[e](n,n.exports,a),n.exports}a.m=t,a.c=o,a.amdO={},a.d=(e,r)=>{for(var t in r)a.o(r,t)&&!a.o(e,t)&&Object.defineProperty(e,t,{enumerable:!0,get:r[t]})},a.f={},a.e=e=>Promise.all(Object.keys(a.f).reduce(((r,t)=>(a.f[t](e,r),r)),[])),a.u=e=>e+".b079a9b51d0cddaf4ab3.js?v=b079a9b51d0cddaf4ab3",a.g=function(){if("object"==typeof globalThis)return globalThis;try{return this||new Function("return this")()}catch(e){if("object"==typeof window)return window}}(),a.o=(e,r)=>Object.prototype.hasOwnProperty.call(e,r),e={},r="jupyterlab-plotly:",a.l=(t,o,n,i)=>{if(e[t])e[t].push(o);else{var l,u;if(void 0!==n)for(var p=document.getElementsByTagName("script"),s=0;s<p.length;s++){var c=p[s];if(c.getAttribute("src")==t||c.getAttribute("data-webpack")==r+n){l=c;break}}l||(u=!0,(l=document.createElement("script")).charset="utf-8",l.timeout=120,a.nc&&l.setAttribute("nonce",a.nc),l.setAttribute("data-webpack",r+n),l.src=t),e[t]=[o];var d=(r,o)=>{l.onerror=l.onload=null,clearTimeout(f);var a=e[t];if(delete e[t],l.parentNode&&l.parentNode.removeChild(l),a&&a.forEach((e=>e(o))),r)return r(o)},f=setTimeout(d.bind(null,void 0,{type:"timeout",target:l}),12e4);l.onerror=d.bind(null,l.onerror),l.onload=d.bind(null,l.onload),u&&document.head.appendChild(l)}},a.r=e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},(()=>{a.S={};var e={},r={};a.I=(t,o)=>{o||(o=[]);var n=r[t];if(n||(n=r[t]={}),!(o.indexOf(n)>=0)){if(o.push(n),e[t])return e[t];a.o(a.S,t)||(a.S[t]={});var i=a.S[t],l="jupyterlab-plotly",u=[];return"default"===t&&((e,r,t,o)=>{var n=i[e]=i[e]||{},u=n[r];(!u||!u.loaded&&(1!=!u.eager?o:l>u.from))&&(n[r]={get:()=>a.e(340).then((()=>()=>a(340))),from:l,eager:!1})})("jupyterlab-plotly","6.0.1"),e[t]=u.length?Promise.all(u).then((()=>e[t]=1)):1}}})(),(()=>{var e;a.g.importScripts&&(e=a.g.location+"");var r=a.g.document;if(!e&&r&&(r.currentScript&&"SCRIPT"===r.currentScript.tagName.toUpperCase()&&(e=r.currentScript.src),!e)){var t=r.getElementsByTagName("script");if(t.length)for(var o=t.length-1;o>-1&&(!e||!/^http(s?):/.test(e));)e=t[o--].src}if(!e)throw new Error("Automatic publicPath is not supported in this browser");e=e.replace(/^blob:/,"").replace(/#.*$/,"").replace(/\?.*$/,"").replace(/\/[^\/]+$/,"/"),a.p=e})(),(()=>{var e={80:0};a.f.j=(r,t)=>{var o=a.o(e,r)?e[r]:void 0;if(0!==o)if(o)t.push(o[2]);else{var n=new Promise(((t,a)=>o=e[r]=[t,a]));t.push(o[2]=n);var i=a.p+a.u(r),l=new Error;a.l(i,(t=>{if(a.o(e,r)&&(0!==(o=e[r])&&(e[r]=void 0),o)){var n=t&&("load"===t.type?"missing":t.type),i=t&&t.target&&t.target.src;l.message="Loading chunk "+r+" failed.\n("+n+": "+i+")",l.name="ChunkLoadError",l.type=n,l.request=i,o[1](l)}}),"chunk-"+r,r)}};var r=(r,t)=>{var o,n,[i,l,u]=t,p=0;if(i.some((r=>0!==e[r]))){for(o in l)a.o(l,o)&&(a.m[o]=l[o]);u&&u(a)}for(r&&r(t);p<i.length;p++)n=i[p],a.o(e,n)&&e[n]&&e[n][0](),e[n]=0},t=self.webpackChunkjupyterlab_plotly=self.webpackChunkjupyterlab_plotly||[];t.forEach(r.bind(null,0)),t.push=r.bind(null,t.push.bind(t))})();var n=a(987);(_JUPYTERLAB=void 0===_JUPYTERLAB?{}:_JUPYTERLAB)["jupyterlab-plotly"]=n})();
|
.venv_haddock/lib/python3.12/site-packages/plotly/labextension/static/style.js
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/* This is a generated file of CSS imports */
|
| 2 |
+
/* It was generated by @jupyterlab/builder in Build.ensureAssets() */
|
| 3 |
+
|
| 4 |
+
|
.venv_haddock/lib/python3.12/site-packages/plotly/matplotlylib/.___init__.py
ADDED
|
Binary file (4.1 kB). View file
|
|
|
.venv_haddock/lib/python3.12/site-packages/plotly/matplotlylib/.___pycache__
ADDED
|
Binary file (4.1 kB). View file
|
|
|
.venv_haddock/lib/python3.12/site-packages/plotly/matplotlylib/._mplexporter
ADDED
|
Binary file (4.1 kB). View file
|
|
|
.venv_haddock/lib/python3.12/site-packages/plotly/matplotlylib/._mpltools.py
ADDED
|
Binary file (4.1 kB). View file
|
|
|
.venv_haddock/lib/python3.12/site-packages/plotly/matplotlylib/._renderer.py
ADDED
|
Binary file (4.1 kB). View file
|
|
|
.venv_haddock/lib/python3.12/site-packages/plotly/matplotlylib/._tests
ADDED
|
Binary file (4.1 kB). View file
|
|
|