Skip to content

Commit cb4552c

Browse files
committed
iplot in offline works with frames
1 parent e8dc76e commit cb4552c

File tree

1 file changed

+40
-11
lines changed

1 file changed

+40
-11
lines changed

plotly/offline/offline.py

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,15 @@ def init_notebook_mode(connected=False):
162162

163163
def _plot_html(figure_or_data, show_link, link_text, validate,
164164
default_width, default_height, global_requirejs):
165-
166-
figure = tools.return_figure_from_figure_or_data(figure_or_data, validate)
165+
# force no validation if frames is in the call
166+
if 'frames' in figure_or_data:
167+
figure = tools.return_figure_from_figure_or_data(
168+
figure_or_data, False
169+
)
170+
else:
171+
figure = tools.return_figure_from_figure_or_data(
172+
figure_or_data, validate
173+
)
167174

168175
width = figure.get('layout', {}).get('width', default_width)
169176
height = figure.get('layout', {}).get('height', default_height)
@@ -185,6 +192,8 @@ def _plot_html(figure_or_data, show_link, link_text, validate,
185192
plotdivid = uuid.uuid4()
186193
jdata = json.dumps(figure.get('data', []), cls=utils.PlotlyJSONEncoder)
187194
jlayout = json.dumps(figure.get('layout', {}), cls=utils.PlotlyJSONEncoder)
195+
if 'frames' in figure_or_data:
196+
jframes = json.dumps(figure.get('frames', {}), cls=utils.PlotlyJSONEncoder)
188197

189198
config = {}
190199
config['showLink'] = show_link
@@ -204,11 +213,30 @@ def _plot_html(figure_or_data, show_link, link_text, validate,
204213
link_text = link_text.replace('plot.ly', link_domain)
205214
config['linkText'] = link_text
206215

207-
script = 'Plotly.newPlot("{id}", {data}, {layout}, {config})'.format(
208-
id=plotdivid,
209-
data=jdata,
210-
layout=jlayout,
211-
config=jconfig)
216+
if 'frames' in figure_or_data:
217+
script = '''
218+
Plotly.plot(
219+
'{id}',
220+
{data},
221+
{layout},
222+
{config}
223+
).then(function () {add_frames}).then(function(){animate})
224+
'''.format(
225+
id=plotdivid,
226+
data=jdata,
227+
layout=jlayout,
228+
config=jconfig,
229+
add_frames="{" + "return Plotly.addFrames('{id}',{frames}".format(
230+
id=plotdivid, frames=jframes
231+
) + ");}",
232+
animate="{" + "Plotly.animate('{id}');".format(id=plotdivid) + "}"
233+
)
234+
else:
235+
script = 'Plotly.newPlot("{id}", {data}, {layout}, {config})'.format(
236+
id=plotdivid,
237+
data=jdata,
238+
layout=jlayout,
239+
config=jconfig)
212240

213241
optional_line1 = ('require(["plotly"], function(Plotly) {{ '
214242
if global_requirejs else '')
@@ -232,6 +260,7 @@ def _plot_html(figure_or_data, show_link, link_text, validate,
232260

233261
return plotly_html_div, plotdivid, width, height
234262

263+
235264
def iplot(figure_or_data, show_link=True, link_text='Export to plot.ly',
236265
validate=True, image=None, filename='plot_image', image_width=800,
237266
image_height=600):
@@ -301,10 +330,10 @@ def iplot(figure_or_data, show_link=True, link_text='Export to plot.ly',
301330
)
302331
# if image is given, and is a valid format, we will download the image
303332
script = get_image_download_script('iplot').format(format=image,
304-
width=image_width,
305-
height=image_height,
306-
filename=filename,
307-
plot_id=plotdivid)
333+
width=image_width,
334+
height=image_height,
335+
filename=filename,
336+
plot_id=plotdivid)
308337
# allow time for the plot to draw
309338
time.sleep(1)
310339
# inject code to download an image of the plot

0 commit comments

Comments
 (0)