Skip to content

Reduce size V3 #688

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 4 commits 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
3 changes: 2 additions & 1 deletion plotly/offline/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
iplot,
iplot_mpl,
plot,
plot_mpl
plot_mpl,
serve_plotlyjs_from_directory
)
35 changes: 33 additions & 2 deletions plotly/offline/offline.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from __future__ import absolute_import

import os
import shutil
import uuid
import warnings
from pkg_resources import resource_string
Expand All @@ -23,6 +24,7 @@
matplotlib = optional_imports.get_module('matplotlib')

__PLOTLY_OFFLINE_INITIALIZED = False
__SERVE_PLOTLYJS_FROM_DIRECTORY = False

__IMAGE_FORMATS = ['jpeg', 'png', 'webp', 'svg']

Expand All @@ -37,8 +39,12 @@ def download_plotlyjs(download_url):


def get_plotlyjs():
path = os.path.join('package_data', 'plotly.min.js')
plotlyjs = resource_string('plotly', path).decode('utf-8')
global __SERVE_PLOTLYJS_FROM_DIRECTORY
if __SERVE_PLOTLYJS_FROM_DIRECTORY:
plotlyjs = '</script><script src="plotly.min.js">'
else:
path = os.path.join('offline', 'plotly.min.js')
plotlyjs = resource_string('plotly', path).decode('utf-8')
return plotlyjs

def get_image_download_script(caller):
Expand Down Expand Up @@ -77,6 +83,19 @@ def get_image_download_script(caller):
)


def serve_plotlyjs_from_directory():
"""
Sets global plotly.offline.__SERVE_PLOTLYJS_FROM_DIRECTORY to True,
which will trigger the separation of the html and the plotly.min.js
into two files. This will reduce the overall space required
if muliple plots are created in the same directory, since only one
copy of plotly.min.js will be stored.
"""
global __SERVE_PLOTLYJS_FROM_DIRECTORY
__SERVE_PLOTLYJS_FROM_DIRECTORY = True
return


def init_notebook_mode(connected=False):
"""
Initialize plotly.js in the browser if it hasn't been loaded into the DOM
Expand Down Expand Up @@ -461,6 +480,18 @@ def plot(figure_or_data, show_link=True, link_text='Export to plot.ly',
).format(id=plotdivid)

if output_type == 'file':
src_path = os.path.join(
os.path.dirname(__file__), 'plotly.min.js'
)
# src_path = resource_string('plotly', path)
global __SERVE_PLOTLYJS_FROM_DIRECTORY
if __SERVE_PLOTLYJS_FROM_DIRECTORY:
dest_path = os.path.join(
os.path.dirname( filename ), 'plotly.min.js'
)
if os.path.exists( dest_path ) is False:
shutil.copy( src_path, dest_path )

with open(filename, 'w') as f:
if include_plotlyjs:
plotly_js_script = ''.join([
Expand Down