<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import <EOL> import json <EOL> import os <EOL> import uuid <EOL> import warnings <EOL> from pkg_resources import resource_string <EOL> import webbrowser <EOL> import plotly <EOL> from plotly import tools , utils <EOL> from plotly . exceptions import PlotlyError <EOL> try : <EOL> import IPython <EOL> _ipython_imported = True <EOL> except ImportError : <EOL> _ipython_imported = False <EOL> try : <EOL> import matplotlib <EOL> _matplotlib_imported = True <EOL> except ImportError : <EOL> _matplotlib_imported = False <EOL> __PLOTLY_OFFLINE_INITIALIZED = False <EOL> def download_plotlyjs ( download_url ) : <EOL> warnings . warn ( '''<STR_LIT>''' , DeprecationWarning ) <EOL> pass <EOL> def get_plotlyjs ( ) : <EOL> path = os . path . join ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> plotlyjs = resource_string ( '<STR_LIT>' , path ) . decode ( '<STR_LIT:utf-8>' ) <EOL> return plotlyjs <EOL> def init_notebook_mode ( ) : <EOL> """<STR_LIT>""" <EOL> if not tools . _ipython_imported : <EOL> raise ImportError ( '<STR_LIT>' ) <EOL> from IPython . display import HTML , display <EOL> global __PLOTLY_OFFLINE_INITIALIZED <EOL> if not __PLOTLY_OFFLINE_INITIALIZED : <EOL> display ( HTML ( "<STR_LIT>" + <EOL> "<STR_LIT>" + <EOL> get_plotlyjs ( ) + <EOL> "<STR_LIT>" + <EOL> "<STR_LIT>" + <EOL> "<STR_LIT>" + <EOL> "<STR_LIT>" + <EOL> "<STR_LIT>" ) ) <EOL> __PLOTLY_OFFLINE_INITIALIZED = True <EOL> def _plot_html ( figure_or_data , show_link , link_text , <EOL> validate , default_width , default_height , global_requirejs ) : <EOL> figure = tools . return_figure_from_figure_or_data ( figure_or_data , validate ) <EOL> width = figure . get ( '<STR_LIT>' , { } ) . get ( '<STR_LIT:width>' , default_width ) <EOL> height = figure . get ( '<STR_LIT>' , { } ) . get ( '<STR_LIT>' , default_height ) <EOL> try : <EOL> float ( width ) <EOL> except ( ValueError , TypeError ) : <EOL> pass <EOL> else : <EOL> width = str ( width ) + '<STR_LIT>' <EOL> try : <EOL> float ( width ) <EOL> except ( ValueError , TypeError ) : <EOL> pass <EOL> else : <EOL> width = str ( width ) + '<STR_LIT>' <EOL> plotdivid = uuid . uuid4 ( ) <EOL> jdata = json . dumps ( figure . get ( '<STR_LIT:data>' , [ ] ) , cls = utils . PlotlyJSONEncoder ) <EOL> jlayout = json . dumps ( figure . get ( '<STR_LIT>' , { } ) , cls = utils . PlotlyJSONEncoder ) <EOL> config = { } <EOL> config [ '<STR_LIT>' ] = show_link <EOL> config [ '<STR_LIT>' ] = link_text <EOL> jconfig = json . dumps ( config ) <EOL> plotly_platform_url = plotly . plotly . get_config ( ) . get ( '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> if ( plotly_platform_url != '<STR_LIT>' and <EOL> link_text == '<STR_LIT>' ) : <EOL> link_domain = plotly_platform_url . replace ( '<STR_LIT>' , '<STR_LIT>' ) . replace ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> link_text = link_text . replace ( '<STR_LIT>' , link_domain ) <EOL> script = '<STR_LIT>' . format ( <EOL> id = plotdivid , <EOL> data = jdata , <EOL> layout = jlayout , <EOL> config = jconfig ) <EOL> optional_line1 = ( '<STR_LIT>' <EOL> if global_requirejs else '<STR_LIT>' ) <EOL> optional_line2 = '<STR_LIT>' if global_requirejs else '<STR_LIT>' <EOL> plotly_html_div = ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' + <EOL> optional_line1 + <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' + plotly_platform_url + '<STR_LIT>' <EOL> '<STR_LIT>' + <EOL> optional_line2 + <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) . format ( <EOL> id = plotdivid , script = script , <EOL> height = height , width = width ) <EOL> return plotly_html_div , plotdivid , width , height <EOL> def iplot ( figure_or_data , show_link = True , link_text = '<STR_LIT>' , <EOL> validate = True ) : <EOL> """<STR_LIT>""" <EOL> if not __PLOTLY_OFFLINE_INITIALIZED : <EOL> raise PlotlyError ( '<STR_LIT:\n>' . join ( [ <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> ] ) ) <EOL> if not tools . _ipython_imported : <EOL> raise ImportError ( '<STR_LIT>' ) <EOL> from IPython . display import HTML , display <EOL> plot_html , plotdivid , width , height = _plot_html ( <EOL> figure_or_data , show_link , link_text , validate , <EOL> '<STR_LIT>' , <NUM_LIT> , global_requirejs = True ) <EOL> display ( HTML ( plot_html ) ) <EOL> def plot ( figure_or_data , <EOL> show_link = True , link_text = '<STR_LIT>' , <EOL> validate = True , output_type = '<STR_LIT:file>' , <EOL> include_plotlyjs = True , <EOL> filename = '<STR_LIT>' , <EOL> auto_open = True ) : <EOL> """<STR_LIT>""" <EOL> if output_type not in [ '<STR_LIT>' , '<STR_LIT:file>' ] : <EOL> raise ValueError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" + output_type + "<STR_LIT>" ) <EOL> if not filename . endswith ( '<STR_LIT>' ) and output_type == '<STR_LIT:file>' : <EOL> warnings . warn ( <EOL> "<STR_LIT>" + filename + "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> filename += '<STR_LIT>' <EOL> plot_html , plotdivid , width , height = _plot_html ( <EOL> figure_or_data , show_link , link_text , validate , <EOL> '<STR_LIT>' , '<STR_LIT>' , global_requirejs = False ) <EOL> resize_script = '<STR_LIT>' <EOL> if width == '<STR_LIT>' or height == '<STR_LIT>' : <EOL> resize_script = ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) . format ( id = plotdivid ) <EOL> if output_type == '<STR_LIT:file>' : <EOL> with open ( filename , '<STR_LIT:w>' ) as f : <EOL> if include_plotlyjs : <EOL> plotly_js_script = '<STR_LIT>' . join ( [ <EOL> '<STR_LIT>' , <EOL> get_plotlyjs ( ) , <EOL> '<STR_LIT>' , <EOL> ] ) <EOL> else : <EOL> plotly_js_script = '<STR_LIT>' <EOL> f . write ( '<STR_LIT>' . join ( [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> plotly_js_script , <EOL> plot_html , <EOL> resize_script , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] ) ) <EOL> url = '<STR_LIT>' + os . path . abspath ( filename ) <EOL> if auto_open : <EOL> webbrowser . open ( url ) <EOL> return url <EOL> elif output_type == '<STR_LIT>' : <EOL> if include_plotlyjs : <EOL> return '<STR_LIT>' . join ( [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> get_plotlyjs ( ) , <EOL> '<STR_LIT>' , <EOL> plot_html , <EOL> '<STR_LIT>' <EOL> ] ) <EOL> else : <EOL> return plot_html <EOL> def plot_mpl ( mpl_fig , resize = False , strip_style = False , <EOL> verbose = False , show_link = True , link_text = '<STR_LIT>' , <EOL> validate = True , output_type = '<STR_LIT:file>' , include_plotlyjs = True , <EOL> filename = '<STR_LIT>' , auto_open = True ) : <EOL> """<STR_LIT>""" <EOL> plotly_plot = tools . mpl_to_plotly ( mpl_fig , resize , strip_style , verbose ) <EOL> return plot ( plotly_plot , show_link , link_text , validate , output_type , <EOL> include_plotlyjs , filename , auto_open ) <EOL> def iplot_mpl ( mpl_fig , resize = False , strip_style = False , <EOL> verbose = False , show_link = True , <EOL> link_text = '<STR_LIT>' , validate = True ) : <EOL> """<STR_LIT>""" <EOL> plotly_plot = tools . mpl_to_plotly ( mpl_fig , resize , strip_style , verbose ) <EOL> return iplot ( plotly_plot , show_link , link_text , validate ) <EOL> def enable_mpl_offline ( resize = False , strip_style = False , <EOL> verbose = False , show_link = True , <EOL> link_text = '<STR_LIT>' , validate = True ) : <EOL> """<STR_LIT>""" <EOL> if not __PLOTLY_OFFLINE_INITIALIZED : <EOL> init_notebook_mode ( ) <EOL> ip = IPython . core . getipython . get_ipython ( ) <EOL> formatter = ip . display_formatter . formatters [ '<STR_LIT>' ] <EOL> formatter . for_type ( matplotlib . figure . Figure , <EOL> lambda fig : iplot_mpl ( fig , resize , strip_style , verbose , <EOL> show_link , link_text , validate ) ) </s>
