Skip to content

Reduce plot size #501

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 3 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
)
38 changes: 35 additions & 3 deletions plotly/offline/offline.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import json
import os
import shutil
import uuid
import warnings
from pkg_resources import resource_string
Expand All @@ -30,6 +31,7 @@
_matplotlib_imported = False

__PLOTLY_OFFLINE_INITIALIZED = False
__SERVE_PLOTLYJS_FROM_DIRECTORY = False


def download_plotlyjs(download_url):
Expand All @@ -42,11 +44,28 @@ def download_plotlyjs(download_url):


def get_plotlyjs():
path = os.path.join('offline', '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 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 @@ -242,7 +261,8 @@ def plot(figure_or_data,
validate=True, output_type='file',
include_plotlyjs=True,
filename='temp-plot.html',
auto_open=True):
auto_open=True,
):
""" Create a plotly graph locally as an HTML document or string.

Example:
Expand Down Expand Up @@ -315,6 +335,18 @@ def plot(figure_or_data,
).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