In the previous first steps guides, you created and customized, and combined visualizations.
In this section, you will use various methods to display and export your visualizations.
All examples so far have used the output_file() function to save your visualization to an HTML file. This HTML file contains all the necessary information to display your plot.
output_file()
output_file() accepts various arguments. For example:
filename: the filename for the HTML file
filename
title: the title for you document (to be used in the HTML’s <title> tag)
title
<title>
Bokeh creates the HTML file when you call the show() function. This function also automatically opens a web browser to display the HTML file.
show()
If you want Bokeh to only generate the file but not open it in a web browser, use the save() function instead. You need to import the save() function before using it, just like you did for show().
save()
from bokeh.plotting import figure, output_file, save # prepare some data x = [1, 2, 3, 4, 5] y = [4, 5, 5, 7, 2] # set output to static HTML file output_file(filename="custom_filename.html", title="Static HTML file") # create a new plot with a specific size p = figure(sizing_mode="stretch_width", max_width=500, plot_height=250) # add a circle renderer circle = p.circle(x, y, fill_color="red", size=15) # save the results to a file save(p)
See also
For more information on embedding Bokeh visualizations online, see Embedding Bokeh content in the user guide.
Note
By default, Bokeh-generated HTML files include a standard version of BokehJS that is automatically downloaded from Bokeh’s servers. Use the argument mode with the function output_file() to change this behavior. For more information, see output_file and Resources in the reference guide.
mode
output_file
Resources
If you use Jupyter notebooks, switch out Bokeh’s output_file() for output_notebook().
output_notebook()
Use the show() function to display your visualization right inside your notebook:
For more information on using Jupyter notebooks, see Using with Jupyter in the user guide.
Interact directly with live tutorial notebooks hosted online by MyBinder.
To export PNG or SVG files, you might need to install additional dependencies.
In order to create PNG and SVG files, Bokeh uses Selenium. Selenium allows Bokeh to run in a browser without a graphical user interface (GUI). Bokeh uses this browser to render the PNG or SVG files. In order for this to work, Selenium needs to be able to access either a Firefox browser (through a package called geckodriver) or a Chromium browser (through the chromedriver package).
Depending on whether you are using conda or pip, run one of the following commands to make sure you have all the required packages installed:
conda
pip
Installing with conda
conda install selenium geckodriver firefox -c conda-forge
Installing with pip
pip install selenium geckodriver firefox
Once the requirements are installed, you can use the export_png() function to export your plot into a PNG file:
export_png()
from bokeh.io import export_png from bokeh.plotting import figure # prepare some data x = [1, 2, 3, 4, 5] y = [4, 5, 5, 7, 2] # create a new plot with fixed dimensions p = figure(plot_width=350, plot_height=250) # add a circle renderer circle = p.circle(x, y, fill_color="red", size=15) # save the results to a file export_png(p, filename="plot.png")
For information on how to export PNG and SVG files, see Exporting plots in the user guide.
Previous
Next