Using bokeh Commands

It is possible to generate various kinds of output from Bokeh programmatically. The use of output_file, output_notebook, etc. has been demonstrated previously in many areas of the User Guide. However, Bokeh also provides a command line tool bokeh that can often offer a more flexible way to generate different kinds of output from the same source code, as well as making it easier to and faster to iterate.

There are three basic bokeh commands:

The bokeh html command can create standalone HTML documents from any kind of Bokeh application source: e.g., python scripts, app directories, JSON files, and others. For example:

bokeh html myapp.py

The bokeh json command will generate a serialized JSON representation of a Bokeh document from any kind of Bokeh application source. For example:

bokeh json myapp.py

Finally, the bokeh serve command let’s you instantly turn Bokeh documents into interactive web applications. For example:

bokeh serve myapp.py

In all of these cases, the same file myapp.py can be used without modification to generate different sorts of output.

Standalone HTML

To generate a standalone HTML page for a Bokeh application from a single Python script, pass the script name to bokeh html on the command line:

bokeh html app_script.py

The generated HTML will be saved in the current working directory with the name app_script.html.

Applications can also be created from directories. The directory should contain a main.py (and any other helper modules that are required) as well as any additional assets (e.g., theme files). Pass the directory name to bokeh html to generate the HTML:

bokeh html app_dir

It is possible to generate HTML pages for multiple applications at once:

bokeh html app_script.py app_dir

If you would like to automatically open a browser to display the HTML page(s), you can pass the --show option on the command line:

bokeh html app_script.py app_dir --show

This will open two pages, for app_script.html and app_dir.html, respectively.

Warning

Applications that use on_change callbacks require using the Bokeh server to execute the callback code. Though the application may render, the callbacks will not function. See Bokeh Server Applications for more information on using bokeh serve.

Serialized JSON

To generate the serialized JSON representation for a Bokeh application from a single Python script, pass the script name to bokeh json on the command line:

bokeh json app_script.py

The generated JSON will be saved in the current working directory with the name app_script.json.

Applications can also be created from directories. The directory should contain a main.py (and any other helper modules that are required) as well as any additional assets (e.g., theme files). Pass the directory name to bokeh json to generate the JSON:

bokeh json app_dir

By default, the generated JSON is output as one line, with no indentation. To generate “pretty printed” JSON on multiple lines, you can specify an indentation level with the --indent argument:

bokeh json app_script.py --indent=2

Bokeh Server Applications

To run a Bokeh application on a Bokeh server from a single Python script, pass the script name to bokeh serve on the command line:

bokeh serve app_script.py

By default, the Bokeh application will be served by the Bokeh server on a default port (5006) at localhost, under the path /app_script, i.e.,

http://localhost:5006/app_script

Applications can also be created from directories. The directory should contain a main.py (and any other helper modules that are required) as well as any additional assets (e.g., theme files). Pass the directory name to bokeh serve to run the application:

bokeh serve app_dir

It is possible to run multiple applications at once:

bokeh serve app_script.py app_dir

If you would like to automatically open a browser to display the HTML page(s), you can pass the --show option on the command line:

bokeh serve app_script.py app_dir --show

This will open two pages, for /app_script and /app_dir, respectively.

Network Configuration

To control the port that the Bokeh server listens on, use the --port argument:

bokeh serve app_script.py --port=8080

Similarly, a specific network address can be specified with the --address argument. For example:

bokeh serve app_script.py --address=0.0.0.0

will have the Bokeh server listen all available network addresses.

Additionally, it is possible to configure a hosts whitelist that must be matched by the Host header in new requests. You can specify multiple acceptable host values with the --host option:

bokeh serve app_script.py --host foo.com:8081 --host bar.com

If no port is specified in a host value, then port 80 will be used. In the example above Bokeh server will accept requests from foo.com:8081 and bar.com:80.

If no host values are specified, then by default the Bokeh server will accept requests from localhost:<port> where <port> is the port that the server is configured to listen on (by default: 5006).

Also note that the host whitelist applies to all request handlers, including any extra ones added to extend the Bokeh server.

By default, cross site connections to the Bokeh server websocket are not allowed. You can enable websocket connections originating from additional hosts by specifying them with the --allow-websocket-origin option:

bokeh serve app_script.py --allow-websocket-origin foo.com:8081

It is possible to specify multiple allowed websocket origins by adding the --allow-websocket-origin option multiple times.

The Bokeh server can also add an optional prefix to all URL paths. This can often be useful in conjunction with “reverse proxy” setups.

bokeh serve app_script.py --prefix=foobar

Then the application will be served under the following URL:

http://localhost:5006/foobar/app_script

If needed, Bokeh server can send keep-alive pings at a fixed interval. To configure this feature, set the –keep-alive option:

bokeh serve app_script.py --keep-alive 10000

The value is specified in milliseconds. The default keep-alive interval is 37 seconds. Give a value of 0 to disable keep-alive pings.

Session ID Options

Typically, each browser tab connected to a Bokeh server will have its own session ID. When the server generates an ID, it will make it cryptographically unguessable. This keeps users from accessing one another’s sessions.

To control who can use a Bokeh application, the server can sign sessions with a secret key and reject “made up” session names. There are three modes, controlled by the --session-ids argument:

bokeh serve app_script.py --session-ids=signed

The available modes are: unsigned, signed or external-signed

In unsigned mode, the server will accept any session ID provided to it in the URL. For example, http://localhost/app_script?bokeh-session-id=foo will create a session foo. In unsigned mode, if the session ID isn’t provided with ?bokeh-session-id= in the URL, the server will still generate a cryptographically-unguessable ID. However, the server allows clients to create guessable or deliberately-shared sessions if they want to.

unsigned mode is most useful when the server is running locally for development, for example you can have multiple processes access a fixed session name such as default. unsigned mode is also convenient because there’s no need to generate or configure a secret key.

In signed mode, the session ID must be in a special format and signed with a secret key. Attempts to use the application with an invalid session ID will fail, but if no ?bokeh-session-id= parameter is provided, the server will generate a fresh, signed session ID. The result of signed mode is that only secure session IDs are allowed but anyone can connect to the server.

In external-signed mode, the session ID must be signed but the server itself won’t generate a session ID; the ?bokeh-session-id= parameter will be required. To use this mode, you would need some sort of external process (such as another web app) which would use the bokeh.util.session_id.generate_session_id() function to create valid session IDs. The external process and the Bokeh server must share the same BOKEH_SECRET_KEY environment variable.

external-signed mode is useful if you want another process to authenticate access to the Bokeh server; if someone is permitted to use the Bokeh application, you would generate a session ID for them, then redirect them to the Bokeh server with that valid session ID. If you don’t generate a session ID for someone, then they can’t load the app from the Bokeh server.

In both signed and external-signed mode, the secret key must be kept secret; anyone with the key can generate a valid session ID.

The secret key should be set in a BOKEH_SECRET_KEY environment variable and should be a cryptographically random string with at least 256 bits (32 bytes) of entropy. You can generate a new secret key with the bokeh secret command.

Development Options

The logging level can be controlled by the --log-level argument:

bokeh serve app_script.py --log-level=debug

The available log levels are: debug, info, warning, error or critical

* DEVELOP MODE BELOW NOT YET IMPLEMENTED *

Additionally, the Bokeh server supports a “develop” mode, which will watch application sources and automatically reload the application when any of them change. To use this mode, add the --develop argument on the command line:

bokeh serve app_script.py --develop

Note

The --develop mode option should not be used in “production” usage.