From 2eef30132f806d9f40153512c130b80efcf61332 Mon Sep 17 00:00:00 2001 From: Chris P Date: Mon, 16 Feb 2015 22:32:09 -0500 Subject: [PATCH 1/7] move figure_or_data argument logic to tools for use in widgets.py --- plotly/plotly/plotly.py | 29 ++--------------------------- plotly/tools.py | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 27 deletions(-) diff --git a/plotly/plotly/plotly.py b/plotly/plotly/plotly.py index e3ba3fa4292..926aa354ac3 100644 --- a/plotly/plotly/plotly.py +++ b/plotly/plotly/plotly.py @@ -163,33 +163,8 @@ def plot(figure_or_data, validate=True, **plot_options): False: do not open plot in the browser, but do return the unique url """ - if isinstance(figure_or_data, dict): - figure = figure_or_data - elif isinstance(figure_or_data, list): - figure = {'data': figure_or_data} - else: - raise exceptions.PlotlyError("The `figure_or_data` positional argument " - "must be either `dict`-like or " - "`list`-like.") - if validate: - try: - tools.validate(figure, obj_type='Figure') - except exceptions.PlotlyError as err: - raise exceptions.PlotlyError("Invalid 'figure_or_data' argument. " - "Plotly will not be able to properly " - "parse the resulting JSON. If you " - "want to send this 'figure_or_data' " - "to Plotly anyway (not recommended), " - "you can set 'validate=False' as a " - "plot option.\nHere's why you're " - "seeing this error:\n\n{0}" - "".format(err)) - if not figure['data']: - raise exceptions.PlotlyEmptyDataError( - "Empty data list found. Make sure that you populated the " - "list of data objects you're sending and try again.\n" - "Questions? support@plot.ly" - ) + figure = tools.return_figure_from_figure_or_data(figure_or_data, validate) + for entry in figure['data']: for key, val in list(entry.items()): try: diff --git a/plotly/tools.py b/plotly/tools.py index 85ea9644cec..fa5c6898598 100644 --- a/plotly/tools.py +++ b/plotly/tools.py @@ -1230,3 +1230,35 @@ def __init__(self, url, width, height): def _repr_html_(self): return self.embed_code + + +def return_figure_from_figure_or_data(figure_or_data, validate_figure): + if isinstance(figure_or_data, dict): + figure = figure_or_data + elif isinstance(figure_or_data, list): + figure = {'data': figure_or_data} + else: + raise exceptions.PlotlyError("The `figure_or_data` positional " + "argument must be either " + "`dict`-like or `list`-like.") + if validate_figure: + try: + validate(figure, obj_type='Figure') + except exceptions.PlotlyError as err: + raise exceptions.PlotlyError("Invalid 'figure_or_data' argument. " + "Plotly will not be able to properly " + "parse the resulting JSON. If you " + "want to send this 'figure_or_data' " + "to Plotly anyway (not recommended), " + "you can set 'validate=False' as a " + "plot option.\nHere's why you're " + "seeing this error:\n\n{0}" + "".format(err)) + if not figure['data']: + raise exceptions.PlotlyEmptyDataError( + "Empty data list found. Make sure that you populated the " + "list of data objects you're sending and try again.\n" + "Questions? support@plot.ly" + ) + + return figure From c5677c82e8cfb75f4f5d9a18dd038724ab0a3ec3 Mon Sep 17 00:00:00 2001 From: Chris P Date: Mon, 16 Feb 2015 22:32:22 -0500 Subject: [PATCH 2/7] add `plot` command to widgets --- plotly/widgets/graph_widget.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/plotly/widgets/graph_widget.py b/plotly/widgets/graph_widget.py index 1f7666138ac..17751070d22 100644 --- a/plotly/widgets/graph_widget.py +++ b/plotly/widgets/graph_widget.py @@ -1,6 +1,5 @@ from collections import deque import json -import os import uuid # TODO: protected imports? @@ -8,7 +7,7 @@ from IPython.utils.traitlets import Unicode from IPython.display import Javascript, display -from plotly import utils +from plotly import utils, tools from pkg_resources import resource_string # Load JS widget code @@ -247,6 +246,18 @@ def message_handler(widget, ranges): """ self._handle_registration('zoom', callback, remove) + def plot(self, figure_or_data, validate=True): + """Plot figure_or_data in the Plotly graph. + """ + figure = tools.return_figure_from_figure_or_data(figure_or_data, validate) + message = { + 'task': 'newPlot', + 'data': figure['data'], + 'layout': figure.get('layout', {}), + 'graphId': self._graphId + } + self._handle_outgoing_message(message) + def restyle(self, data, indices=None): """Update the style of existing traces in the Plotly graph. From 1afa30a151d7352941b052b4c6ec28d5306e8f05 Mon Sep 17 00:00:00 2001 From: Chris P Date: Mon, 16 Feb 2015 22:39:52 -0500 Subject: [PATCH 3/7] bump version --- plotly/version.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plotly/version.py b/plotly/version.py index 008e8016fb1..12ccbec34c4 100644 --- a/plotly/version.py +++ b/plotly/version.py @@ -1 +1 @@ -__version__ = '1.6.6' +__version__ = '1.6.7' From 4d310d0290a771ecffbe5409e4bf0090f9761cb2 Mon Sep 17 00:00:00 2001 From: Chris P Date: Mon, 16 Feb 2015 22:41:27 -0500 Subject: [PATCH 4/7] start a changelog --- changelog.md | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 changelog.md diff --git a/changelog.md b/changelog.md new file mode 100644 index 00000000000..307530542e3 --- /dev/null +++ b/changelog.md @@ -0,0 +1,4 @@ +### Changelog + +- 1.6.7 +Added `plot` method to `plotly.widgets.GraphWidget` for easy-to-use client-side graphing From fc22d6d15457aeb2789d29bc85f8528abfbe19aa Mon Sep 17 00:00:00 2001 From: Chris P Date: Fri, 20 Feb 2015 10:22:07 -0500 Subject: [PATCH 5/7] rm changelog, use github releases instead (thx @etpinard) --- changelog.md | 4 ---- 1 file changed, 4 deletions(-) delete mode 100644 changelog.md diff --git a/changelog.md b/changelog.md deleted file mode 100644 index 307530542e3..00000000000 --- a/changelog.md +++ /dev/null @@ -1,4 +0,0 @@ -### Changelog - -- 1.6.7 -Added `plot` method to `plotly.widgets.GraphWidget` for easy-to-use client-side graphing From 4ed0b855724699006c91a2e101e22f0ffcca7be7 Mon Sep 17 00:00:00 2001 From: Chris P Date: Fri, 20 Feb 2015 10:23:48 -0500 Subject: [PATCH 6/7] allow empty Fig, {}, [] as a way to clear a graph (great idea @theengineear !) --- plotly/widgets/graph_widget.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/plotly/widgets/graph_widget.py b/plotly/widgets/graph_widget.py index 17751070d22..0ccc2fbc691 100644 --- a/plotly/widgets/graph_widget.py +++ b/plotly/widgets/graph_widget.py @@ -8,6 +8,7 @@ from IPython.display import Javascript, display from plotly import utils, tools +from plotly.graph_objs import Figure from pkg_resources import resource_string # Load JS widget code @@ -249,10 +250,14 @@ def message_handler(widget, ranges): def plot(self, figure_or_data, validate=True): """Plot figure_or_data in the Plotly graph. """ - figure = tools.return_figure_from_figure_or_data(figure_or_data, validate) + if figure_or_data == {} or figure_or_data == Figure(): + validate = False + + figure = tools.return_figure_from_figure_or_data(figure_or_data, + validate) message = { 'task': 'newPlot', - 'data': figure['data'], + 'data': figure.get('data', []), 'layout': figure.get('layout', {}), 'graphId': self._graphId } From 84867793ec08ceb005eecd17924f492d6131c852 Mon Sep 17 00:00:00 2001 From: Chris P Date: Fri, 20 Feb 2015 10:24:06 -0500 Subject: [PATCH 7/7] provide some examples for widget plottin --- plotly/widgets/graph_widget.py | 41 +++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/plotly/widgets/graph_widget.py b/plotly/widgets/graph_widget.py index 0ccc2fbc691..22c5c28434f 100644 --- a/plotly/widgets/graph_widget.py +++ b/plotly/widgets/graph_widget.py @@ -248,7 +248,46 @@ def message_handler(widget, ranges): self._handle_registration('zoom', callback, remove) def plot(self, figure_or_data, validate=True): - """Plot figure_or_data in the Plotly graph. + """Plot figure_or_data in the Plotly graph widget. + + Args: + figure_or_data (dict, list, or plotly.graph_obj object): + The standard Plotly graph object that describes Plotly + graphs as used in `plotly.plotly.plot`. See examples + of the figure_or_data in https://plot.ly/python/ + + Returns: None + + Example 1 - Graph a scatter plot: + ``` + from plotly.graph_objs import Scatter + g = GraphWidget() + g.plot([Scatter(x=[1, 2, 3], y=[10, 15, 13])]) + ``` + + Example 2 - Graph a scatter plot with a title: + ``` + from plotly.graph_objs import Scatter, Figure, Data + fig = Figure( + data = Data([ + Scatter(x=[1, 2, 3], y=[20, 15, 13]) + ]), + layout = Layout(title='Experimental Data') + ) + + g = GraphWidget() + g.plot(fig) + ``` + + Example 3 - Clear a graph widget + ``` + from plotly.graph_objs import Scatter, Figure + g = GraphWidget() + g.plot([Scatter(x=[1, 2, 3], y=[10, 15, 13])]) + + # Now clear it + g.plot({}) # alternatively, g.plot(Figure()) + ``` """ if figure_or_data == {} or figure_or_data == Figure(): validate = False