Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please refer to our video player troubleshooting guide for assistance.

Generating Your First Figure to a Static HTML File

This video will show you how to output your Bokeh visualation to a static HTML file. Bokeh will generate all of the HTML and Javascript automatically for you, when you run your script.

More information about output_file() can be found in the Bokeh official docs.

File: FirstFigure.py

Python
# Bokeh Libraries
from bokeh.io import output_file
from bokeh.plotting import figure, show

# The figure will be rendered in a static HTML file
# called output_file_test.html
output_file('output_file_test.html', 
            title='Empty Bokeh Figure')

# Set up a generic figure() object
fig = figure()

# See what it looks like
show(fig)

00:00 In this video, I’ll show you how to generate your first figure to a static HTML file. To start this process, you’re going to need a new file. The script is going to be called FirstFigure.py.

00:12 You should already be in a directory named Bokeh/. You should put it in the root of that directory. Okay. What goes in it? First off, bring in the bokeh libraries that we’re going to use for this part. So from bokeh, there’s an io, input output, library.

00:31 The first one we’re going to use is what’s called output_file. You’ll see here there’s two choices, output_file and output_notebook.

00:39 I’ll have you focus on this one first. Okay. What else do you need? From bokeh, you need to do a little bit of plotting. So from that, import figure, which is going to be very crucial, and show. Next, you’re going to create this very simple figure rendered into a static HTML file. It will be named output_file_test.html.

01:04 So take the output_file() that you just imported and call that method. It offers you a couple of different things to output. One is the filename, which is the name that we selected right here,

01:19 so 'output_file_test.html'. That would be the filename.

01:25 HTML pages have a title. The title you’re going to use is going to be 'Empty Bokeh Figure'. Next up, set up a generic figure() object. To do that, you’re going to create an object named fig and instantiate figure() into it with no arguments. Last, see what it looks like. To do that, you’ll call the method show(). In this case, the argument will be the figure object that you just created.

01:55 Great. I’m going to save. So, right now here in the file explorer there’s FirstFigure.py. And down here in my terminal, I’ll have you run that script, FirstFigure.py. What’s going to happen?

02:12 Okay. You’ll see here that it opened up the browser and it opened up this output_file_test.html document. It’s a very simple document, as you can see here.

02:24 It gives you a great look at what Bokeh creates for you. It creates an entire HTML little webpage that can be, as a static file, put into an existing website if you’d like, where you can take the code, which we’ll look at here in a second. You’ll see here, the toolbar. I’ll have you go over this in a lot of detail through these other videos. Back here, you’ll see that this is the file that it created, output_file_test.html. If you select it, it brings up the HTML itself that it created.

02:53 The Bokeh library generated all this static HTML for you. It’s importing style sheets and JavaScript.

03:03 What’s great is you don’t need to do a whole lot with all that information. It’s already coded and ready to go. In the next video, I’ll show you how to lay out a figure inline inside of a Jupyter Notebook.

Kalesis on Sept. 8, 2019

Hello In the sample code, the object figure() was missing. regards

hayatmor on March 18, 2020

WARNING:bokeh.core.validation.check:W-1000 (MISSING_RENDERERS): Plot has no renderers: Figure(id=‘1002’, …)

soultion

>>> from bokeh.core.validation import silence
>>> from bokeh.core.validation.warnings import EMPTY_LAYOUT
>>> silence(EMPTY_LAYOUT, True)

rickecon on March 26, 2020

Working on a macOS Catalina (v. 10.15.3). I set up the environment as specified. When I run my FirstFigure.py, I get a blank browser window in Chrome with the following error:

WARNING:bokeh.core.validation.check:W-1000 (MISSING_RENDERERS): Plot has no renderers: Figure(id='1001', ...)

My code for FirstFigure.py is the following, which I think is exactly as demonstrated:

# Bokeh libraries
from bokeh.io import output_file
from bokeh.plotting import figure, show

# The figure will be rendered in a static HTML file called output_file_test.html
output_file('output_file_test.html', title='Empty Bokeh Figure')

# Set up a generic figure object
fig = figure()

# See what it looks like
show(fig)

I tried using the previous comment’s suggestion which is the same in the bokeh user guide of adding the core.validation settings at import, which resulted in the same error and the same blank browser window.

# Bokeh libraries
from bokeh.io import output_file
from bokeh.plotting import figure, show
from bokeh.core.validation import silence
from bokeh.core.validation.warnings import EMPTY_LAYOUT
silence(EMPTY_LAYOUT, True)

# The figure will be rendered in a static HTML file called
# output_file_test.html
output_file('output_file_test.html', title='Empty Bokeh Figure')

# Set up a generic figure object
fig = figure()

# See what it looks like
show(fig)

I saw in the bokeh user guide that id='1001' means BAD_COLUMN_NAME: “A glyph has a property set to a field name that does not correspond to any column in the GlyphRenderer’s data source.” But I didn’t specify any properties in the code above.

Please advise.

rickecon on March 26, 2020

UPDATE: If I open the HTML file generated in each of the examples above using Safari, it looks fine. So my fix is the following.

show(fig, browser='safari')

Any idea why Chrome produces a blank figure?

Chris Bailey RP Team on March 26, 2020

Hi @rickecon, Thanks for your comments. As Bokeh has updated it looks like this “Warning” was added, and makes this portion of the course a bit more awkward. The lessons going forward all have rendering. The idea of the lesson was to show the bare minimum of how to make an ‘empty’ plot. So the warning makes sense, there are no renderers. I will look at ways to update this particular lesson as the Bokeh package moves forward.

rickecon on March 28, 2020

Thanks Chris. For some reason, the problem has gone away. The only thing I updated was Xcode developer tools. But everything works great now.

R morel on May 20, 2020

show_in = ‘safari’

ganapathy2 on Feb. 3, 2022

Hi Good Morning, As you type the code next level is getting populated. for example “from bokeh.io import out”, when you type “out” three options are show adjacent to “out”. I am also using VScode, but I am not getting it. I am missing something. could you please help me out. Thanks

Bartosz Zaczyński RP Team on Feb. 3, 2022

@ganapathy2 Bokeh is an external library. Perhaps, you need to select the right Python interpreter where you’ve installed it? You’ll find a relevant dropdown menu in the bottom left corner in VS Code.

ganapathy2 on Feb. 3, 2022

Thank you very much. I got it

Become a Member to join the conversation.