Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 40c2a45

Browse filesBrowse files
committed
Merge pull request #192 from plotly/plot_postmessage
`plot` method in `GraphWidget`
2 parents a283d83 + 8486779 commit 40c2a45
Copy full SHA for 40c2a45

File tree

Expand file treeCollapse file tree

4 files changed

+92
-30
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+92
-30
lines changed

‎plotly/plotly/plotly.py

Copy file name to clipboardExpand all lines: plotly/plotly/plotly.py
+2-27Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -163,33 +163,8 @@ def plot(figure_or_data, validate=True, **plot_options):
163163
False: do not open plot in the browser, but do return the unique url
164164
165165
"""
166-
if isinstance(figure_or_data, dict):
167-
figure = figure_or_data
168-
elif isinstance(figure_or_data, list):
169-
figure = {'data': figure_or_data}
170-
else:
171-
raise exceptions.PlotlyError("The `figure_or_data` positional argument "
172-
"must be either `dict`-like or "
173-
"`list`-like.")
174-
if validate:
175-
try:
176-
tools.validate(figure, obj_type='Figure')
177-
except exceptions.PlotlyError as err:
178-
raise exceptions.PlotlyError("Invalid 'figure_or_data' argument. "
179-
"Plotly will not be able to properly "
180-
"parse the resulting JSON. If you "
181-
"want to send this 'figure_or_data' "
182-
"to Plotly anyway (not recommended), "
183-
"you can set 'validate=False' as a "
184-
"plot option.\nHere's why you're "
185-
"seeing this error:\n\n{0}"
186-
"".format(err))
187-
if not figure['data']:
188-
raise exceptions.PlotlyEmptyDataError(
189-
"Empty data list found. Make sure that you populated the "
190-
"list of data objects you're sending and try again.\n"
191-
"Questions? support@plot.ly"
192-
)
166+
figure = tools.return_figure_from_figure_or_data(figure_or_data, validate)
167+
193168
for entry in figure['data']:
194169
for key, val in list(entry.items()):
195170
try:

‎plotly/tools.py

Copy file name to clipboardExpand all lines: plotly/tools.py
+32Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1230,3 +1230,35 @@ def __init__(self, url, width, height):
12301230

12311231
def _repr_html_(self):
12321232
return self.embed_code
1233+
1234+
1235+
def return_figure_from_figure_or_data(figure_or_data, validate_figure):
1236+
if isinstance(figure_or_data, dict):
1237+
figure = figure_or_data
1238+
elif isinstance(figure_or_data, list):
1239+
figure = {'data': figure_or_data}
1240+
else:
1241+
raise exceptions.PlotlyError("The `figure_or_data` positional "
1242+
"argument must be either "
1243+
"`dict`-like or `list`-like.")
1244+
if validate_figure:
1245+
try:
1246+
validate(figure, obj_type='Figure')
1247+
except exceptions.PlotlyError as err:
1248+
raise exceptions.PlotlyError("Invalid 'figure_or_data' argument. "
1249+
"Plotly will not be able to properly "
1250+
"parse the resulting JSON. If you "
1251+
"want to send this 'figure_or_data' "
1252+
"to Plotly anyway (not recommended), "
1253+
"you can set 'validate=False' as a "
1254+
"plot option.\nHere's why you're "
1255+
"seeing this error:\n\n{0}"
1256+
"".format(err))
1257+
if not figure['data']:
1258+
raise exceptions.PlotlyEmptyDataError(
1259+
"Empty data list found. Make sure that you populated the "
1260+
"list of data objects you're sending and try again.\n"
1261+
"Questions? support@plot.ly"
1262+
)
1263+
1264+
return figure

‎plotly/version.py

Copy file name to clipboard
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '1.6.6'
1+
__version__ = '1.6.7'

‎plotly/widgets/graph_widget.py

Copy file name to clipboardExpand all lines: plotly/widgets/graph_widget.py
+57-2Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
from collections import deque
22
import json
3-
import os
43
import uuid
54

65
# TODO: protected imports?
76
from IPython.html import widgets
87
from IPython.utils.traitlets import Unicode
98
from IPython.display import Javascript, display
109

11-
from plotly import utils
10+
from plotly import utils, tools
11+
from plotly.graph_objs import Figure
1212
from pkg_resources import resource_string
1313

1414
# Load JS widget code
@@ -247,6 +247,61 @@ def message_handler(widget, ranges):
247247
"""
248248
self._handle_registration('zoom', callback, remove)
249249

250+
def plot(self, figure_or_data, validate=True):
251+
"""Plot figure_or_data in the Plotly graph widget.
252+
253+
Args:
254+
figure_or_data (dict, list, or plotly.graph_obj object):
255+
The standard Plotly graph object that describes Plotly
256+
graphs as used in `plotly.plotly.plot`. See examples
257+
of the figure_or_data in https://plot.ly/python/
258+
259+
Returns: None
260+
261+
Example 1 - Graph a scatter plot:
262+
```
263+
from plotly.graph_objs import Scatter
264+
g = GraphWidget()
265+
g.plot([Scatter(x=[1, 2, 3], y=[10, 15, 13])])
266+
```
267+
268+
Example 2 - Graph a scatter plot with a title:
269+
```
270+
from plotly.graph_objs import Scatter, Figure, Data
271+
fig = Figure(
272+
data = Data([
273+
Scatter(x=[1, 2, 3], y=[20, 15, 13])
274+
]),
275+
layout = Layout(title='Experimental Data')
276+
)
277+
278+
g = GraphWidget()
279+
g.plot(fig)
280+
```
281+
282+
Example 3 - Clear a graph widget
283+
```
284+
from plotly.graph_objs import Scatter, Figure
285+
g = GraphWidget()
286+
g.plot([Scatter(x=[1, 2, 3], y=[10, 15, 13])])
287+
288+
# Now clear it
289+
g.plot({}) # alternatively, g.plot(Figure())
290+
```
291+
"""
292+
if figure_or_data == {} or figure_or_data == Figure():
293+
validate = False
294+
295+
figure = tools.return_figure_from_figure_or_data(figure_or_data,
296+
validate)
297+
message = {
298+
'task': 'newPlot',
299+
'data': figure.get('data', []),
300+
'layout': figure.get('layout', {}),
301+
'graphId': self._graphId
302+
}
303+
self._handle_outgoing_message(message)
304+
250305
def restyle(self, data, indices=None):
251306
"""Update the style of existing traces in the Plotly graph.
252307

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.