#-----------------------------------------------------------------------------# Copyright (c) 2012 - 2022, Anaconda, Inc., and Bokeh Contributors.# All rights reserved.## The full license is in the file LICENSE.txt, distributed with this software.#-----------------------------------------------------------------------------''''''#-----------------------------------------------------------------------------# Boilerplate#-----------------------------------------------------------------------------from__future__importannotationsimportlogging# isort:skiplog=logging.getLogger(__name__)#-----------------------------------------------------------------------------# Imports#-----------------------------------------------------------------------------# Standard library importsfromos.pathimportabspath,expanduserfromtypingimportSequence# External importsfromjinja2importTemplate# Bokeh importsfrom..core.templatesimportFILEfrom..core.typesimportPathLikefrom..models.uiimportUIElementfrom..resourcesimportResources,ResourcesLikefrom..settingsimportsettingsfrom..themesimportThemefrom..util.warningsimportwarnfrom.stateimportState,curstatefrom.utilimportdefault_filename#-----------------------------------------------------------------------------# Globals and constants#-----------------------------------------------------------------------------DEFAULT_TITLE="Bokeh Plot"__all__=('save',)#-----------------------------------------------------------------------------# General API#-----------------------------------------------------------------------------
[docs]defsave(obj:UIElement|Sequence[UIElement],filename:PathLike|None=None,resources:ResourcesLike|None=None,title:str|None=None,template:Template|None=None,state:State|None=None)->str:''' Save an HTML file with the data for the current document. Will fall back to the default output state (or an explicitly provided :class:`State` object) for ``filename``, ``resources``, or ``title`` if they are not provided. If the filename is not given and not provided via output state, it is derived from the script name (e.g. ``/foo/myplot.py`` will create ``/foo/myplot.html``) Args: obj (UIElement object) : a Layout (Row/Column), Plot or Widget object to display filename (PathLike, e.g. str, Path, optional) : filename to save document under (default: None) If None, use the default state configuration. resources (Resources or ResourcesMode, optional) : A Resources config to use (default: None) If None, use the default state configuration, if there is one. otherwise use ``resources.INLINE``. title (str, optional) : a title for the HTML document (default: None) If None, use the default state title value, if there is one. Otherwise, use "Bokeh Plot" template (Template, optional) : HTML document template (default: FILE) A Jinja2 Template, see bokeh.core.templates.FILE for the required template parameters state (State, optional) : A :class:`State` object. If None, then the current default implicit state is used. (default: None). Returns: str: the filename where the HTML file is saved. '''ifstateisNone:state=curstate()theme=state.document.themefilename,resources,title=_get_save_args(state,filename,resources,title)_save_helper(obj,filename,resources,title,template,theme)returnabspath(expanduser(filename))
#-----------------------------------------------------------------------------# Dev API#-----------------------------------------------------------------------------#-----------------------------------------------------------------------------# Private API#-----------------------------------------------------------------------------def_get_save_args(state:State,filename:PathLike|None,resources:ResourcesLike|None,title:str|None)->tuple[PathLike,Resources,str]:''' '''filename,is_default_filename=_get_save_filename(state,filename)resources=_get_save_resources(state,resources,is_default_filename)title=_get_save_title(state,title,is_default_filename)returnfilename,resources,titledef_get_save_filename(state:State,filename:PathLike|None)->tuple[PathLike,bool]:iffilenameisnotNone:returnfilename,Falseifstate.fileandnotsettings.ignore_filename():returnstate.file.filename,Falsereturndefault_filename("html"),Truedef_get_save_resources(state:State,resources:ResourcesLike|None,suppress_warning:bool)->Resources:ifresourcesisnotNone:ifisinstance(resources,Resources):returnresourceselse:returnResources(mode=resources)ifstate.file:returnstate.file.resourcesifnotsuppress_warning:warn("save() called but no resources were supplied and output_file(...) was never called, defaulting to resources.CDN")returnResources(mode=settings.resources())def_get_save_title(state:State,title:str|None,suppress_warning:bool)->str:iftitleisnotNone:returntitleifstate.file:returnstate.file.titleifnotsuppress_warning:warn("save() called but no title was supplied and output_file(...) was never called, using default title 'Bokeh Plot'")returnDEFAULT_TITLEdef_save_helper(obj:UIElement|Sequence[UIElement],filename:PathLike,resources:Resources|None,title:str|None,template:Template|None,theme:Theme|None=None)->None:''' '''from..embedimportfile_htmlhtml=file_html(obj,resources,title=title,template=templateorFILE,theme=theme)withopen(filename,mode="w",encoding="utf-8")asf:f.write(html)#-----------------------------------------------------------------------------# Code#-----------------------------------------------------------------------------