How about wrapping your Delphi Objects to Python Objects with a single line of code? Sounds Interesting? Yes, Python4Delphi has the flexibility to do that using a TPyDelphiWrapper component. This benefits Delphi Developers easily to wrap the existing or new Delphi Objects into Python Objects. This post will guide you on how to wrap a FireDAC TFDTable to a python Object and Manipulates with table data using python scripts. You can also use Python4Delphi with C++Builder.
Python4Delphi Demo10_FireDAC Sample App shows how to wrap a Delphi Object(TFDTable, TFDQuery) to Python Object with some Examples as listed. You can find the Demo10_FireDAC source on GitHub.
Prerequisites: Download and install the latest Python for your platform. Follow the Python4Delphi installation instructions mentioned here. Alternatively, you can check out this video Getting started with Python4Delphi.
Components used in Python4Delphi Demo10_FireDAC App:
- TPythonEngine: A collection of relatively low-level routines for communicating with Python, creating Python types in Delphi, etc. It’s a singleton class.
- TPythonGUIInputOutput: Inherited from TPythonInputOutput (which works as a console for python outputs) Using this component Output property you can associate the Memo component to show the Output.
- TPythonModule: It’s inherited from TMethodsContainer class allows creating modules by providing a name. You can use routines AddMethod, AddMethodWithKW to add a method of type PyCFunction. You can create events using the Events property.
- TPyDelphiWrapper: Component that wraps the Delphi Object to Python Object inherited from TEngineClient. It has the capability to store Delphi class registration information, registration for Helper Types(not correspond to Delphi Classes), register python module functions, created event handlers. Wrap, WrapRecord, WrapInterface are the key methods to wrap Delphi Object, Record, and Interface respectively.
- TSynEdit: Syntax highlighting edit control, not based on the Windows common controls and supported on Windows. For some history check here. How to get and use SynEdit check here.
- TMemo: A multiline text editing control, providing text scrolling. The text in the memo control can be edited as a whole or line by line.
- Along with these components TFDConnection,TFDTable,TFDQuery, TDataSource were used.
You can find the Python4Delphi Demo10_FireDAC sample project from the extracted GitHub repository ..\Python4Delphi\Demos\Demo10_FireDAC.dproj. Open this project in RAD Studio 10.4.1 and run the application.-
Implementation Details:
- PythonEngine component provides the connection to Python or rather the Python API. This project uses Python3.9 which can be seen in TPythonEngine DllName property.
- PythonGUIInputOutput component provides a conduit for routing input and output between the Graphical User Interface (GUI) and the currently
executing Python script. - PyDelphiWrapper component contains Module and Engine Property which is associated with PythonEngine and modDBFireDAC respectively.
- In Example 1, SynEditScript1 load the Python script file Example1.py and clicking Execute Button, connected to the database selected in the Combobox, and the script is executed. Example1.py imports the module modDBFireDAC, create an own TFDTable object, and manipulates the table values mentioned in the script.
- In Example 2, SynEditScript2 loads the Python script file Example2.py and clicking Execute Button, the script is executed. Example2.py imports the module modDBFireDAC shows you how to create a TFDTable object connected to an already created Delphi TFDTable (which may include calculated fields and the like).
- In Example 3, SynEditScript3 loads the Python script file Example3.py and clicking Execute Button, the script is executed. Example3.py shows you how to connect a Python Table to a Delphi Datasource.
- In Example 4, SynEditScript4 loads the Python script file Example4.py and clicking Execute Button, the script is executed. Example4.py shows you how to create your own TFDQuery object and work with it.