Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 767d01c

Browse files
committed
http://i.imgur.com/7y6JoyU.png
1 parent ce5b42c commit 767d01c

File tree

2 files changed

+197
-0
lines changed

2 files changed

+197
-0
lines changed

plotly/offline/__init__.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
"""
2+
offline
3+
======
4+
This module provides offline functionality.
5+
"""
6+
from . offline import (
7+
download_plotlyjs,
8+
init_notebook_mode,
9+
iplot
10+
)

plotly/offline/offline.py

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
""" Plotly Offline
2+
A module to use Plotly's graphing library with Python
3+
without connecting to a public or private plotly enterprise
4+
server.
5+
"""
6+
from __future__ import absolute_import
7+
8+
import uuid
9+
import json
10+
import os
11+
import requests
12+
13+
import plotly.plotly as py
14+
from plotly import utils
15+
from plotly import tools
16+
from plotly.exceptions import PlotlyError
17+
18+
_PLOTLY_OFFLINE_DIRECTORY = plotlyjs_path = os.path.expanduser(
19+
os.path.join(*'~/.plotly/plotlyjs'.split('/')))
20+
_PLOTLY_OFFLINE_BUNDLE = os.path.join(_PLOTLY_OFFLINE_DIRECTORY,
21+
'plotly-ipython-offline-bundle.js')
22+
23+
__PLOTLY_OFFLINE_INITIALIZED = False
24+
25+
26+
def download_plotlyjs(download_url):
27+
if not os.path.exists(plotlyjs_path):
28+
os.makedirs(plotlyjs_path)
29+
30+
res = requests.get(download_url)
31+
res.raise_for_status()
32+
33+
with open(_PLOTLY_OFFLINE_BUNDLE, 'wb') as f:
34+
f.write(res.content)
35+
36+
print('\n'.join([
37+
'Success! Now start an IPython notebook and run the following ' +
38+
'code to make your first offline graph:',
39+
'',
40+
'import plotly',
41+
'plotly.offline.init_notebook_mode() '
42+
'# run at the start of every ipython notebook',
43+
'plotly.offline.iplot([{"x": [1, 2, 3], "y": [3, 1, 6]}])'
44+
]))
45+
46+
47+
def init_notebook_mode():
48+
"""
49+
Initialize Plotly Offline mode in an IPython Notebook.
50+
Run this function at the start of an IPython notebook
51+
to load the necessary javascript files for creating
52+
Plotly graphs with plotly.offline.iplot.
53+
"""
54+
if not tools._ipython_imported:
55+
raise ImportError('`iplot` can only run inside an IPython Notebook.')
56+
from IPython.display import HTML, display
57+
58+
if not os.path.exists(_PLOTLY_OFFLINE_BUNDLE):
59+
raise PlotlyError('Plotly Offline source file at {source_path} '
60+
'is not found.\n'
61+
'If you have a Plotly Offline license, then try '
62+
'running plotly.offline.download_plotlyjs(url) '
63+
'with a licensed download url.\n'
64+
"Don't have a Plotly Offline license? "
65+
'Contact [email protected] learn more about licensing.\n'
66+
'Questions? [email protected].'
67+
.format(source_path=_PLOTLY_OFFLINE_BUNDLE))
68+
69+
global __PLOTLY_OFFLINE_INITIALIZED
70+
__PLOTLY_OFFLINE_INITIALIZED = True
71+
display(HTML('<script type="text/javascript">' +
72+
open(_PLOTLY_OFFLINE_BUNDLE).read() + '</script>'))
73+
74+
75+
def iplot(figure_or_data, show_link=True, link_text='Export to plot.ly'):
76+
"""
77+
Draw plotly graphs inside an IPython notebook without
78+
connecting to an external server.
79+
To save the chart to Plotly Cloud or Plotly Enterprise, use
80+
`plotly.plotly.iplot`.
81+
To embed an image of the chart, use `plotly.image.ishow`.
82+
83+
figure_or_data -- a plotly.graph_objs.Figure or plotly.graph_objs.Data or
84+
dict or list that describes a Plotly graph.
85+
See https://plot.ly/python/ for examples of
86+
graph descriptions.
87+
88+
Keyword arguments:
89+
show_link (default=True) -- display a link in the bottom-right corner of
90+
of the chart that will export the chart to
91+
Plotly Cloud or Plotly Enterprise
92+
link_text (default='Export to plot.ly') -- the text of export link
93+
94+
Example:
95+
```
96+
from plotly.offline import init_notebook_mode, iplot
97+
init_notebook_mode()
98+
99+
iplot([{'x': [1, 2, 3], 'y': [5, 2, 7]}])
100+
```
101+
"""
102+
if not __PLOTLY_OFFLINE_INITIALIZED:
103+
raise PlotlyError('\n'.join([
104+
'Plotly Offline mode has not been initialized in this notebook. '
105+
'Run: ',
106+
'',
107+
'import plotly',
108+
'plotly.offline.init_notebook_mode() '
109+
'# run at the start of every ipython notebook',
110+
]))
111+
if not tools._ipython_imported:
112+
raise ImportError('`iplot` can only run inside an IPython Notebook.')
113+
114+
from IPython.display import HTML, display
115+
if isinstance(figure_or_data, dict):
116+
data = figure_or_data['data']
117+
layout = figure_or_data.get('layout', {})
118+
else:
119+
data = figure_or_data
120+
layout = {}
121+
122+
width = layout.get('width', '100%')
123+
height = layout.get('height', 525)
124+
try:
125+
float(width)
126+
except (ValueError, TypeError):
127+
pass
128+
else:
129+
width = str(width) + 'px'
130+
131+
try:
132+
float(width)
133+
except (ValueError, TypeError):
134+
pass
135+
else:
136+
width = str(width) + 'px'
137+
138+
plotdivid = uuid.uuid4()
139+
jdata = json.dumps(data, cls=utils.PlotlyJSONEncoder)
140+
jlayout = json.dumps(layout, cls=utils.PlotlyJSONEncoder)
141+
142+
if show_link is False:
143+
link_text = ''
144+
145+
plotly_platform_url = py.get_config().get('plotly_domain',
146+
'https://plot.ly')
147+
if (plotly_platform_url != 'https://plot.ly' and
148+
link_text == 'Export to plot.ly'):
149+
150+
link_domain = plotly_platform_url\
151+
.replace('https://', '')\
152+
.replace('http://', '')
153+
link_text = link_text.replace('plot.ly', link_domain)
154+
155+
display(HTML(
156+
'<script type="text/javascript">'
157+
'window.PLOTLYENV={"BASE_URL": "' + plotly_platform_url + '"};'
158+
'Plotly.LINKTEXT = "' + link_text + '";'
159+
'</script>'
160+
))
161+
162+
script = '\n'.join([
163+
'Plotly.plot("{id}", {data}, {layout}).then(function() {{',
164+
' $(".{id}.loading").remove();',
165+
'}})'
166+
]).format(id=plotdivid,
167+
data=jdata,
168+
layout=jlayout,
169+
link_text=link_text)
170+
171+
display(HTML(''
172+
'<div class="{id} loading" style="color: rgb(50,50,50);">'
173+
'Drawing...</div>'
174+
'<div id="{id}" style="height: {height}; width: {width};" '
175+
'class="plotly-graph-div">'
176+
'</div>'
177+
'<script type="text/javascript">'
178+
'{script}'
179+
'</script>'
180+
''.format(id=plotdivid, script=script,
181+
height=height, width=width)))
182+
183+
184+
def plot():
185+
""" Configured to work with localhost Plotly graph viewer
186+
"""
187+
raise NotImplementedError

0 commit comments

Comments
 (0)