def activate(self): if self.global_init: if not ipython_display: raise ValueError( "The {cls} class requires ipython but it is not installed".format( cls=self.__class__.__name__ ) ) if not self.requirejs: raise ValueError("global_init is only supported with requirejs=True") if self.connected: # Connected so we configure requirejs with the plotly CDN script = """\ <script type="text/javascript"> {win_config} {mathjax_config} if (typeof require !== 'undefined') {{ require.undef("plotly"); requirejs.config({{ paths: {{ 'plotly': ['{plotly_cdn}'] }} }}); require(['plotly'], function(Plotly) {{ window._Plotly = Plotly; }}); }} </script> """.format( win_config=_window_plotly_config, mathjax_config=_mathjax_config, plotly_cdn=plotly_cdn_url().rstrip(".js"), ) else: # If not connected then we embed a copy of the plotly.js # library in the notebook script = """\ <script type="text/javascript"> {win_config} {mathjax_config} if (typeof require !== 'undefined') {{ require.undef("plotly"); define('plotly', function(require, exports, module) {{ {script} }}); require(['plotly'], function(Plotly) {{ window._Plotly = Plotly; }}); }} </script> """.format( script=get_plotlyjs(), win_config=_window_plotly_config, mathjax_config=_mathjax_config, ) ipython_display.display_html(script, raw=True)
def load_nb(cls, inline=True): """ Loads the plotly notebook resources. """ from IPython.display import publish_display_data cls._loaded = True init_notebook_mode(connected=not inline) publish_display_data(data={MIME_TYPES['jlab-hv-load']: get_plotlyjs()})
def load_nb(cls, inline=True): """ Loads the plotly notebook resources. """ from IPython.display import display, HTML, publish_display_data if not cls._loaded: display(HTML(PLOTLY_WARNING)) cls._loaded = True init_notebook_mode(connected=not inline) publish_display_data(data={MIME_TYPES['jlab-hv-load']: get_plotlyjs()})
def activate(self): if self.global_init: if not ipython_display: raise ValueError( 'The {cls} class requires ipython but it is not installed' .format(cls=self.__class__.__name__)) if not self.requirejs: raise ValueError( 'global_init is only supported with requirejs=True') if self.connected: # Connected so we configure requirejs with the plotly CDN script = """\ <script type="text/javascript"> {win_config} {mathjax_config} if (typeof require !== 'undefined') {{ require.undef("plotly"); requirejs.config({{ paths: {{ 'plotly': ['https://cdn.plot.ly/plotly-latest.min'] }} }}); require(['plotly'], function(Plotly) {{ window._Plotly = Plotly; }}); }} </script> """.format(win_config=_window_plotly_config, mathjax_config=_mathjax_config) else: # If not connected then we embed a copy of the plotly.js # library in the notebook script = """\ <script type="text/javascript"> {win_config} {mathjax_config} if (typeof require !== 'undefined') {{ require.undef("plotly"); define('plotly', function(require, exports, module) {{ {script} }}); require(['plotly'], function(Plotly) {{ window._Plotly = Plotly; }}); }} </script> """.format(script=get_plotlyjs(), win_config=_window_plotly_config, mathjax_config=_mathjax_config) ipython_display.display_html(script, raw=True)
def plotly_include(): return """ <script type="text/javascript"> require_=require;requirejs_=requirejs; define_=define; require=requirejs=define=undefined; </script> <script type="text/javascript"> {include} </script> <script type="text/javascript"> require=require_;requirejs=requirejs_; define=define_; </script>""".format(include=get_plotlyjs())
def write_html(fig, file, config=None, auto_play=True, include_plotlyjs=True, include_mathjax=False, post_script=None, full_html=True, animation_opts=None, validate=True, default_width='100%', default_height='100%', auto_open=False): """ Write a figure to an HTML file representation Parameters ---------- fig: Figure object or dict representing a figure file: str or writeable A string representing a local file path or a writeable object (e.g. an open file descriptor) config: dict or None (default None) Plotly.js figure config options auto_play: bool (default=True) Whether to automatically start the animation sequence on page load if the figure contains frames. Has no effect if the figure does not contain frames. include_plotlyjs: bool or string (default True) Specifies how the plotly.js library is included/loaded in the output div string. If True, a script tag containing the plotly.js source code (~3MB) is included in the output. HTML files generated with this option are fully self-contained and can be used offline. If 'cdn', a script tag that references the plotly.js CDN is included in the output. HTML files generated with this option are about 3MB smaller than those generated with include_plotlyjs=True, but they require an active internet connection in order to load the plotly.js library. If 'directory', a script tag is included that references an external plotly.min.js bundle that is assumed to reside in the same directory as the HTML file. If `file` is a string to a local file path and `full_html` is True then If 'directory', a script tag is included that references an external plotly.min.js bundle that is assumed to reside in the same directory as the HTML file. If `file` is a string to a local file path and `full_html` is True, then the plotly.min.js bundle is copied into the directory of the resulting HTML file. If a file named plotly.min.js already exists in the output directory then this file is left unmodified and no copy is performed. HTML files generated with this option can be used offline, but they require a copy of the plotly.min.js bundle in the same directory. This option is useful when many figures will be saved as HTML files in the same directory because the plotly.js source code will be included only once per output directory, rather than once per output file. If 'require', Plotly.js is loaded using require.js. This option assumes that require.js is globally available and that it has been globally configured to know how to find Plotly.js as 'plotly'. This option is not advised when full_html=True as it will result in a non-functional html file. If a string that ends in '.js', a script tag is included that references the specified path. This approach can be used to point the resulting HTML file to an alternative CDN or local bundle. If False, no script tag referencing plotly.js is included. This is useful when the resulting div string will be placed inside an HTML document that already loads plotly.js. This option is not advised when full_html=True as it will result in a non-functional html file. include_mathjax: bool or string (default False) Specifies how the MathJax.js library is included in the output html div string. MathJax is required in order to display labels with LaTeX typesetting. If False, no script tag referencing MathJax.js will be included in the output. If 'cdn', a script tag that references a MathJax CDN location will be included in the output. HTML div strings generated with this option will be able to display LaTeX typesetting as long as internet access is available. If a string that ends in '.js', a script tag is included that references the specified path. This approach can be used to point the resulting HTML div string to an alternative CDN. post_script: str or list or None (default None) JavaScript snippet(s) to be included in the resulting div just after plot creation. The string(s) may include '{plot_id}' placeholders that will then be replaced by the `id` of the div element that the plotly.js figure is associated with. One application for this script is to install custom plotly.js event handlers. full_html: bool (default True) If True, produce a string containing a complete HTML document starting with an <html> tag. If False, produce a string containing a single <div> element. animation_opts: dict or None (default None) dict of custom animation parameters to be passed to the function Plotly.animate in Plotly.js. See https://github.com/plotly/plotly.js/blob/master/src/plots/animation_attributes.js for available options. Has no effect if the figure does not contain frames, or auto_play is False. default_width, default_height: number or str (default '100%') The default figure width/height to use if the provided figure does not specify its own layout.width/layout.height property. May be specified in pixels as an integer (e.g. 500), or as a css width style string (e.g. '500px', '100%'). validate: bool (default True) True if the figure should be validated before being converted to JSON, False otherwise. auto_open: bool (default True If True, open the saved file in a web browser after saving. This argument only applies if `full_html` is True. Returns ------- str Representation of figure as an HTML div string """ # Build HTML string html_str = to_html( fig, config=config, auto_play=auto_play, include_plotlyjs=include_plotlyjs, include_mathjax=include_mathjax, post_script=post_script, full_html=full_html, animation_opts=animation_opts, default_width=default_width, default_height=default_height, validate=validate, ) # Check if file is a string file_is_str = isinstance(file, six.string_types) # Write HTML string if file_is_str: with open(file, 'w') as f: f.write(html_str) else: file.write(html_str) # Check if we should copy plotly.min.js to output directory if file_is_str and full_html and include_plotlyjs == 'directory': bundle_path = os.path.join(os.path.dirname(file), 'plotly.min.js') if not os.path.exists(bundle_path): with open(bundle_path, 'w') as f: f.write(get_plotlyjs()) # Handle auto_open if file_is_str and full_html and auto_open: url = 'file://' + os.path.abspath(file) webbrowser.open(url)
class PlotlyRenderer(Renderer): backend = param.String(default='plotly', doc="The backend name.") fig = param.ObjectSelector(default='auto', objects=['html', 'json', 'auto'], doc=""" Output render format for static figures. If None, no figure rendering will occur. """) mode_formats = { 'fig': { 'default': ['html', 'json'] }, 'holomap': { 'default': ['widgets', 'scrubber', 'auto'] } } widgets = { 'scrubber': PlotlyScrubberWidget, 'widgets': PlotlySelectionWidget } backend_dependencies = {'js': (get_plotlyjs(), )} comm_msg_handler = plotly_msg_handler _loaded = False def __call__(self, obj, fmt='html', divuuid=None): plot, fmt = self._validate(obj, fmt) mime_types = {'file-ext': fmt, 'mime_type': MIME_TYPES[fmt]} if isinstance(plot, tuple(self.widgets.values())): return plot(), mime_types elif fmt == 'html': return self._figure_data(plot, divuuid=divuuid), mime_types elif fmt == 'json': return self.diff(plot), mime_types def diff(self, plot, serialize=True): """ Returns a json diff required to update an existing plot with the latest plot data. """ diff = { 'data': plot.state.get('data', []), 'layout': plot.state.get('layout', {}) } if serialize: return json.dumps(diff, cls=utils.PlotlyJSONEncoder) else: return diff def _figure_data(self, plot, fmt=None, divuuid=None, comm=True, as_script=False, width=800, height=600): figure = plot.state if divuuid is None: divuuid = plot.id jdata = json.dumps(figure.get('data', []), cls=utils.PlotlyJSONEncoder) jlayout = json.dumps(figure.get('layout', {}), cls=utils.PlotlyJSONEncoder) config = {} config['showLink'] = False jconfig = json.dumps(config) if as_script: header = 'window.PLOTLYENV=window.PLOTLYENV || {};' else: header = ('<script type="text/javascript">' 'window.PLOTLYENV=window.PLOTLYENV || {};' '</script>') script = '\n'.join([ 'Plotly.plot("{id}", {data}, {layout}, {config}).then(function() {{', ' var elem = document.getElementById("{id}.loading"); elem.parentNode.removeChild(elem);', '}})' ]).format(id=divuuid, data=jdata, layout=jlayout, config=jconfig) html = ('<div id="{id}.loading" style="color: rgb(50,50,50);">' 'Drawing...</div>' '<div id="{id}" style="height: {height}; width: {width};" ' 'class="plotly-graph-div">' '</div>'.format(id=divuuid, height=height, width=width)) if as_script: return html, header + script content = ('{html}' '<script type="text/javascript">' ' {script}' '</script>').format(html=html, script=script) return '\n'.join([header, content]) @classmethod def plot_options(cls, obj, percent_size): factor = percent_size / 100.0 obj = obj.last if isinstance(obj, HoloMap) else obj plot = Store.registry[cls.backend].get(type(obj), None) options = plot.lookup_options(obj, 'plot').options width = options.get('width', plot.width) * factor height = options.get('height', plot.height) * factor return dict(options, **{'width': int(width), 'height': int(height)}) @classmethod def load_nb(cls, inline=True): """ Loads the plotly notebook resources. """ from IPython.display import display, HTML, publish_display_data if not cls._loaded: display(HTML(PLOTLY_WARNING)) cls._loaded = True init_notebook_mode(connected=not inline) publish_display_data(data={MIME_TYPES['jlab-hv-load']: get_plotlyjs()})
def to_html(fig, config=None, auto_play=True, include_plotlyjs=True, include_mathjax=False, post_script=None, full_html=True, animation_opts=None, default_width='100%', default_height='100%', validate=True): """ Convert a figure to an HTML string representation. Parameters ---------- fig: Figure object or dict representing a figure config: dict or None (default None) Plotly.js figure config options auto_play: bool (default=True) Whether to automatically start the animation sequence on page load if the figure contains frames. Has no effect if the figure does not contain frames. include_plotlyjs: bool or string (default True) Specifies how the plotly.js library is included/loaded in the output div string. If True, a script tag containing the plotly.js source code (~3MB) is included in the output. HTML files generated with this option are fully self-contained and can be used offline. If 'cdn', a script tag that references the plotly.js CDN is included in the output. HTML files generated with this option are about 3MB smaller than those generated with include_plotlyjs=True, but they require an active internet connection in order to load the plotly.js library. If 'directory', a script tag is included that references an external plotly.min.js bundle that is assumed to reside in the same directory as the HTML file. If 'require', Plotly.js is loaded using require.js. This option assumes that require.js is globally available and that it has been globally configured to know how to find Plotly.js as 'plotly'. This option is not advised when full_html=True as it will result in a non-functional html file. If a string that ends in '.js', a script tag is included that references the specified path. This approach can be used to point the resulting HTML file to an alternative CDN or local bundle. If False, no script tag referencing plotly.js is included. This is useful when the resulting div string will be placed inside an HTML document that already loads plotly.js. This option is not advised when full_html=True as it will result in a non-functional html file. include_mathjax: bool or string (default False) Specifies how the MathJax.js library is included in the output html div string. MathJax is required in order to display labels with LaTeX typesetting. If False, no script tag referencing MathJax.js will be included in the output. If 'cdn', a script tag that references a MathJax CDN location will be included in the output. HTML div strings generated with this option will be able to display LaTeX typesetting as long as internet access is available. If a string that ends in '.js', a script tag is included that references the specified path. This approach can be used to point the resulting HTML div string to an alternative CDN. post_script: str or list or None (default None) JavaScript snippet(s) to be included in the resulting div just after plot creation. The string(s) may include '{plot_id}' placeholders that will then be replaced by the `id` of the div element that the plotly.js figure is associated with. One application for this script is to install custom plotly.js event handlers. full_html: bool (default True) If True, produce a string containing a complete HTML document starting with an <html> tag. If False, produce a string containing a single <div> element. animation_opts: dict or None (default None) dict of custom animation parameters to be passed to the function Plotly.animate in Plotly.js. See https://github.com/plotly/plotly.js/blob/master/src/plots/animation_attributes.js for available options. Has no effect if the figure does not contain frames, or auto_play is False. default_width, default_height: number or str (default '100%') The default figure width/height to use if the provided figure does not specify its own layout.width/layout.height property. May be specified in pixels as an integer (e.g. 500), or as a css width style string (e.g. '500px', '100%'). validate: bool (default True) True if the figure should be validated before being converted to JSON, False otherwise. Returns ------- str Representation of figure as an HTML div string """ # ## Validate figure ## fig_dict = validate_coerce_fig_to_dict(fig, validate) # ## Generate div id ## plotdivid = str(uuid.uuid4()) # ## Serialize figure ## jdata = json.dumps(fig_dict.get('data', []), cls=utils.PlotlyJSONEncoder, sort_keys=True) jlayout = json.dumps(fig_dict.get('layout', {}), cls=utils.PlotlyJSONEncoder, sort_keys=True) if fig_dict.get('frames', None): jframes = json.dumps(fig_dict.get('frames', []), cls=utils.PlotlyJSONEncoder) else: jframes = None # ## Serialize figure config ## config = _get_jconfig(config) # Set responsive config.setdefault('responsive', True) jconfig = json.dumps(config) # Get div width/height layout_dict = fig_dict.get('layout', {}) template_dict = (fig_dict.get('layout', {}).get('template', {}).get('layout', {})) div_width = layout_dict.get('width', template_dict.get('width', default_width)) div_height = layout_dict.get('height', template_dict.get('height', default_height)) # Add 'px' suffix to numeric widths try: float(div_width) except (ValueError, TypeError): pass else: div_width = str(div_width) + 'px' try: float(div_height) except (ValueError, TypeError): pass else: div_height = str(div_height) + 'px' # ## Get platform URL ## plotly_platform_url = config.get('plotlyServerURL', 'https://plot.ly') # ## Build script body ## # This is the part that actually calls Plotly.js # build post script snippet(s) then_post_script = '' if post_script: if not isinstance(post_script, (list, tuple)): post_script = [post_script] for ps in post_script: then_post_script += """.then(function(){{ {post_script} }})""".format( post_script=ps.replace('{plot_id}', plotdivid)) then_addframes = '' then_animate = '' if jframes: then_addframes = """.then(function(){{ Plotly.addFrames('{id}', {frames}); }})""".format(id=plotdivid, frames=jframes) if auto_play: if animation_opts: animation_opts_arg = ', ' + json.dumps(animation_opts) else: animation_opts_arg = '' then_animate = """.then(function(){{ Plotly.animate('{id}', null{animation_opts}); }})""".format(id=plotdivid, animation_opts=animation_opts_arg) script = """ if (document.getElementById("{id}")) {{ Plotly.newPlot( '{id}', {data}, {layout}, {config} ){then_addframes}{then_animate}{then_post_script} }}""".format(id=plotdivid, data=jdata, layout=jlayout, config=jconfig, then_addframes=then_addframes, then_animate=then_animate, then_post_script=then_post_script) # ## Handle loading/initializing plotly.js ## include_plotlyjs_orig = include_plotlyjs if isinstance(include_plotlyjs, six.string_types): include_plotlyjs = include_plotlyjs.lower() # Start/end of requirejs block (if any) require_start = '' require_end = '' # Init and load load_plotlyjs = '' # Init plotlyjs. This block needs to run before plotly.js is loaded in # order for MathJax configuration to work properly if include_plotlyjs == 'require': require_start = 'require(["plotly"], function(Plotly) {' require_end = '});' elif include_plotlyjs == 'cdn': load_plotlyjs = """\ {win_config} <script src="https://cdn.plot.ly/plotly-latest.min.js"></script>\ """.format(win_config=_window_plotly_config) elif include_plotlyjs == 'directory': load_plotlyjs = """\ {win_config} <script src="plotly.min.js"></script>\ """.format(win_config=_window_plotly_config) elif (isinstance(include_plotlyjs, six.string_types) and include_plotlyjs.endswith('.js')): load_plotlyjs = """\ {win_config} <script src="{url}"></script>\ """.format(win_config=_window_plotly_config, url=include_plotlyjs_orig) elif include_plotlyjs: load_plotlyjs = """\ {win_config} <script type="text/javascript">{plotlyjs}</script>\ """.format(win_config=_window_plotly_config, plotlyjs=get_plotlyjs()) # ## Handle loading/initializing MathJax ## include_mathjax_orig = include_mathjax if isinstance(include_mathjax, six.string_types): include_mathjax = include_mathjax.lower() mathjax_template = """\ <script src="{url}?config=TeX-AMS-MML_SVG"></script>""" if include_mathjax == 'cdn': mathjax_script = mathjax_template.format( url=('https://cdnjs.cloudflare.com' '/ajax/libs/mathjax/2.7.5/MathJax.js')) + _mathjax_config elif (isinstance(include_mathjax, six.string_types) and include_mathjax.endswith('.js')): mathjax_script = mathjax_template.format( url=include_mathjax_orig) + _mathjax_config elif not include_mathjax: mathjax_script = '' else: raise ValueError("""\ Invalid value of type {typ} received as the include_mathjax argument Received value: {val} include_mathjax may be specified as False, 'cdn', or a string ending with '.js' """.format(typ=type(include_mathjax), val=repr(include_mathjax))) plotly_html_div = """\ <div> {mathjax_script} {load_plotlyjs} <div id="{id}" class="plotly-graph-div" \ style="height:{height}; width:{width};"></div> <script type="text/javascript"> {require_start} window.PLOTLYENV=window.PLOTLYENV || {{}}; window.PLOTLYENV.BASE_URL='{plotly_platform_url}'; {script}; {require_end} </script> </div>""".format(mathjax_script=mathjax_script, load_plotlyjs=load_plotlyjs, id=plotdivid, width=div_width, height=div_height, plotly_platform_url=plotly_platform_url, require_start=require_start, script=script, require_end=require_end) if full_html: return """\ <html> <head><meta charset="utf-8" /></head> <body> {div} </body> </html>""".format(div=plotly_html_div) else: return plotly_html_div
def get_context_data(self, **context): vegetarian_options = { k: v for k, v in Preference.objects.values("vegetarian") .annotate(Count("vegetarian")) .values_list("vegetarian", "vegetarian__count") } context["vegetarian"] = piechart( data=[vegetarian_options.get(k, 0) for k, _ in Preference.BOOL_CHOICES], labels=[v for _, v in Preference.BOOL_CHOICES], ) participative_kitchen = { k: v for k, v in Preference.objects.values("participative_kitchen") .annotate(Count("participative_kitchen")) .values_list("participative_kitchen", "participative_kitchen__count") } context["participative_kitchen"] = barchart( data=[ participative_kitchen.get(k, 0) for k, _ in Preference.ETHIC_CHOICES_F ], labels=[v for _, v in Preference.ETHIC_CHOICES_F], ) values = dict() values_order = dict() for value in ["organic", "sustainable", "fairtrade", "local", "zero_waste"]: toto = { k: v for k, v in Preference.objects.values(value) .annotate(Count(value)) .values_list(value, value + "__count") } values[value] = toto values_order[value] = sum( val * count for val, count in values[value].items() ) ordered_values = sorted(values, key=values_order.get) context["values"] = horizontal_barchart( data=[ (str(v), [values[val].get(k, 0) for val in ordered_values]) for k, v in Preference.ETHIC_CHOICES ], labels=[str(v) for v in ordered_values], ) entity_type = { k: v for k, v in Preference.objects.values("entity_type") .annotate(Count("entity_type")) .values_list("entity_type", "entity_type__count") } context["entity_type_label"] = PreferenceForm.Meta.labels["entity_type"] context["entity_type"] = barchart( data=[entity_type.get(k, 0) for k, _ in Preference.ENTITY_CHOICES], labels=[str(v) for _, v in Preference.ENTITY_CHOICES], ) price_markup = { k: v for k, v in Preference.objects.values("price_markup") .annotate(Count("price_markup")) .values_list("price_markup", "price_markup__count") } context["price_markup"] = barchart( data=[price_markup.get(k, 0) for k, _ in Preference.INCREASE_CHOICES], labels=[str(v) for _, v in Preference.INCREASE_CHOICES], ) context["plotlyjs"] = get_plotlyjs() return context
def to_html(fig, config=None, auto_play=True, include_plotlyjs=True, include_mathjax=False, post_script=None, full_html=True, animation_opts=None, default_width='100%', default_height='100%', validate=True): """ Convert a figure to an HTML string representation. Parameters ---------- fig: Figure object or dict representing a figure config: dict or None (default None) Plotly.js figure config options auto_play: bool (default=True) Whether to automatically start the animation sequence on page load if the figure contains frames. Has no effect if the figure does not contain frames. include_plotlyjs: bool or string (default True) Specifies how the plotly.js library is included/loaded in the output div string. If True, a script tag containing the plotly.js source code (~3MB) is included in the output. HTML files generated with this option are fully self-contained and can be used offline. If 'cdn', a script tag that references the plotly.js CDN is included in the output. HTML files generated with this option are about 3MB smaller than those generated with include_plotlyjs=True, but they require an active internet connection in order to load the plotly.js library. If 'directory', a script tag is included that references an external plotly.min.js bundle that is assumed to reside in the same directory as the HTML file. If 'require', Plotly.js is loaded using require.js. This option assumes that require.js is globally available and that it has been globally configured to know how to find Plotly.js as 'plotly'. This option is not advised when full_html=True as it will result in a non-functional html file. If a string that ends in '.js', a script tag is included that references the specified path. This approach can be used to point the resulting HTML file to an alternative CDN or local bundle. If False, no script tag referencing plotly.js is included. This is useful when the resulting div string will be placed inside an HTML document that already loads plotly.js. This option is not advised when full_html=True as it will result in a non-functional html file. include_mathjax: bool or string (default False) Specifies how the MathJax.js library is included in the output html div string. MathJax is required in order to display labels with LaTeX typesetting. If False, no script tag referencing MathJax.js will be included in the output. If 'cdn', a script tag that references a MathJax CDN location will be included in the output. HTML div strings generated with this option will be able to display LaTeX typesetting as long as internet access is available. If a string that ends in '.js', a script tag is included that references the specified path. This approach can be used to point the resulting HTML div string to an alternative CDN. post_script: str or list or None (default None) JavaScript snippet(s) to be included in the resulting div just after plot creation. The string(s) may include '{plot_id}' placeholders that will then be replaced by the `id` of the div element that the plotly.js figure is associated with. One application for this script is to install custom plotly.js event handlers. full_html: bool (default True) If True, produce a string containing a complete HTML document starting with an <html> tag. If False, produce a string containing a single <div> element. animation_opts: dict or None (default None) dict of custom animation parameters to be passed to the function Plotly.animate in Plotly.js. See https://github.com/plotly/plotly.js/blob/master/src/plots/animation_attributes.js for available options. Has no effect if the figure does not contain frames, or auto_play is False. default_width, default_height: number or str (default '100%') The default figure width/height to use if the provided figure does not specify its own layout.width/layout.height property. May be specified in pixels as an integer (e.g. 500), or as a css width style string (e.g. '500px', '100%'). validate: bool (default True) True if the figure should be validated before being converted to JSON, False otherwise. Returns ------- str Representation of figure as an HTML div string """ # ## Validate figure ## fig_dict = validate_coerce_fig_to_dict(fig, validate) # ## Generate div id ## plotdivid = str(uuid.uuid4()) # ## Serialize figure ## jdata = json.dumps( fig_dict.get('data', []), cls=utils.PlotlyJSONEncoder, sort_keys=True) jlayout = json.dumps( fig_dict.get('layout', {}), cls=utils.PlotlyJSONEncoder, sort_keys=True) if fig_dict.get('frames', None): jframes = json.dumps( fig_dict.get('frames', []), cls=utils.PlotlyJSONEncoder) else: jframes = None # ## Serialize figure config ## config = _get_jconfig(config) # Set responsive config.setdefault('responsive', True) jconfig = json.dumps(config) # Get div width/height layout_dict = fig_dict.get('layout', {}) template_dict = (fig_dict .get('layout', {}) .get('template', {}) .get('layout', {})) div_width = layout_dict.get( 'width', template_dict.get('width', default_width)) div_height = layout_dict.get( 'height', template_dict.get('height', default_height)) # Add 'px' suffix to numeric widths try: float(div_width) except (ValueError, TypeError): pass else: div_width = str(div_width) + 'px' try: float(div_height) except (ValueError, TypeError): pass else: div_height = str(div_height) + 'px' # ## Get platform URL ## plotly_platform_url = config.get('plotlyServerURL', 'https://plot.ly') # ## Build script body ## # This is the part that actually calls Plotly.js # build post script snippet(s) then_post_script = '' if post_script: if not isinstance(post_script, (list, tuple)): post_script = [post_script] for ps in post_script: then_post_script += """.then(function(){{ {post_script} }})""".format( post_script=ps.replace('{plot_id}', plotdivid)) then_addframes = '' then_animate = '' if jframes: then_addframes = """.then(function(){{ Plotly.addFrames('{id}', {frames}); }})""".format(id=plotdivid, frames=jframes) if auto_play: if animation_opts: animation_opts_arg = ', ' + json.dumps(animation_opts) else: animation_opts_arg = '' then_animate = """.then(function(){{ Plotly.animate('{id}', null{animation_opts}); }})""".format(id=plotdivid, animation_opts=animation_opts_arg) script = """ if (document.getElementById("{id}")) {{ Plotly.newPlot( '{id}', {data}, {layout}, {config} ){then_addframes}{then_animate}{then_post_script} }}""".format( id=plotdivid, data=jdata, layout=jlayout, config=jconfig, then_addframes=then_addframes, then_animate=then_animate, then_post_script=then_post_script) # ## Handle loading/initializing plotly.js ## include_plotlyjs_orig = include_plotlyjs if isinstance(include_plotlyjs, six.string_types): include_plotlyjs = include_plotlyjs.lower() # Start/end of requirejs block (if any) require_start = '' require_end = '' # Init and load load_plotlyjs = '' # Init plotlyjs. This block needs to run before plotly.js is loaded in # order for MathJax configuration to work properly if include_plotlyjs == 'require': require_start = 'require(["plotly"], function(Plotly) {' require_end = '});' elif include_plotlyjs == 'cdn': load_plotlyjs = """\ {win_config} <script src="https://cdn.plot.ly/plotly-latest.min.js"></script>\ """.format(win_config=_window_plotly_config) elif include_plotlyjs == 'directory': load_plotlyjs = """\ {win_config} <script src="plotly.min.js"></script>\ """.format(win_config=_window_plotly_config) elif (isinstance(include_plotlyjs, six.string_types) and include_plotlyjs.endswith('.js')): load_plotlyjs = """\ {win_config} <script src="{url}"></script>\ """.format(win_config=_window_plotly_config, url=include_plotlyjs_orig) elif include_plotlyjs: load_plotlyjs = """\ {win_config} <script type="text/javascript">{plotlyjs}</script>\ """.format(win_config=_window_plotly_config, plotlyjs=get_plotlyjs()) # ## Handle loading/initializing MathJax ## include_mathjax_orig = include_mathjax if isinstance(include_mathjax, six.string_types): include_mathjax = include_mathjax.lower() mathjax_template = """\ <script src="{url}?config=TeX-AMS-MML_SVG"></script>""" if include_mathjax == 'cdn': mathjax_script = mathjax_template.format( url=('https://cdnjs.cloudflare.com' '/ajax/libs/mathjax/2.7.5/MathJax.js')) + _mathjax_config elif (isinstance(include_mathjax, six.string_types) and include_mathjax.endswith('.js')): mathjax_script = mathjax_template.format( url=include_mathjax_orig) + _mathjax_config elif not include_mathjax: mathjax_script = '' else: raise ValueError("""\ Invalid value of type {typ} received as the include_mathjax argument Received value: {val} include_mathjax may be specified as False, 'cdn', or a string ending with '.js' """.format(typ=type(include_mathjax), val=repr(include_mathjax))) plotly_html_div = """\ <div> {mathjax_script} {load_plotlyjs} <div id="{id}" class="plotly-graph-div" \ style="height:{height}; width:{width};"></div> <script type="text/javascript"> {require_start} window.PLOTLYENV=window.PLOTLYENV || {{}}; window.PLOTLYENV.BASE_URL='{plotly_platform_url}'; {script}; {require_end} </script> </div>""".format( mathjax_script=mathjax_script, load_plotlyjs=load_plotlyjs, id=plotdivid, width=div_width, height=div_height, plotly_platform_url=plotly_platform_url, require_start=require_start, script=script, require_end=require_end) if full_html: return """\ <html> <head><meta charset="utf-8" /></head> <body> {div} </body> </html>""".format(div=plotly_html_div) else: return plotly_html_div
def getCustomJS(self): ## bolt on plotly library return offline.get_plotlyjs()
def to_html( fig, config=None, auto_play=True, include_plotlyjs=True, include_mathjax=False, post_script=None, full_html=True, animation_opts=None, default_width="100%", default_height="100%", validate=True, compress=True, ): """ Convert a figure to an HTML string representation. Parameters ---------- fig: Figure object or dict representing a figure config: dict or None (default None) Plotly.js figure config options auto_play: bool (default=True) Whether to automatically start the animation sequence on page load if the figure contains frames. Has no effect if the figure does not contain frames. include_plotlyjs: bool or string (default True) Specifies how the plotly.js library is included/loaded in the output div string. If True, a script tag containing the plotly.js source code (~3MB) is included in the output. HTML files generated with this option are fully self-contained and can be used offline. If 'cdn', a script tag that references the plotly.js CDN is included in the output. HTML files generated with this option are about 3MB smaller than those generated with include_plotlyjs=True, but they require an active internet connection in order to load the plotly.js library. If 'directory', a script tag is included that references an external plotly.min.js bundle that is assumed to reside in the same directory as the HTML file. If 'require', Plotly.js is loaded using require.js. This option assumes that require.js is globally available and that it has been globally configured to know how to find Plotly.js as 'plotly'. This option is not advised when full_html=True as it will result in a non-functional html file. If a string that ends in '.js', a script tag is included that references the specified path. This approach can be used to point the resulting HTML file to an alternative CDN or local bundle. If False, no script tag referencing plotly.js is included. This is useful when the resulting div string will be placed inside an HTML document that already loads plotly.js. This option is not advised when full_html=True as it will result in a non-functional html file. include_mathjax: bool or string (default False) Specifies how the MathJax.js library is included in the output html div string. MathJax is required in order to display labels with LaTeX typesetting. If False, no script tag referencing MathJax.js will be included in the output. If 'cdn', a script tag that references a MathJax CDN location will be included in the output. HTML div strings generated with this option will be able to display LaTeX typesetting as long as internet access is available. If a string that ends in '.js', a script tag is included that references the specified path. This approach can be used to point the resulting HTML div string to an alternative CDN. post_script: str or list or None (default None) JavaScript snippet(s) to be included in the resulting div just after plot creation. The string(s) may include '{plot_id}' placeholders that will then be replaced by the `id` of the div element that the plotly.js figure is associated with. One application for this script is to install custom plotly.js event handlers. full_html: bool (default True) If True, produce a string containing a complete HTML document starting with an <html> tag. If False, produce a string containing a single <div> element. animation_opts: dict or None (default None) dict of custom animation parameters to be passed to the function Plotly.animate in Plotly.js. See https://github.com/plotly/plotly.js/blob/master/src/plots/animation_attributes.js for available options. Has no effect if the figure does not contain frames, or auto_play is False. default_width, default_height: number or str (default '100%') The default figure width/height to use if the provided figure does not specify its own layout.width/layout.height property. May be specified in pixels as an integer (e.g. 500), or as a css width style string (e.g. '500px', '100%'). validate: bool (default True) True if the figure should be validated before being converted to JSON, False otherwise. compress: bool (default False) If True, the figure data is compressed reducing the total file size. It adds an external compression library which requires an active internet connection. Returns ------- str Representation of figure as an HTML div string """ # ## Validate figure ## fig_dict = validate_coerce_fig_to_dict(fig, validate) # ## Generate div id ## plotdivid = str(uuid.uuid4()) # ## Serialize figure ## jdata = json.dumps(fig_dict.get("data", []), cls=utils.PlotlyJSONEncoder, sort_keys=True) jlayout = json.dumps(fig_dict.get("layout", {}), cls=utils.PlotlyJSONEncoder, sort_keys=True) if fig_dict.get("frames", None): jframes = json.dumps(fig_dict.get("frames", []), cls=utils.PlotlyJSONEncoder) else: jframes = None # ## Serialize figure config ## config = _get_jconfig(config) # Set responsive config.setdefault("responsive", True) # Get div width/height layout_dict = fig_dict.get("layout", {}) template_dict = fig_dict.get("layout", {}).get("template", {}).get("layout", {}) div_width = layout_dict.get("width", template_dict.get("width", default_width)) div_height = layout_dict.get("height", template_dict.get("height", default_height)) # Add 'px' suffix to numeric widths try: float(div_width) except (ValueError, TypeError): pass else: div_width = str(div_width) + "px" try: float(div_height) except (ValueError, TypeError): pass else: div_height = str(div_height) + "px" # ## Get platform URL ## if config.get("showLink", False) or config.get("showSendToCloud", False): # Figure is going to include a Chart Studio link or send-to-cloud button, # So we need to configure the PLOTLYENV.BASE_URL property base_url_line = """ window.PLOTLYENV.BASE_URL='{plotly_platform_url}';\ """.format(plotly_platform_url=config.get("plotlyServerURL", "https://plot.ly")) else: # Figure is not going to include a Chart Studio link or send-to-cloud button, # In this case we don't want https://plot.ly to show up anywhere in the HTML # output config.pop("plotlyServerURL", None) config.pop("linkText", None) config.pop("showLink", None) base_url_line = "" # ## Build script body ## # This is the part that actually calls Plotly.js # build post script snippet(s) then_post_script = "" if post_script: if not isinstance(post_script, (list, tuple)): post_script = [post_script] for ps in post_script: then_post_script += """.then(function(){{ {post_script} }})""".format( post_script=ps.replace("{plot_id}", plotdivid)) then_addframes = "" then_animate = "" script_compressed_frames = "" if jframes: # If compression is enabled, then add zipped data decompress before Plotly is called. if compress: frames_compr_b64 = base64.b64encode( gzip.compress(jframes.encode('utf-8'))).decode('ascii') script_compressed_frames = """\ const frames_compr_b64 = "{frames_compr_b64}"; const frames_raw = fflate.decompressSync( fflate.strToU8(atob(frames_compr_b64), true) ); const frames = JSON.parse(fflate.strFromU8(frames_raw)); """.format(frames_compr_b64=frames_compr_b64) # Replace jframes with variable named `frames` jframes = "frames" then_addframes = """.then(function(){{ Plotly.addFrames('{id}', {frames}); }})""".format(id=plotdivid, frames=jframes) if auto_play: if animation_opts: animation_opts_arg = ", " + json.dumps(animation_opts) else: animation_opts_arg = "" then_animate = """.then(function(){{ Plotly.animate('{id}', null{animation_opts}); }})""".format(id=plotdivid, animation_opts=animation_opts_arg) # Serialize config dict to JSON jconfig = json.dumps(config) # Compress data via fflate and use a variable "data" to store the unpacked. script_compress = "" if compress: compressed_data = base64.b64encode(gzip.compress( jdata.encode('utf-8'))).decode('ascii') script_compress = """\ const data_compr_b64 = "{compressed_data}"; const data_raw = fflate.decompressSync( fflate.strToU8(atob(data_compr_b64), true) ); const data = JSON.parse(fflate.strFromU8(data_raw)); """.format(compressed_data=compressed_data) # Replace the plotly data with the variable "data". jdata = "data" script = """\ if (document.getElementById("{id}")) {{\ {script_compress}\ {script_compressed_frames}\ Plotly.newPlot(\ "{id}",\ {data},\ {layout},\ {config}\ ){then_addframes}{then_animate}{then_post_script}\ }}""".format( id=plotdivid, data=jdata, script_compress=script_compress, script_compressed_frames=script_compressed_frames, layout=jlayout, config=jconfig, then_addframes=then_addframes, then_animate=then_animate, then_post_script=then_post_script, ) # ## Handle loading/initializing plotly.js ## include_plotlyjs_orig = include_plotlyjs if isinstance(include_plotlyjs, six.string_types): include_plotlyjs = include_plotlyjs.lower() # Start/end of requirejs block (if any) require_start = "" require_end = "" # Init and load load_plotlyjs = "" # Init plotlyjs. This block needs to run before plotly.js is loaded in # order for MathJax configuration to work properly if include_plotlyjs == "require": require_start = 'require(["plotly"], function(Plotly) {' require_end = "});" elif include_plotlyjs == "cdn": load_plotlyjs = """\ {win_config} <script src="https://cdn.plot.ly/plotly-latest.min.js"></script>\ """.format(win_config=_window_plotly_config) elif include_plotlyjs == "directory": load_plotlyjs = """\ {win_config} <script src="plotly.min.js"></script>\ """.format(win_config=_window_plotly_config) elif isinstance(include_plotlyjs, six.string_types) and include_plotlyjs.endswith(".js"): load_plotlyjs = """\ {win_config} <script src="{url}"></script>\ """.format(win_config=_window_plotly_config, url=include_plotlyjs_orig) elif include_plotlyjs: load_plotlyjs = """\ {win_config} <script type="text/javascript">{plotlyjs}</script>\ """.format(win_config=_window_plotly_config, plotlyjs=get_plotlyjs()) load_fflatejs = "" if compress: load_fflatejs = "<script src=\"https://cdn.jsdelivr.net/npm/[email protected]/umd/index.min.js\"></script>" # ## Handle loading/initializing MathJax ## include_mathjax_orig = include_mathjax if isinstance(include_mathjax, six.string_types): include_mathjax = include_mathjax.lower() mathjax_template = """\ <script src="{url}?config=TeX-AMS-MML_SVG"></script>""" if include_mathjax == "cdn": mathjax_script = (mathjax_template.format( url=("https://cdnjs.cloudflare.com" "/ajax/libs/mathjax/2.7.5/MathJax.js")) + _mathjax_config) elif isinstance(include_mathjax, six.string_types) and include_mathjax.endswith(".js"): mathjax_script = (mathjax_template.format(url=include_mathjax_orig) + _mathjax_config) elif not include_mathjax: mathjax_script = "" else: raise ValueError("""\ Invalid value of type {typ} received as the include_mathjax argument Received value: {val} include_mathjax may be specified as False, 'cdn', or a string ending with '.js' """.format(typ=type(include_mathjax), val=repr(include_mathjax))) plotly_html_div = """\ <div>\ {mathjax_script}\ {load_plotlyjs}\ {load_fflatejs}\ <div id="{id}" class="plotly-graph-div" \ style="height:{height}; width:{width};"></div>\ <script type="text/javascript">\ {require_start}\ window.PLOTLYENV=window.PLOTLYENV || {{}};{base_url_line}\ {script};\ {require_end}\ </script>\ </div>""".format( mathjax_script=mathjax_script, load_plotlyjs=load_plotlyjs, load_fflatejs=load_fflatejs, id=plotdivid, width=div_width, height=div_height, base_url_line=base_url_line, require_start=require_start, script=script, require_end=require_end, ).strip() if full_html: return """\ <html> <head><meta charset="utf-8" /></head> <body> {div} </body> </html>""".format(div=plotly_html_div) else: return plotly_html_div
def ajax_get_plot(request, file_type='json'): if file_type == 'csv': permission_required = 'download_data' else: permission_required = 'view_plots' try: plot_type = request.GET['plotType'] template = request.GET.get('theme', default_plotly_template) if template not in ALLOWED_TEMPLATES: return HttpResponse('Please select an allowed template', status=400) dataset_id = int(request.GET['datasetId']) dataset2_id = request.GET.get('dataset2Id', None) if dataset2_id == "": dataset2_id = None if dataset2_id is not None: dataset2_id = int(dataset2_id) cell_line_id = request.GET.getlist('c') drug_id = request.GET.getlist('d') cell_line_id = [int(cl) for cl in cell_line_id] drug_ids = [] for dr in drug_id: try: drug_ids.append(int(dr)) except ValueError: drug_ids.append([int(d) for d in dr.split(",")]) drug_id = drug_ids assay = request.GET.get('assayId') yaxis = request.GET.get('logTransform', 'None') except (KeyError, ValueError): raise Http404() try: dataset = HTSDataset.objects.get(pk=dataset_id) except HTSDataset.DoesNotExist: raise Http404() _assert_has_perm(request, dataset, permission_required) if not license_accepted(request, dataset): return HttpResponse(LICENSE_UNSIGNED.format(dataset.name), status=400) if plot_type == 'tc': if len(drug_id) != 1 or len(cell_line_id) != 1: return HttpResponse( 'Please select exactly one cell line and ' 'drug for time course plot', status=400) try: df_data = df_doses_assays_controls(dataset=dataset, drug_id=drug_id, cell_line_id=cell_line_id, assay=assay) except NoDataException: return HttpResponse( 'No data found for this request. This ' 'drug/cell line/assay combination may not ' 'exist.', status=400) if assay is None: assay = df_data.assays.index.get_level_values('assay')[0] overlay_dip_fit = request.GET.get('overlayDipFit', 'false') == 'true' if overlay_dip_fit and assay != df_data.dip_assay_name: return HttpResponse( 'Can only overlay DIP rate on cell ' 'proliferation assays', status=400) plot_fig = plot_time_course(df_data, log_yaxis=yaxis == 'log2', assay_name=assay, show_dip_fit=overlay_dip_fit, subtitle=dataset.name, template=template) elif plot_type in ('drc', 'drpar'): if all(isinstance(d, int) for d in drug_id): plot_fig = _dose_response_plot(request, dataset, dataset2_id, permission_required, drug_id, cell_line_id, plot_type, template) else: if dataset2_id is not None: return HttpResponse( 'Please select a single dataset at a time to view drug ' 'combination heat plots', status=400) plot_fig = _drug_combination_heatmap(request, dataset, drug_id, cell_line_id, template) if isinstance(plot_fig, HttpResponse): return plot_fig elif plot_type == 'qc': qc_view = request.GET.get('qcView', None) if qc_view == 'ctrldipbox': ctrl_dip_data = df_ctrl_dip_rates(dataset_id) if ctrl_dip_data is None: return HttpResponse( 'No control wells with DIP rates ' 'available were detected in this ' 'dataset.', status=400) plot_fig = plot_ctrl_dip_by_plate(ctrl_dip_data, template=template) elif qc_view == 'ctrlcellbox': # Try to fetch from cache cache_key = f'dataset_{dataset_id}_plot_ctrlcellbox' cur_plotver = 1 plot_cached = cache.get(cache_key) if plot_cached: plot_fig = plot_cached['plot_fig'] if plot_cached is None or plot_cached['dataset_last_modified'] < dataset.modified_date or \ plot_cached['plot_version'] < cur_plotver: # Create plot groupings = dataset_groupings(dataset) if not groupings['singleTimepoint']: return HttpResponse( 'This plot type is only available for ' 'single time-point datasets', status=400) try: df_data = df_control_wells(dataset_id=dataset, assay=assay) except NoDataException: return HttpResponse('No data found for this request.', status=400) if (df_data['value'] == 100.0).all(): return HttpResponse( 'The raw data for this dataset is given as relative viability, so no control ' 'wells are available', status=400) plot_fig = plot_ctrl_cell_counts_by_plate( df_data, subtitle=dataset.name, template=template) # Push to cache cache.set( cache_key, { 'dataset_last_modified': dataset.modified_date, 'plot_version': cur_plotver, 'plot_fig': plot_fig }) elif qc_view == 'dipplatemap': plate_id = request.GET.get('plateId', None) try: plate_id = int(plate_id) except ValueError: return HttpResponse('Integer plateId required', status=400) pl_data = ajax_load_plate(request, plate_id, return_as_platedata=True, use_names=True) plot_fig = plot_plate_map(pl_data, color_by='dip_rates', template=template) else: return HttpResponse('Unimplemented QC view: {}'.format(qc_view), status=400) else: return HttpResponse('Unimplemented plot type: %s' % plot_type, status=400) as_attachment = request.GET.get('download', '0') == '1' if file_type == 'json': j = json.dumps(plot_fig, cls=PlotlyJSONEncoder) response = HttpResponse(j, content_type='application/json') elif file_type == 'csv': response = HttpResponse(plotly_to_dataframe(plot_fig).to_csv(), content_type='text/csv') elif file_type == 'html': template = 'plotly_plot{}.html'.format( '_standalone' if as_attachment else '') context = { 'data': json.dumps(plot_fig, cls=PlotlyJSONEncoder), 'page_title': strip_tags(plot_fig['layout']['title']['text']) } if as_attachment: context['plotlyjs'] = get_plotlyjs() response = render(request, template, context) else: return HttpResponse('Unknown file type: %s' % file_type, status=400) if as_attachment: try: title = plot_fig['layout']['title']['text'] except KeyError: title = 'Plot' response['Content-Disposition'] = \ 'attachment; filename="{}.{}"'.format(strip_tags(title), file_type) return response
def plot(figure_or_data, show_link=True, link_text='Export to plot.ly', validate=True, output_type='file', include_plotlyjs=True, filename='temp-plot.html', auto_open=True, image=None, image_filename='plot_image', image_width=800, image_height=600): """ Create a plotly graph locally as an HTML document or string. Example: ``` from plotly.offline import plot import plotly.graph_objs as go plot([go.Scatter(x=[1, 2, 3], y=[3, 2, 6])], filename='my-graph.html') # We can also download an image of the plot by setting the image parameter # to the image format we want plot([go.Scatter(x=[1, 2, 3], y=[3, 2, 6])], filename='my-graph.html' image='jpeg') ``` More examples below. figure_or_data -- a plotly.graph_objs.Figure or plotly.graph_objs.Data or dict or list that describes a Plotly graph. See https://plot.ly/python/ for examples of graph descriptions. Keyword arguments: show_link (default=True) -- display a link in the bottom-right corner of of the chart that will export the chart to Plotly Cloud or Plotly Enterprise link_text (default='Export to plot.ly') -- the text of export link validate (default=True) -- validate that all of the keys in the figure are valid? omit if your version of plotly.js has become outdated with your version of graph_reference.json or if you need to include extra, unnecessary keys in your figure. output_type ('file' | 'div' - default 'file') -- if 'file', then the graph is saved as a standalone HTML file and `plot` returns None. If 'div', then `plot` returns a string that just contains the HTML <div> that contains the graph and the script to generate the graph. Use 'file' if you want to save and view a single graph at a time in a standalone HTML file. Use 'div' if you are embedding these graphs in an HTML file with other graphs or HTML markup, like a HTML report or an website. include_plotlyjs (default=True) -- If True, include the plotly.js source code in the output file or string. Set as False if your HTML file already contains a copy of the plotly.js library. filename (default='temp-plot.html') -- The local filename to save the outputted chart to. If the filename already exists, it will be overwritten. This argument only applies if `output_type` is 'file'. auto_open (default=True) -- If True, open the saved file in a web browser after saving. This argument only applies if `output_type` is 'file'. image (default=None |'png' |'jpeg' |'svg' |'webp') -- This parameter sets the format of the image to be downloaded, if we choose to download an image. This parameter has a default value of None indicating that no image should be downloaded. Please note: for higher resolution images and more export options, consider making requests to our image servers. Type: `help(py.image)` for more details. image_filename (default='plot_image') -- Sets the name of the file your image will be saved to. The extension should not be included. image_height (default=600) -- Specifies the height of the image in `px`. image_width (default=800) -- Specifies the width of the image in `px`. """ if output_type not in ['div', 'file']: raise ValueError("`output_type` argument must be 'div' or 'file'. " "You supplied `" + output_type + "``") if not filename.endswith('.html') and output_type == 'file': warnings.warn("Your filename `" + filename + "` didn't end with .html. " "Adding .html to the end of your file.") filename += '.html' config = {} config['showLink'] = show_link config['linkText'] = link_text plot_html, plotdivid, width, height = _plot_html(figure_or_data, config, validate, '100%', '100%', global_requirejs=False) resize_script = '' if width == '100%' or height == '100%': resize_script = ( '' '<script type="text/javascript">' 'window.removeEventListener("resize");' 'window.addEventListener("resize", function(){{' 'Plotly.Plots.resize(document.getElementById("{id}"));}});' '</script>').format(id=plotdivid) if output_type == 'file': with open(filename, 'w') as f: if include_plotlyjs: plotly_js_script = ''.join([ '<script type="text/javascript">', get_plotlyjs(), '</script>', ]) else: plotly_js_script = '' if image: if image not in __IMAGE_FORMATS: raise ValueError('The image parameter must be one of the ' 'following: {}'.format(__IMAGE_FORMATS)) # if the check passes then download script is injected. # write the download script: script = get_image_download_script('plot') script = script.format(format=image, width=image_width, height=image_height, filename=image_filename, plot_id=plotdivid) else: script = '' f.write(''.join([ '<html>', '<head><meta charset="utf-8" /></head>', '<body>', plotly_js_script, plot_html, resize_script, script, '</body>', '</html>' ])) url = 'file://' + os.path.abspath(filename) if auto_open: webbrowser.open(url) return url elif output_type == 'div': if include_plotlyjs: return ''.join([ '<div>', '<script type="text/javascript">', get_plotlyjs(), '</script>', plot_html, '</div>' ]) else: return plot_html
def render(self, context): """ Load in JS/CSS dependencies to HTML """ # NOTE: Use render_context as it is recommended to do so here # https://docs.djangoproject.com/en/1.10/howto/custom-template-tags/ # initialize lists to store global gizmo css/js dependencies if 'global_gizmo_js_list' not in context.render_context: context.render_context['global_gizmo_js_list'] = [] if 'global_gizmo_css_list' not in context.render_context: context.render_context['global_gizmo_css_list'] = [] # initialize lists to store gizmo css/js dependencies if 'gizmo_js_list' not in context.render_context: context.render_context['gizmo_js_list'] = [] if 'gizmo_css_list' not in context.render_context: context.render_context['gizmo_css_list'] = [] # load list of gizmo css/js dependencies if 'gizmo_dependencies_loaded' not in context.render_context: # add all gizmos in context to be loaded for dict_element in context: for key in dict_element: resolved_options = template.Variable(key).resolve(context) if hasattr(resolved_options, GIZMO_NAME_PROPERTY): if resolved_options.gizmo_name not in context['gizmos_rendered']: context['gizmos_rendered'].append(resolved_options.gizmo_name) for rendered_gizmo in context['gizmos_rendered']: # Retrieve the "gizmo_dependencies" module and find the appropriate function dependencies_module = GIZMO_NAME_MAP[rendered_gizmo] # Only append dependencies if they do not already exist for dependency in dependencies_module.get_gizmo_css(): self._append_dependency(dependency, context.render_context['gizmo_css_list']) for dependency in dependencies_module.get_gizmo_js(): self._append_dependency(dependency, context.render_context['gizmo_js_list']) for dependency in dependencies_module.get_vendor_css(): self._append_dependency(dependency, context.render_context['global_gizmo_css_list']) for dependency in dependencies_module.get_vendor_js(): self._append_dependency(dependency, context.render_context['global_gizmo_js_list']) # Add the main gizmo dependencies last for dependency in TethysGizmoOptions.get_tethys_gizmos_css(): self._append_dependency(dependency, context.render_context['gizmo_css_list']) for dependency in TethysGizmoOptions.get_tethys_gizmos_js(): self._append_dependency(dependency, context.render_context['gizmo_js_list']) context.render_context['gizmo_dependencies_loaded'] = True # Create markup tags script_tags = [] style_tags = [] if self.output_type == CSS_GLOBAL_OUTPUT_TYPE or self.output_type is None: for dependency in context.render_context['global_gizmo_css_list']: style_tags.append('<link href="{0}" rel="stylesheet" />'.format(dependency)) if self.output_type == CSS_OUTPUT_TYPE or self.output_type is None: for dependency in context.render_context['gizmo_css_list']: style_tags.append('<link href="{0}" rel="stylesheet" />'.format(dependency)) if self.output_type == JS_GLOBAL_OUTPUT_TYPE or self.output_type is None: for dependency in context.render_context['global_gizmo_js_list']: if dependency.endswith('plotly-load_from_python.js'): script_tags.append(''.join( [ '<script type="text/javascript">', get_plotlyjs(), '</script>', ]) ) else: script_tags.append('<script src="{0}" type="text/javascript"></script>'.format(dependency)) if self.output_type == JS_OUTPUT_TYPE or self.output_type is None: for dependency in context.render_context['gizmo_js_list']: script_tags.append('<script src="{0}" type="text/javascript"></script>'.format(dependency)) # Combine all tags tags = style_tags + script_tags tags_string = '\n'.join(tags) return tags_string
def write_html(fig, file, config=None, auto_play=True, include_plotlyjs=True, include_mathjax=False, post_script=None, full_html=True, animation_opts=None, validate=True, default_width='100%', default_height='100%', auto_open=False): """ Write a figure to an HTML file representation Parameters ---------- fig: Figure object or dict representing a figure file: str or writeable A string representing a local file path or a writeable object (e.g. an open file descriptor) config: dict or None (default None) Plotly.js figure config options auto_play: bool (default=True) Whether to automatically start the animation sequence on page load if the figure contains frames. Has no effect if the figure does not contain frames. include_plotlyjs: bool or string (default True) Specifies how the plotly.js library is included/loaded in the output div string. If True, a script tag containing the plotly.js source code (~3MB) is included in the output. HTML files generated with this option are fully self-contained and can be used offline. If 'cdn', a script tag that references the plotly.js CDN is included in the output. HTML files generated with this option are about 3MB smaller than those generated with include_plotlyjs=True, but they require an active internet connection in order to load the plotly.js library. If 'directory', a script tag is included that references an external plotly.min.js bundle that is assumed to reside in the same directory as the HTML file. If `file` is a string to a local file path and `full_html` is True then If 'directory', a script tag is included that references an external plotly.min.js bundle that is assumed to reside in the same directory as the HTML file. If `file` is a string to a local file path and `full_html` is True, then the plotly.min.js bundle is copied into the directory of the resulting HTML file. If a file named plotly.min.js already exists in the output directory then this file is left unmodified and no copy is performed. HTML files generated with this option can be used offline, but they require a copy of the plotly.min.js bundle in the same directory. This option is useful when many figures will be saved as HTML files in the same directory because the plotly.js source code will be included only once per output directory, rather than once per output file. If 'require', Plotly.js is loaded using require.js. This option assumes that require.js is globally available and that it has been globally configured to know how to find Plotly.js as 'plotly'. This option is not advised when full_html=True as it will result in a non-functional html file. If a string that ends in '.js', a script tag is included that references the specified path. This approach can be used to point the resulting HTML file to an alternative CDN or local bundle. If False, no script tag referencing plotly.js is included. This is useful when the resulting div string will be placed inside an HTML document that already loads plotly.js. This option is not advised when full_html=True as it will result in a non-functional html file. include_mathjax: bool or string (default False) Specifies how the MathJax.js library is included in the output html div string. MathJax is required in order to display labels with LaTeX typesetting. If False, no script tag referencing MathJax.js will be included in the output. If 'cdn', a script tag that references a MathJax CDN location will be included in the output. HTML div strings generated with this option will be able to display LaTeX typesetting as long as internet access is available. If a string that ends in '.js', a script tag is included that references the specified path. This approach can be used to point the resulting HTML div string to an alternative CDN. post_script: str or list or None (default None) JavaScript snippet(s) to be included in the resulting div just after plot creation. The string(s) may include '{plot_id}' placeholders that will then be replaced by the `id` of the div element that the plotly.js figure is associated with. One application for this script is to install custom plotly.js event handlers. full_html: bool (default True) If True, produce a string containing a complete HTML document starting with an <html> tag. If False, produce a string containing a single <div> element. animation_opts: dict or None (default None) dict of custom animation parameters to be passed to the function Plotly.animate in Plotly.js. See https://github.com/plotly/plotly.js/blob/master/src/plots/animation_attributes.js for available options. Has no effect if the figure does not contain frames, or auto_play is False. default_width, default_height: number or str (default '100%') The default figure width/height to use if the provided figure does not specify its own layout.width/layout.height property. May be specified in pixels as an integer (e.g. 500), or as a css width style string (e.g. '500px', '100%'). validate: bool (default True) True if the figure should be validated before being converted to JSON, False otherwise. auto_open: bool (default True If True, open the saved file in a web browser after saving. This argument only applies if `full_html` is True. Returns ------- str Representation of figure as an HTML div string """ # Build HTML string html_str = to_html( fig, config=config, auto_play=auto_play, include_plotlyjs=include_plotlyjs, include_mathjax=include_mathjax, post_script=post_script, full_html=full_html, animation_opts=animation_opts, default_width=default_width, default_height=default_height, validate=validate, ) # Check if file is a string file_is_str = isinstance(file, six.string_types) # Write HTML string if file_is_str: with open(file, 'w') as f: f.write(html_str) else: file.write(html_str) # Check if we should copy plotly.min.js to output directory if file_is_str and full_html and include_plotlyjs == 'directory': bundle_path = os.path.join( os.path.dirname(file), 'plotly.min.js') if not os.path.exists(bundle_path): with open(bundle_path, 'w') as f: f.write(get_plotlyjs()) # Handle auto_open if file_is_str and full_html and auto_open: url = 'file://' + os.path.abspath(file) webbrowser.open(url)
def render(self, context): """ Load in JS/CSS dependencies to HTML """ # NOTE: Use render_context as it is recommended to do so here # https://docs.djangoproject.com/en/1.10/howto/custom-template-tags/ # initialize lists to store global gizmo css/js dependencies if 'global_gizmo_js_list' not in context.render_context: context.render_context['global_gizmo_js_list'] = [] if 'global_gizmo_css_list' not in context.render_context: context.render_context['global_gizmo_css_list'] = [] # initialize lists to store gizmo css/js dependencies if 'gizmo_js_list' not in context.render_context: context.render_context['gizmo_js_list'] = [] if 'gizmo_css_list' not in context.render_context: context.render_context['gizmo_css_list'] = [] # load list of gizmo css/js dependencies if 'gizmo_dependencies_loaded' not in context.render_context: # add all gizmos in context to be loaded for dict_element in context: for key in dict_element: resolved_options = template.Variable(key).resolve(context) if hasattr(resolved_options, GIZMO_NAME_PROPERTY): if resolved_options.gizmo_name not in context[ 'gizmos_rendered']: context['gizmos_rendered'].append( resolved_options.gizmo_name) for rendered_gizmo in context['gizmos_rendered']: # Retrieve the "gizmo_dependencies" module and find the appropriate function dependencies_module = GIZMO_NAME_MAP[rendered_gizmo] # Only append dependencies if they do not already exist for dependency in dependencies_module.get_gizmo_css(): self._append_dependency( dependency, context.render_context['gizmo_css_list']) for dependency in dependencies_module.get_gizmo_js(): self._append_dependency( dependency, context.render_context['gizmo_js_list']) for dependency in dependencies_module.get_vendor_css(): self._append_dependency( dependency, context.render_context['global_gizmo_css_list']) for dependency in dependencies_module.get_vendor_js(): self._append_dependency( dependency, context.render_context['global_gizmo_js_list']) # Add the main gizmo dependencies last for dependency in TethysGizmoOptions.get_tethys_gizmos_css(): self._append_dependency( dependency, context.render_context['gizmo_css_list']) for dependency in TethysGizmoOptions.get_tethys_gizmos_js(): self._append_dependency( dependency, context.render_context['gizmo_js_list']) context.render_context['gizmo_dependencies_loaded'] = True # Create markup tags script_tags = [] style_tags = [] if self.output_type == CSS_GLOBAL_OUTPUT_TYPE or self.output_type is None: for dependency in context.render_context['global_gizmo_css_list']: style_tags.append( '<link href="{0}" rel="stylesheet" />'.format(dependency)) if self.output_type == CSS_OUTPUT_TYPE or self.output_type is None: for dependency in context.render_context['gizmo_css_list']: style_tags.append( '<link href="{0}" rel="stylesheet" />'.format(dependency)) if self.output_type == JS_GLOBAL_OUTPUT_TYPE or self.output_type is None: for dependency in context.render_context['global_gizmo_js_list']: if dependency.endswith('plotly-load_from_python.js'): script_tags.append(''.join([ '<script type="text/javascript">', get_plotlyjs(), '</script>', ])) else: script_tags.append( '<script src="{0}" type="text/javascript"></script>'. format(dependency)) if self.output_type == JS_OUTPUT_TYPE or self.output_type is None: for dependency in context.render_context['gizmo_js_list']: script_tags.append( '<script src="{0}" type="text/javascript"></script>'. format(dependency)) # Combine all tags tags = style_tags + script_tags tags_string = '\n'.join(tags) return tags_string
def plot(figure_or_data, show_link=True, link_text='Export to plot.ly', validate=True, output_type='file', include_plotlyjs=True, filename='temp-plot.html', auto_open=True, image=None, image_filename='plot_image', image_width=800, image_height=600): """ Create a plotly graph locally as an HTML document or string. Example: ``` from plotly.offline import plot import plotly.graph_objs as go plot([go.Scatter(x=[1, 2, 3], y=[3, 2, 6])], filename='my-graph.html') # We can also download an image of the plot by setting the image parameter # to the image format we want plot([go.Scatter(x=[1, 2, 3], y=[3, 2, 6])], filename='my-graph.html' image='jpeg') ``` More examples below. figure_or_data -- a plotly.graph_objs.Figure or plotly.graph_objs.Data or dict or list that describes a Plotly graph. See https://plot.ly/python/ for examples of graph descriptions. Keyword arguments: show_link (default=True) -- display a link in the bottom-right corner of of the chart that will export the chart to Plotly Cloud or Plotly Enterprise link_text (default='Export to plot.ly') -- the text of export link validate (default=True) -- validate that all of the keys in the figure are valid? omit if your version of plotly.js has become outdated with your version of graph_reference.json or if you need to include extra, unnecessary keys in your figure. output_type ('file' | 'div' - default 'file') -- if 'file', then the graph is saved as a standalone HTML file and `plot` returns None. If 'div', then `plot` returns a string that just contains the HTML <div> that contains the graph and the script to generate the graph. Use 'file' if you want to save and view a single graph at a time in a standalone HTML file. Use 'div' if you are embedding these graphs in an HTML file with other graphs or HTML markup, like a HTML report or an website. include_plotlyjs (default=True) -- If True, include the plotly.js source code in the output file or string. Set as False if your HTML file already contains a copy of the plotly.js library. filename (default='temp-plot.html') -- The local filename to save the outputted chart to. If the filename already exists, it will be overwritten. This argument only applies if `output_type` is 'file'. auto_open (default=True) -- If True, open the saved file in a web browser after saving. This argument only applies if `output_type` is 'file'. image (default=None |'png' |'jpeg' |'svg' |'webp') -- This parameter sets the format of the image to be downloaded, if we choose to download an image. This parameter has a default value of None indicating that no image should be downloaded. Please note: for higher resolution images and more export options, consider making requests to our image servers. Type: `help(py.image)` for more details. image_filename (default='plot_image') -- Sets the name of the file your image will be saved to. The extension should not be included. image_height (default=600) -- Specifies the height of the image in `px`. image_width (default=800) -- Specifies the width of the image in `px`. """ if output_type not in ['div', 'file']: raise ValueError( "`output_type` argument must be 'div' or 'file'. " "You supplied `" + output_type + "``") if not filename.endswith('.html') and output_type == 'file': warnings.warn( "Your filename `" + filename + "` didn't end with .html. " "Adding .html to the end of your file.") filename += '.html' config = {} config['showLink'] = show_link config['linkText'] = link_text plot_html, plotdivid, width, height = _plot_html( figure_or_data, config, validate, '100%', '100%', global_requirejs=False) resize_script = '' if width == '100%' or height == '100%': resize_script = ( '' '<script type="text/javascript">' 'window.removeEventListener("resize");' 'window.addEventListener("resize", function(){{' 'Plotly.Plots.resize(document.getElementById("{id}"));}});' '</script>' ).format(id=plotdivid) if output_type == 'file': with open(filename, 'w') as f: if include_plotlyjs: plotly_js_script = ''.join([ '<script type="text/javascript">', get_plotlyjs(), '</script>', ]) else: plotly_js_script = '' if image: if image not in __IMAGE_FORMATS: raise ValueError('The image parameter must be one of the ' 'following: {}'.format(__IMAGE_FORMATS)) # if the check passes then download script is injected. # write the download script: script = get_image_download_script('plot') script = script.format(format=image, width=image_width, height=image_height, filename=image_filename, plot_id=plotdivid) else: script = '' f.write(''.join([ '<html>', '<head><meta charset="utf-8" /></head>', '<body>', plotly_js_script, plot_html, resize_script, script, '</body>', '</html>'])) url = 'file://' + os.path.abspath(filename) if auto_open: webbrowser.open(url) return url elif output_type == 'div': if include_plotlyjs: return ''.join([ '<div>', '<script type="text/javascript">', get_plotlyjs(), '</script>', plot_html, '</div>' ]) else: return plot_html