Skip to content

Commit d5fe515

Browse files
committed
Imporve config
1 parent 4f03efb commit d5fe515

5 files changed

Lines changed: 56 additions & 19 deletions

File tree

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,13 @@ xpx(da).line(color=None) # time→x, city→facet_col
105105
Customize label extraction and slot assignment behavior:
106106

107107
```python
108-
import xarray_plotly as xp
108+
from xarray_plotly import config, xpx
109109

110110
# View current options
111-
xp.config.get_options()
111+
config.get_options()
112112

113-
# Set globally
114-
with xp.config.set_options(label_include_units=False):
113+
# Set globally (temporary)
114+
with config.set_options(label_include_units=False):
115115
fig = xpx(da).line() # Labels won't include units
116116
```
117117

docs/api.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,20 +47,21 @@ The accessor style (`da.plotly.line()`) works but doesn't provide IDE code compl
4747
Customize label extraction and slot assignment behavior:
4848

4949
```python
50-
import xarray_plotly as xp
50+
from xarray_plotly import config, xpx
5151

5252
# View current options
53-
xp.config.get_options()
53+
config.get_options()
5454

5555
# Set options (works as context manager)
56-
with xp.config.set_options(label_include_units=False):
56+
with config.set_options(label_include_units=False):
5757
fig = xpx(da).line()
5858
```
5959

6060
::: xarray_plotly.config
6161
options:
6262
show_root_heading: true
6363
members:
64+
- notebook
6465
- get_options
6566
- set_options
6667
- Options

docs/examples/plot-types.ipynb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,11 @@
1717
"source": [
1818
"import numpy as np\n",
1919
"import pandas as pd\n",
20-
"import plotly.io as pio\n",
2120
"import xarray as xr\n",
2221
"\n",
23-
"from xarray_plotly import xpx\n",
22+
"from xarray_plotly import config, xpx\n",
2423
"\n",
25-
"pio.renderers.default = \"notebook\""
24+
"config.notebook() # Configure Plotly for notebook rendering"
2625
]
2726
},
2827
{

docs/getting-started.ipynb

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,11 @@
4343
"source": [
4444
"import numpy as np\n",
4545
"import pandas as pd\n",
46-
"import plotly.io as pio\n",
4746
"import xarray as xr\n",
4847
"\n",
49-
"from xarray_plotly import xpx # Provides full IDE code completion\n",
48+
"from xarray_plotly import config, xpx\n",
5049
"\n",
51-
"# Set renderer for mkdocs-jupyter compatibility\n",
52-
"pio.renderers.default = \"notebook\""
50+
"config.notebook() # Configure Plotly for notebook rendering"
5351
]
5452
},
5553
{
@@ -211,6 +209,25 @@
211209
"fig"
212210
]
213211
},
212+
{
213+
"cell_type": "markdown",
214+
"metadata": {},
215+
"source": [
216+
"## Configuration\n",
217+
"\n",
218+
"Customize label extraction and other behavior with the config module:"
219+
]
220+
},
221+
{
222+
"cell_type": "code",
223+
"execution_count": null,
224+
"metadata": {},
225+
"outputs": [],
226+
"source": [
227+
"# View current options\n",
228+
"config.get_options()"
229+
]
230+
},
214231
{
215232
"cell_type": "markdown",
216233
"metadata": {},

xarray_plotly/config.py

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ def get_options() -> dict[str, Any]:
101101
102102
Examples
103103
--------
104-
>>> import xarray_plotly as xp
105-
>>> xp.get_options()
104+
>>> from xarray_plotly import config
105+
>>> config.get_options()
106106
{'label_use_long_name': True, 'label_include_units': True, ...}
107107
"""
108108
return _options.to_dict()
@@ -142,12 +142,12 @@ def set_options(
142142
--------
143143
Set globally:
144144
145-
>>> import xarray_plotly as xp
146-
>>> xp.config.set_options(label_include_units=False)
145+
>>> from xarray_plotly import config
146+
>>> config.set_options(label_include_units=False)
147147
148148
Use as context manager:
149149
150-
>>> with xp.config.set_options(label_include_units=False):
150+
>>> with config.set_options(label_include_units=False):
151151
... fig = xpx(da).line() # No units in labels
152152
>>> # Units are back after the context
153153
"""
@@ -181,3 +181,23 @@ def set_options(
181181
_options.label_include_units = old_values["label_include_units"]
182182
_options.label_unit_format = old_values["label_unit_format"]
183183
_options.slot_orders = old_values["slot_orders"]
184+
185+
186+
def notebook(renderer: str = "notebook") -> None:
187+
"""
188+
Configure Plotly for Jupyter notebook rendering.
189+
190+
Parameters
191+
----------
192+
renderer : str, optional
193+
The Plotly renderer to use. Default is "notebook".
194+
Other options include "jupyterlab", "colab", "kaggle", etc.
195+
196+
Examples
197+
--------
198+
>>> from xarray_plotly import config
199+
>>> config.notebook() # Configure for Jupyter notebooks
200+
"""
201+
import plotly.io as pio
202+
203+
pio.renderers.default = renderer

0 commit comments

Comments
 (0)