Do you want to build an elegant, concise construction of versatile graphics, and affords high-performance interactivity over large or streaming datasets applications in Delphi/C++ builder? Don’t know where to start with? Don’t worry, with the help of Python4Delphi we can quickly and easily make interactive plots, dashboards, and data applications using Bokeh Python Library. This post helps to understand with sample Python script.
Delphi itself offers a number of advanced charting libraries. TeeChart is a third party chart bundled with the RAD Studio installation. If you have an existing Python application and you need to create charts you could use the Bokeh library or you could send the data to Delphi and create the chart there. Alternately, you could create the chart via Bokeh and then display it in the Delphi Windows GUI app. You can use Python4Delphi a number of different ways such as:
- Create a Windows GUI around you existing Python app.
- Add Python scripting to your Delphi Windows apps.
- Add parallel processing to your Python apps through Delphi threads.
- Enhance your speed sensitive Python apps with functions from Delphi for more speed.
- If not python and Python4Delphi is not installed on your machine, Check this, how to run a simple python script in Delphi application using Python4Delphi sample app
- Open windows open command prompt, and type pip install -U bokeh to install Bokeh. For more info for Installing Python Modules check here
- First, run the Demo1 project for executing Python script in Python for Delphi. Then load the script in the Memo1 field and press the Execute Script button to see the result. Go to GitHub to download the Demo1 source.
procedure TForm1.Button1Click(Sender: TObject);
PythonEngine1.ExecStrings( Memo1.Lines );
Bokeh is an interactive visualization library for modern web browsers. Bokeh exposes two interface levels to users:
- bokeh.models A low-level interface that provides the most flexibility to application developers.
- bokeh.plotting A higher-level interface centered around composing visual glyphs.
Bokeh Python Library sample script details: Plotting data in basic Python lists as a line plot, including zoom, pan, save, and other tools, is simple and straightforward:
- Prepare some data, In this case, plain Python lists, but NumPy arrays or Pandas series also work.
- Generate output using output_file() with the filename “lines.html”. Another option is output_notebook() for use in Jupyter notebooks.
- Call figure() this creates a plot with typical default options and easy customization of title, tools, and axes labels.
- Add renderers like line() for our data, specifying visual customizations like colors, legends, and widths.
- Ask Bokeh to show() or save() the results. These functions save the plot to an HTML file and optionally display it in a browser.
Steps three and four can be repeated to create more than one plot.
- When you execute this script, you will see that a new output file “lines.html” is created and that a browser automatically opens a new tab to display it (for presentation purposes we have included the plot output directly inline in this document).
from bokeh.plotting import figure, output_file, show
# prepare some data
x = [1, 2, 3, 4, 5]
y = [6, 7, 2, 4, 5]
# output to static HTML file
# create a new plot with a title and axis labels
p = figure(title="simple line example", x_axis_label='x', y_axis_label='y')
# add a line renderer with legend and line thickness
p.line(x, y, legend_label="Temp.", line_width=2)
# show the results
Note: Samples used for demonstration were picked from here. You can much more with this library from Providing Data Directly, Creating Layouts, Visualizing Network Graphs, and much more. You can check the APIs and some more samples from the same place. It’s mandatory to include the below code to avoid exceptions. To know more about MaskFPUExceptions check here.
You have read the quick overview of Bokeh library, download this library from here and build your dashboards, interactive plots in your applications. Check out Python4Delphi and easily build Python GUIs for Windows using Delphi.