With the growing demand for Data Science and Analytics skill sets, drawing graphics programmatically is a very popular task these days. Especially the need for real-time and interactive graphics. You can easily create a GUI app for interactive plots by combining the power of the Bokeh library with Python4Delphi (P4D). P4D is a free set of powerful tools that allows you to work with Python scripts, modules, and types in Delphi and easily create Windows GUI with it.
Bokeh is an interactive visualization library for modern web browsers. It provides elegant, concise construction of versatile graphics, and affords high-performance interactivity over large or streaming datasets. Bokeh can help anyone who would like to quickly and easily make interactive plots, dashboards, and data applications.
This post will guide you on how to run the Bokeh library using Python for Delphi to display it in the Delphi Windows GUI app.
First, open and run our Python GUI using project Demo1 from Python4Delphi with RAD Studio. Then insert the script into the lower Memo, click the Execute button, and get the result in the upper Memo. You can find the Demo1 source on GitHub. The behind the scene details of how Delphi manages to run your Python code in this amazing Python GUI can be found at this link.
At a glance, Bokeh provides us with:
- Flexible
Bokeh makes it simple to create common plots but also can handle custom or specialized use-cases.
- Interactive
Tools and widgets let you and your audience probe “what if” scenarios or drill-down into the details of your data.
- Shareable
Plots, dashboards, and apps can be published on web pages or Jupyter notebooks.
- Productive
Work in Python close to all the PyData tools you are already familiar with.
- Powerful
You can always add custom JavaScript to support advanced or specialized cases.
- Open Source
Everything, including the Bokeh server, is BSD licensed and available on GitHub.
Let’s try an interactive map plot from Bokeh example gallery.
Table of Contents
1. Installing Sample Data
Bokeh can download and install a collection of sample data. This includes a variety of freely available data tables and databases that you can use with Bokeh. This sample data is rather large, so it is not included in Bokeh’s installation packages by default.
In this tutorial, we will use the Texas unemployment dataset in 2009 as our sample data. Use the following code to download all available sample datasets:
1 2 3 |
import bokeh.sampledata bokeh.sampledata.download() |
The result in Python GUI:
2. Demo: Plotting the Unemployment Rate in Texas
Run the following code in your Python4Delphi VCL Demo1, for plotting the unemployment rate in Texas. We use the demo from Bokeh documentation:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
from bokeh.io import show from bokeh.models import LogColorMapper from bokeh.palettes import Viridis6 as palette from bokeh.plotting import figure from bokeh.sampledata.unemployment import data as unemployment from bokeh.sampledata.us_counties import data as counties palette = tuple(reversed(palette)) counties = { code: county for code, county in counties.items() if county["state"] == "tx" } county_xs = [county["lons"] for county in counties.values()] county_ys = [county["lats"] for county in counties.values()] county_names = [county['name'] for county in counties.values()] county_rates = [unemployment[county_id] for county_id in counties] color_mapper = LogColorMapper(palette=palette) data=dict( x=county_xs, y=county_ys, name=county_names, rate=county_rates, ) TOOLS = "pan,wheel_zoom,reset,hover,save" p = figure( title="Texas Unemployment, 2009", tools=TOOLS, x_axis_location=None, y_axis_location=None, tooltips=[ ("Name", "@name"), ("Unemployment rate", "@rate%"), ("(Long, Lat)", "($x, $y)") ]) p.grid.grid_line_color = None p.hover.point_policy = "follow_mouse" p.patches('x', 'y', source=data, fill_color={'field': 'rate', 'transform': color_mapper}, fill_alpha=0.7, line_color="white", line_width=0.5) show(p) |
When you execute the script above, your default browser will automatically open to display your interactive plot.
Move your cursor to any County, and you can see the unemployment rate and the coordinate for that County. Bokeh also provides these tools in your plot: Pan, Wheel Zoom, Reset, Hover, Save or Download plot.
Congratulations, now you have learned how to run the Bokeh library using Python for Delphi to display it in the Delphi Windows GUI app! Now you can solve various real-world problems using enterprise-grade interactive plots created by the Bokeh library and Python4Delphi. The only limit is your imagination.
Check out the Bokeh data visualization library for Python and use it in your projects: https://pypi.org/project/bokeh/ and
Check out Python4Delphi which easily allows you to build Python GUIs for Windows using Delphi: https://github.com/pyscripter/python4delphi