it:delphi_python
Table of Contents
Python language in Delphi
Introduction
- Delphi 11 now has ability to run Python scripts via Python4Delphi which has a wrapper for the Python dll
- SynEdit is an optional library that provides syntax highlighting and proper indention behaviors if you want to allow users to edit Python code in your application.
Python4Delphi (P4D)
- let's you run Python scripts from Delphi
- can create new Python modules
- can create Python extensions as DLLs
- if you get error “cannot load python DLL”:
- ensure you have compiled your Delphi app with same version (32bit or 64bit compile) as the Python on your computer - you should generally be using 64 bit!
Displaying plots as well as text outputs from running Python scripts
- as plots are SVG files from matplotlib you need to install svgiconimagelist components into Delphi
- optoinally you can also install Microsoft Edge Webview 2 to display results in Delphi TEdgeViewer - but this method is not shown here
- add to your form:
- initialization section on the Form source file to prevent module import errors:
initialization MaskFPUExceptions(True);
- scripting control:
- SynPythonSyn
- SynEdit - set highlighter to SynPythonSyn
- SynEditPythonBehaviour1 - set editor to SynEdit
- text output component
- eg. a memo component
- plot output component
- SVGIconImage
- Python4Delphi components
- PythonGUIInputOutput - link it to a memo component to display text outputs
- PythonEngine - set IO to the PythonGUIInputOutput
- PythonModule - name it delphi_module, set engine to PythonEngine
- PyWrapper - set engine to PythonEngine and set Module to PythonModule
- Button to run script
- OnClick:
var Script: string; begin Script := SynEdit1.Text; GetPythonEngine.ExecString(Utf8Encode(Script)); end;
- Form Create event:
begin var Py := PyDelphiWrapper.Wrap(SVGIconImage, soReference); PythonModule.SetVar('svg_image', Py); GetPythonEngine.Py_DECREF(Py); end;
- then your Python script must be modified as follows (or you can automatically add these in code to the script in the OnButtonClick event:
- append:
from delphi_module import svg_image from io import StringIO figfile = StringIO() plt.savefig(figfile, format='svg') figdata_svg = figfile.getvalue() svg_image.SvgText = figdata_svg
- remember - any new script you run will just will act like a new kernel cell, so variables and imports from prior runs will remain in memory!
- you have to close app and restart it to create a new clean kernel
TPythonEngine
- UseLatestVersion - automatically detects latest registered version on your computer
- Python DLL name - I set this to python311.dll (default was python37.dll)
- DLL path - I set this to my environment path as per Anaconda
- Registered version
- IO - link to a TPythonGUIInputOutput component
- run Python script via: PythonEngine1.ExecStrings( memo1.Lines );
- To avoid module import FloatDivideZero errors, type PythonEngine.MaskFPUExceptions(True); prior to calling PythonEngine1.ExecStrings( memo1.Lines ); - preferably in the Form initialization section
TPythonGUIInputOutput
- Output - link to a TMemo
TPythonModule
- create a Python module using Delphi code which can be called from Python using a low level approach
- Delphi maths may be 100x faster than python maths eg. in determining if an integer is a prime
- Engine - link to TPythonEngine
- ModuleName eg. Delphi_maths_module
- then use the event handler to run Delphi code and send Python Objects - but you will also need to call reference counter for Python as Python reference counts objects PyINCREF(result) where result is a PyObject
TPyWrapper
- high level approach to use Delphi code which can be called from Python - uses RTTI, no need for python reference count issues at each function level
- link to TPyModule
TPythonType
- create a Python type class in Delphi
- Engine - link to TPythonEngine
- TypeName
- Module - link to TPythonModule
- Services
- etc
TPythonDelphiVar
- Engine - link to TPythonEngine
- Module
- VarName
TPythonInputOutput
- receive python input
- RawOutput true/false
- UnicodeIO true/false
Other components utilising Python4Delphi Python binding
- Keras4Delphi
- a high-level neural networks API, written in Delphi's Object Pascal with Python Binding and capable of running on top of TensorFlow.
- Supports both convolutional networks and recurrent networks, as well as combinations of the two. Runs seamlessly on CPU and GPU.
- uses python4delphi and partial conversion of NumPy4Delphi
- requires Python 3, keras, nump, tensorflow to be installed
Python Environments
- failed to install
Lightweight Python Wrappers
- wrappers based on P4D allow import of Python modules into Delphi components
- works with Python Environments
Delphi4Python
- allows using Delphi VCL and FMX form designers to create a Python GUI and export them for use in Python
it/delphi_python.txt · Last modified: 2023/10/19 01:40 by gary1