Skip to content

Commit d9229ec

Browse files
committed
When updating an initialized tuple property with a longer tuple,
append the extra elements to the end
1 parent 7ce3c00 commit d9229ec

File tree

3 files changed

+121
-1
lines changed

3 files changed

+121
-1
lines changed

migration-guide.md

+82-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,86 @@
1+
# Migration to Version 4
2+
This section contains guidance for migrating from plotly.py version 3 to version 4.
3+
4+
## Online features moved to chart-studio package.
5+
Prior versions of plotly.py contained functionality for creating figures in both "online" and "offline" modes. In "online" mode figures were uploaded to the Chart Studio cloud (or on-premise) service, whereas in "offline" mode figures were rendered locally. In version 4, all "online" functionality has been removed from the main `plotly` distribution package and moved to the new `chart-studio` distribution package.
6+
7+
To migrate version 3 "online" functionality, first install the `chart-studio` package using pip...
8+
9+
```
10+
$ pip install chart-studio
11+
```
12+
13+
of conda.
14+
15+
```
16+
$ conda install -c plotly chart-studio
17+
```
18+
19+
Then, update your Python import statements to import "online" functionality from the top-level `chart_studio` package, rather than the top-level `plotly` package. For example. replace
20+
21+
```python
22+
from plotly.plotly import plot, iplot
23+
```
24+
25+
with
26+
27+
```python
28+
from chart_studio.plotly import plot, iplot
29+
```
30+
31+
Similarly,
32+
`plotly.api` -> `chart_studio.api`
33+
`plotly.dashboard_objs` -> `chart_studio.dashboard_objs`
34+
`plotly.grid_objs` -> `chart_studio.grid_objs`
35+
`plotly.presentation_objs` -> `chart_studio.presentation_objs`
36+
`plotly.widgets` -> `chart_studio.widgets`
37+
38+
39+
## New default theme
40+
A new default theme has been enabled for figures created with plotly.py version 4. You can revert to the version 3 figure appearance by disabling the default theme as follows:
41+
42+
```python
43+
import plotly.io as pio
44+
pio.templates.default = 'none'
45+
```
46+
See https://plot.ly/python-next/renderers for more information on theming in plotly.py version 4
47+
48+
## Add trace return value
49+
In version 3, the `add_trace` graph object figure method returned a reference to the newly created trace. This was also the case for the `add_{trace_type}` methods (e.g. `add_scatter`, `add_bar`, etc.). In version 4, these methods return a reference to the calling figure. This change was made to support method chaining of figure operations. For example
50+
51+
```python
52+
from plotly.subplots import make_subplots
53+
(make_subplots(rows=1, cols=2)
54+
.add_scatter(y=[2, 1, 3], row=1, col=1)
55+
.add_bar(y=[3, 2, 1], row=1, col=2)
56+
.update_layout(title_text='Figure title')
57+
.show())
58+
```
59+
60+
Code that relied on the `add_*` methods to return a reference to the newly created trace can be updated as follows.
61+
62+
63+
64+
65+
## Renderers framework
66+
Version 4 introduces a new renderers framework that is a generalization of the `plotly.offline.init_notebook_mode` and `plotly.offline.iplot` functions for displaying figures. The `plotly.offline.iplot` function is still available and has been reimplemented on top of the renderers framework, so no changes are required when poritng to version 4. Going forward, we recommend using the renderers framework directly. See https://plot.ly/python-next/renderers for more information.
67+
68+
69+
70+
71+
72+
73+
74+
## Removals
75+
76+
### fileopt argument removal
77+
The `fileopt` argument to `chart_studio.plotly.plot` has been removed, so in-place modifications to previously published figures are no longer supported.
78+
79+
80+
81+
182
# Migration to Version 3
2-
There are many new and great features in Plotly 3.0 including deeper Jupyter integration, deeper figure validation, improved performance, and more. This guide contains the a summary of the breaking changes that you need to be aware of when migrating code from version 2 to version 3.
83+
There are many new and great features in plotly.py 3.0 including deeper Jupyter integration, deeper figure validation, improved performance, and more. This guide contains a summary of the breaking changes that you need to be aware of when migrating code from version 2 to version 3.
384

485
For a high level overview, read our [announcement post](https://medium.com/@plotlygraphs/introducing-plotly-py-3-0-0-7bb1333f69c6).
586

packages/python/plotly/plotly/basedatatypes.py

+9
Original file line numberDiff line numberDiff line change
@@ -2755,6 +2755,15 @@ def _perform_update(plotly_obj, update_obj):
27552755
# plotly_obj has an existing non-empty array for key
27562756
# In this case we merge val into the existing elements
27572757
BaseFigure._perform_update(plotly_obj[key], val)
2758+
2759+
# If update tuple is longer that current tuple, append the
2760+
# extra elements to the end
2761+
if isinstance(val, (list, tuple)) and len(val) > len(
2762+
plotly_obj[key]
2763+
):
2764+
plotly_obj[key] = plotly_obj[key] + tuple(
2765+
val[len(plotly_obj[key]) :]
2766+
)
27582767
else:
27592768
# plotly_obj is an empty or uninitialized list for key
27602769
# In this case we accept val as is

packages/python/plotly/plotly/tests/test_core/test_graph_objs/test_update.py

+30
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,33 @@ def test_update_initialized_nonempty_list_with_dict(self):
127127

128128
self.assertEqual(len(layout.annotations), 2)
129129
self.assertEqual(layout.to_plotly_json(), expected)
130+
131+
def test_update_initialize_nonempty_list_with_list_extends(self):
132+
layout = go.Layout(
133+
annotations=[
134+
go.layout.Annotation(text="one"),
135+
go.layout.Annotation(text="two"),
136+
]
137+
)
138+
139+
layout.update(
140+
annotations=[
141+
go.layout.Annotation(width=10),
142+
go.layout.Annotation(width=20),
143+
go.layout.Annotation(width=30),
144+
go.layout.Annotation(width=40),
145+
go.layout.Annotation(width=50),
146+
]
147+
)
148+
149+
expected = {
150+
"annotations": [
151+
{"text": "one", "width": 10},
152+
{"text": "two", "width": 20},
153+
{"width": 30},
154+
{"width": 40},
155+
{"width": 50},
156+
]
157+
}
158+
159+
self.assertEqual(layout.to_plotly_json(), expected)

0 commit comments

Comments
 (0)