|
1 | 1 | from collections import deque |
2 | 2 | import json |
3 | | -import os |
4 | 3 | import uuid |
5 | 4 |
|
6 | 5 | # TODO: protected imports? |
7 | 6 | from IPython.html import widgets |
8 | 7 | from IPython.utils.traitlets import Unicode |
9 | 8 | from IPython.display import Javascript, display |
10 | 9 |
|
11 | | -from plotly import utils |
| 10 | +from plotly import utils, tools |
| 11 | +from plotly.graph_objs import Figure |
12 | 12 | from pkg_resources import resource_string |
13 | 13 |
|
14 | 14 | # Load JS widget code |
@@ -247,6 +247,61 @@ def message_handler(widget, ranges): |
247 | 247 | """ |
248 | 248 | self._handle_registration('zoom', callback, remove) |
249 | 249 |
|
| 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 | + |
250 | 305 | def restyle(self, data, indices=None): |
251 | 306 | """Update the style of existing traces in the Plotly graph. |
252 | 307 |
|
|
0 commit comments