Skip to content

Commit 27f6413

Browse files
author
Jon M. Mease
committed
Add support for online/offline plot/iplot methods
It's now possible to execute a fair amount of legacy graph construction and plotting code
1 parent 6565372 commit 27f6413

File tree

4 files changed

+19
-22
lines changed

4 files changed

+19
-22
lines changed

plotly/basedatatypes.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -944,6 +944,9 @@ def to_dict(self):
944944

945945
return res
946946

947+
def to_plotly_json(self):
948+
return self.to_dict()
949+
947950
def save_html(self, filename, auto_open=False, responsive=False):
948951
data = self.to_dict()
949952
if responsive:
@@ -1283,6 +1286,9 @@ def _props(self):
12831286
# Get data from parent's dict
12841287
return self.parent._get_child_props(self)
12851288

1289+
def to_plotly_json(self):
1290+
return deepcopy(self._props)
1291+
12861292
def _init_props(self):
12871293
# Ensure that _data is initialized.
12881294
if self._props is not None:

plotly/offline/offline.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -150,16 +150,8 @@ def init_notebook_mode(connected=False):
150150

151151
def _plot_html(figure_or_data, config, validate, default_width,
152152
default_height, global_requirejs):
153-
# force no validation if frames is in the call
154-
# TODO - add validation for frames in call - #605
155-
if 'frames' in figure_or_data:
156-
figure = tools.return_figure_from_figure_or_data(
157-
figure_or_data, False
158-
)
159-
else:
160-
figure = tools.return_figure_from_figure_or_data(
161-
figure_or_data, validate
162-
)
153+
154+
figure = tools.return_figure_from_figure_or_data(figure_or_data, validate)
163155

164156
width = figure.get('layout', {}).get('width', default_width)
165157
height = figure.get('layout', {}).get('height', default_height)

plotly/plotly/plotly.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -714,14 +714,7 @@ def get(figure_or_data, format='png', width=None, height=None, scale=None):
714714
715715
"""
716716
# TODO: format is a built-in name... we shouldn't really use it
717-
if isinstance(figure_or_data, dict):
718-
figure = figure_or_data
719-
elif isinstance(figure_or_data, list):
720-
figure = {'data': figure_or_data}
721-
else:
722-
raise exceptions.PlotlyEmptyDataError(
723-
"`figure_or_data` must be a dict or a list."
724-
)
717+
figure = tools.return_figure_from_figure_or_data(figure_or_data, True)
725718

726719
if format not in ['png', 'svg', 'jpeg', 'pdf']:
727720
raise exceptions.PlotlyError(

plotly/tools.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1440,18 +1440,24 @@ def _repr_html_(self):
14401440

14411441
def return_figure_from_figure_or_data(figure_or_data, validate_figure):
14421442
from plotly.graph_objs import graph_objs
1443+
1444+
validated = False
14431445
if isinstance(figure_or_data, dict):
14441446
figure = figure_or_data
14451447
elif isinstance(figure_or_data, list):
14461448
figure = {'data': figure_or_data}
1449+
elif isinstance(figure_or_data, graph_objs.Figure):
1450+
figure = figure_or_data.to_dict()
1451+
validated = True
14471452
else:
14481453
raise exceptions.PlotlyError("The `figure_or_data` positional "
1449-
"argument must be either "
1450-
"`dict`-like or `list`-like.")
1451-
if validate_figure:
1454+
"argument must be "
1455+
"`dict`-like, `list`-like, or an instance of plotly.graph_objs.Figure")
1456+
1457+
if validate_figure and not validated:
14521458

14531459
try:
1454-
graph_objs.Figure(figure)
1460+
graph_objs.Figure(**figure)
14551461
except exceptions.PlotlyError as err:
14561462
raise exceptions.PlotlyError("Invalid 'figure_or_data' argument. "
14571463
"Plotly will not be able to properly "

0 commit comments

Comments
 (0)