Skip to content

Allow for the option of a CDN in offline mode #475

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

Merged
merged 5 commits into from
May 27, 2016
Merged
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
54 changes: 38 additions & 16 deletions plotly/offline/offline.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
_matplotlib_imported = False

__PLOTLY_OFFLINE_INITIALIZED = False
__PLOTLY_USE_CDN = False


def download_plotlyjs(download_url):
warnings.warn('''
Expand All @@ -46,7 +48,7 @@ def get_plotlyjs():
return plotlyjs


def init_notebook_mode():
def init_notebook_mode(connected=False):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a docstring comment explaining what connected is?

"""
Initialize plotly.js in the browser if it hasn't been loaded into the DOM
yet. This is an idempotent method and can and should be called from any
Expand All @@ -56,20 +58,40 @@ def init_notebook_mode():
raise ImportError('`iplot` can only run inside an IPython Notebook.')

global __PLOTLY_OFFLINE_INITIALIZED
# Inject plotly.js into the output cell
script_inject = (
''
'<script type=\'text/javascript\'>'
'if(!window.Plotly){{'
'define(\'plotly\', function(require, exports, module) {{'
'{script}'
'}});'
'require([\'plotly\'], function(Plotly) {{'
'window.Plotly = Plotly;'
'}});'
'}}'
'</script>'
'').format(script=get_plotlyjs())
global __PLOTLY_USE_CDN
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this a global variable? it looks like you're only using it in this function context


__PLOTLY_USE_CDN = connected

if connected:
# Inject plotly.js into the output cell
script_inject = (
''
'<script>'
'requirejs.config({'
'paths: { '
'\'plotly\': [\'https://cdn.plot.ly/plotly-latest.min\']},'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't this supposed to be https://cdn.plot.ly/plotly-latest.min.js? Are you missing the ".js"?

'});'
'if(!window.Plotly) {{'
'require([\'plotly\'],'
'function(plotly) {window.Plotly=plotly;});'
'}}'
'</script>'
)
else:
# Inject plotly.js into the output cell
script_inject = (
''
'<script type=\'text/javascript\'>'
'if(!window.Plotly){{'
'define(\'plotly\', function(require, exports, module) {{'
'{script}'
'}});'
'require([\'plotly\'], function(Plotly) {{'
'window.Plotly = Plotly;'
'}});'
'}}'
'</script>'
'').format(script=get_plotlyjs())

display(HTML(script_inject))
__PLOTLY_OFFLINE_INITIALIZED = True
Expand Down Expand Up @@ -126,7 +148,7 @@ def _plot_html(figure_or_data, show_link, link_text,

optional_line1 = ('require(["plotly"], function(Plotly) {{ '
if global_requirejs else '')
optional_line2 = '}});' if global_requirejs else ''
optional_line2 = ('}});' if global_requirejs else '')

plotly_html_div = (
''
Expand Down