Python – Bokeh and how to turn it into a GUI

Bokeh and how to turn it into a GUI… here is a solution to the problem.

Bokeh and how to turn it into a GUI

I’m writing code in Python and have all the power to analyze a dataset. Now I want to turn these features into a user-ready application that works like a .exe app in the sense that it works. In Bokeh I see that you can create drawings, tables, etc.; However, if it is possible to create a GUI, you can:

  1. Upload files for analysis
  2. Load

  3. functions from python I wrote to analyze uploaded files
  4. Plot the results onto a chart
  5. Clicking on different buttons that can take you to different (so-called) pages so that you can perform different functions.

Basically, it

can jump from one page to another, just like a web page where you click a button, it will link you to the next page for another purpose and then return to the home page. Can you do this with Bokeh?

Solution

There are several examples of data web applications created using Bokeh in demo.bokeh.org. This is a model modeled after “Shiny Movie Explorer”, but written in pure Python/Bokeh (not R/Shiny).

enter image description here

You can find information about creating and deploying Bokeh in Running a Bokeh Server The section of the documentation for more details of the application.

Here is a complete (but simpler) example that demonstrates the basic points and structure:

import numpy as np

from bokeh.io import curdoc
from bokeh.layouts import column
from bokeh.models import ColumnDataSource, Slider
from bokeh.plotting import figure

# Set up data
x = np.linspace(0, 4*np.pi, 200)
y = np.sin(x)
source = ColumnDataSource(data=dict(x=x, y=y))

# Set up plot
plot = figure(title="my sine wave")
plot.line('x', 'y', source=source, line_width=3, line_alpha=0.6)

# Set up widgets
freq = Slider(title="frequency", value=1.0, start=0.1, end=5.1, step=0.1)

# Set up callbacks
def update_data(attrname, old, new):

# Get the current slider values and set new data
    k = freq.value
    x = np.linspace(0, 4*np.pi, 200)
    y = np.sin(k*x)
    source.data = dict(x=x, y=y)

freq.on_change('value', update_data)

# Set up layouts and add to document    
curdoc().add_root(column(freq, plot))
curdoc().title = "Sliders"

To run locally, you need to execute:

bokeh serve --show myscript.py

For more complex deployments (i.e., using agents) or for direct embedding such as flask, see the documentation.

Related Problems and Solutions