Skip to content

Add json output type to offline plot. #704

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions plotly/offline/offline.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def init_notebook_mode(connected=False):


def _plot_html(figure_or_data, config, validate, default_width,
default_height, global_requirejs):
default_height, global_requirejs, output_type):
# force no validation if frames is in the call
# TODO - add validation for frames in call - #605
if 'frames' in figure_or_data:
Expand Down Expand Up @@ -207,6 +207,13 @@ def _plot_html(figure_or_data, config, validate, default_width,
config_clean = dict((k, config[k]) for k in configkeys if k in config)
jconfig = _json.dumps(config_clean)

if output_type == 'json':
if 'frames' in figure_or_data:
return (jdata, jlayout, jconfig, jframes)
else:
return (jdata, jlayout, jconfig)


# TODO: The get_config 'source of truth' should
# really be somewhere other than plotly.plotly
plotly_platform_url = plotly.plotly.get_config().get('plotly_domain',
Expand Down Expand Up @@ -332,7 +339,7 @@ def iplot(figure_or_data, show_link=True, link_text='Export to plot.ly',
config['linkText'] = link_text

plot_html, plotdivid, width, height = _plot_html(
figure_or_data, config, validate, '100%', 525, True
figure_or_data, config, validate, '100%', 525, True, output_type='html'
)

figure = tools.return_figure_from_figure_or_data(figure_or_data, validate)
Expand Down Expand Up @@ -400,12 +407,14 @@ def plot(figure_or_data, show_link=True, link_text='Export to plot.ly',
are valid? omit if your version of plotly.js has become outdated
with your version of graph_reference.json or if you need to include
extra, unnecessary keys in your figure.
output_type ('file' | 'div' - default 'file') -- if 'file', then
output_type ('file' | 'div' | 'json' - default 'file') -- if 'file', then
the graph is saved as a standalone HTML file and `plot`
returns None.
If 'div', then `plot` returns a string that just contains the
HTML <div> that contains the graph and the script to generate the
graph.
If 'json' then `plot` returns a tuple of JSON strings in the form of
(data, layout, config).
Use 'file' if you want to save and view a single graph at a time
in a standalone HTML file.
Use 'div' if you are embedding these graphs in an HTML file with
Expand All @@ -431,9 +440,9 @@ def plot(figure_or_data, show_link=True, link_text='Export to plot.ly',
image_height (default=600) -- Specifies the height of the image in `px`.
image_width (default=800) -- Specifies the width of the image in `px`.
"""
if output_type not in ['div', 'file']:
if output_type not in ['div', 'file', 'json']:
raise ValueError(
"`output_type` argument must be 'div' or 'file'. "
"`output_type` argument must be 'div', 'file', or 'json'. "
"You supplied `" + output_type + "``")
if not filename.endswith('.html') and output_type == 'file':
warnings.warn(
Expand All @@ -445,9 +454,13 @@ def plot(figure_or_data, show_link=True, link_text='Export to plot.ly',
config['showLink'] = show_link
config['linkText'] = link_text

if output_type == 'json':
return _plot_html(figure_or_data, config, validate,
'100%', '100%', global_requirejs=False, output_type=output_type)

plot_html, plotdivid, width, height = _plot_html(
figure_or_data, config, validate,
'100%', '100%', global_requirejs=False)
'100%', '100%', global_requirejs=False, output_type=output_type)

resize_script = ''
if width == '100%' or height == '100%':
Expand Down
7 changes: 7 additions & 0 deletions plotly/tests/test_core/test_offline/test_offline.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ def test_div_output(self):
self.assertTrue('<html>' not in html and '</html>' not in html)
self.assertTrue(html.startswith('<div>') and html.endswith('</div>'))

def test_json_output(self):
data, layout, config = plotly.offline.plot(fig, output_type='json', auto_open=False)

self.assertEqual(data, '[{"type": "scatter", "x": [1, 2, 3], "y": [10, 20, 30]}]')
self.assertEqual(layout, '{"title": "offline plot"}')
self.assertEqual(config, '{"showLink": true, "linkText": "Export to plot.ly"}')

def test_autoresizing(self):
resize_code_strings = [
'window.addEventListener("resize", ',
Expand Down