#!/usr/bin/env python
# coding: utf-8
#
#
#
#
#
#
# |
#
# Bokeh Tutorial
# |
#
#
#
# 10. Exporting and Embedding
# So far we have seen how to generate interactive Bokeh output directly inline in Jupyter notbeooks. It also possible to embed interactive Bokeh plots and layouts in other contexts, such as standalone HTML files, or Jinja templates. Additionally, Bokeh can export plots to static (non-interactive) PNG and SVG formats.
#
# We will look at all of these possibilities in this chapter. First we make the usual imports.
# In[ ]:
from bokeh.io import output_notebook, show
output_notebook()
# And also load some data that will be used throughout this chapter
# In[ ]:
import pandas as pd
from bokeh.plotting import figure
from bokeh.sampledata.stocks import AAPL
df = pd.DataFrame(AAPL)
df['date'] = pd.to_datetime(df['date'])
# # Embedding Interactive Content
#
# To start we will look different ways of embedding live interactive Bokeh output in various situations.
# ## Displaying in the Notebook
#
# The first way to embed Bokeh output is in the Jupyter Notebooks, as we have already, seen. As a reminder, the cell below will generate a plot inline as output, because we executed `output_notebook` above.
# In[ ]:
p = figure(width=800, height=250, x_axis_type="datetime")
p.line(df['date'], df['close'], color='navy', alpha=0.5)
show(p)
# ## Saving to an HTML File
#
# It is also often useful to generate a standalone HTML script containing Bokeh content. This is accomplished by calling the `output_file(...)` function. It is especially common to do this from standard Python scripts, but here we see that it works in the notebook as well.
# In[ ]:
from bokeh.io import output_file, show
# In[ ]:
output_file("plot.html")
# In[ ]:
show(p) # save(p) will save without opening a new browser tab
# In addition the inline plot above, you should also have seen a new browser tab open with the contents of the newly saved "plot.html" file. It is important to note that `output_file` initiates a *persistent mode of operation*. That is, all subsequent calls to show will generate output to the specified file. We can "reset" where output will go by calling `reset_output`:
# In[ ]:
from bokeh.io import reset_output
reset_output()
# ## Templating in HTML Documents
#
# Another use case is to embed Bokeh content in a Jinja HTML template. We will look at a simple explicit case first, and then see how this technique might be used in a web app framework such as Flask.
#
# The simplest way to embed standalone (i.e. not Bokeh server) content is to use the `components` function. This function takes a Bokeh object, and returns a `
Hello Bokeh!
Below is a simple plot of stock closing prices
{{ script }}
{{ div }}