|
| 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 | +import utils |
| 7 | +import uuid |
| 8 | +import json |
| 9 | +import os |
| 10 | +import requests |
| 11 | + |
| 12 | + |
| 13 | +def download_plotlyjs(download_url, plotlyjs_dir='~/.plotly/plotlyjs'): |
| 14 | + plotlyjs_path = os.path.expanduser( |
| 15 | + os.path.join(*'~/.plotly/plotlyjs'.split('/'))) |
| 16 | + |
| 17 | + if not os.path.exists(plotlyjs_path): |
| 18 | + os.makedirs(plotlyjs_path) |
| 19 | + |
| 20 | + res = requests.get(download_url + '/sourcefiles.json') |
| 21 | + res.raise_for_status() |
| 22 | + |
| 23 | + with open(os.path.join(plotlyjs_path, 'sourcefiles.json'), 'w') as f: |
| 24 | + f.write(res.content) |
| 25 | + |
| 26 | + download_queue = json.loads(res.content)['files'] |
| 27 | + for fn in download_queue: |
| 28 | + file_url = download_url + '/' + fn |
| 29 | + print('Downloading {}'.format(file_url)) |
| 30 | + res = requests.get(file_url) |
| 31 | + res.raise_for_status() |
| 32 | + |
| 33 | + file_path = os.path.join(plotlyjs_path, *fn.split('/')) |
| 34 | + file_dir = os.path.dirname(file_path) |
| 35 | + if not os.path.exists(file_dir): |
| 36 | + os.makedirs(file_dir) |
| 37 | + |
| 38 | + with open(file_path, 'w') as f: |
| 39 | + print('Copying into {}'.format(file_path)) |
| 40 | + f.write(res.content) |
| 41 | + |
| 42 | + print('\n\n' |
| 43 | + 'Success! Now start an IPython notebook, ' |
| 44 | + 'initialize offline mode with\n' |
| 45 | + 'plotly.offline.init_notebook_mode()\n' |
| 46 | + 'and make your first offline graph:\n' |
| 47 | + 'plotly.offline.iplot([{"x": [1, 2, 3], "y": [3, 1, 6]}])\n') |
| 48 | + |
| 49 | + |
| 50 | +def init_notebook_mode(plotlyjs_dir='~/.plotly/plotlyjs'): |
| 51 | + # TODO: check if ipython is available...? |
| 52 | + from IPython.display import HTML, display |
| 53 | + |
| 54 | + # Split and join the install path for cross-OS directories |
| 55 | + plotlyjs_dir = os.path.expanduser(os.path.join(*'~/.plotly/plotlyjs'.split('/'))) |
| 56 | + sourcefiles_path = os.path.join(plotlyjs_dir, 'sourcefiles.json') |
| 57 | + if not os.path.exists(sourcefiles_path): |
| 58 | + raise Exception('Plotly Offline configuration file at {source_path} ' |
| 59 | + 'is not found.\n' |
| 60 | + 'If you have a Plotly Offline license, then try ' |
| 61 | + 'running plotly.offline.configure_offline(url) ' |
| 62 | + 'with a licensed download url.\n' |
| 63 | + "Don't have a Plotly Offline license?" |
| 64 | + 'Contact [email protected] learn more about licensing.\n' |
| 65 | + |
| 66 | + .format(source_path=sourcefiles_path)) |
| 67 | + |
| 68 | + sourcefiles = json.load(open(sourcefiles_path))['files'] |
| 69 | + |
| 70 | + # Include all of the files in the dependencies folder |
| 71 | + scriptpaths = [os.path.join(plotlyjs_dir, *sourcefile.split('/')) |
| 72 | + for sourcefile in sourcefiles] |
| 73 | + |
| 74 | + unrequirejs = ('<script type="text/javascript">' |
| 75 | + 'require=requirejs=define=undefined;</script>') |
| 76 | + |
| 77 | + display(HTML( |
| 78 | + unrequirejs + |
| 79 | + '\n'.join([ |
| 80 | + '<script type="text/javascript">' + open(script).read() + |
| 81 | + '</script>' for script in scriptpaths |
| 82 | + ]) |
| 83 | + )) |
| 84 | + |
| 85 | + |
| 86 | +def iplot(figure_or_data, show_link=True, link_text='Export to plot.ly'): |
| 87 | + """ |
| 88 | + """ |
| 89 | + from IPython.display import HTML, display |
| 90 | + if isinstance(figure_or_data, dict): |
| 91 | + data = figure_or_data['data'] |
| 92 | + layout = figure_or_data.get('layout', {}) |
| 93 | + else: |
| 94 | + data = figure_or_data |
| 95 | + layout = {} |
| 96 | + |
| 97 | + width = layout.get('width', '100%') |
| 98 | + height = layout.get('height', 525) |
| 99 | + try: |
| 100 | + float(width) |
| 101 | + width = str(width) + 'px' |
| 102 | + except: |
| 103 | + pass |
| 104 | + try: |
| 105 | + float(height) |
| 106 | + height = str(height) + 'px' |
| 107 | + except: |
| 108 | + pass |
| 109 | + |
| 110 | + plotdivid = uuid.uuid4() |
| 111 | + jdata = json.dumps(data, cls=utils.PlotlyJSONEncoder) |
| 112 | + jlayout = json.dumps(layout, cls=utils.PlotlyJSONEncoder) |
| 113 | + |
| 114 | + script = '\n'.join([ |
| 115 | + 'Plotly.plot("{id}", {data}, {layout}).then(function() {{', |
| 116 | + ' $(".{id}.loading").remove();', |
| 117 | + ' $(".link--embedview").text("{link_text}");' |
| 118 | + ]).format(id=plotdivid, |
| 119 | + data=jdata, |
| 120 | + layout=jlayout, |
| 121 | + link_text=link_text) |
| 122 | + |
| 123 | + if not show_link: |
| 124 | + script += '\n'.join([ |
| 125 | + ' $("{} .link--embedview").remove();'.format(plotdivid), |
| 126 | + '\n});']) |
| 127 | + else: |
| 128 | + script += '\n});' |
| 129 | + |
| 130 | + display(HTML('' |
| 131 | + '<div class="{id} loading" style="color: rgb(50,50,50);">' |
| 132 | + 'Drawing...</div>' |
| 133 | + '<div id="{id}" style="height: {height}; width: {width};" ' |
| 134 | + 'class="plotly-graph-div">' |
| 135 | + '</div>' |
| 136 | + '<script type="text/javascript">' |
| 137 | + '{script}' |
| 138 | + '</script>' |
| 139 | + ''.format(id=plotdivid, script=script, |
| 140 | + height=height, width=width))) |
| 141 | + |
| 142 | + |
| 143 | +def plot(): |
| 144 | + """ Configured to work with localhost Plotly graph viewer |
| 145 | + """ |
| 146 | + raise NotImplementedError |
| 147 | + |
0 commit comments