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.
1 2 3 4 5 6 7 8 9 10 11 |
procedure TMain.btnExecuteExample1Click(Sender: TObject); var l_sConnName: String; begin l_sConnName := cobxConnSQLServer.Items[cobxConnSQLServer.ItemIndex]; if self.DBConnectionClosedCheck(l_sConnName) then begin with GetPythonEngine do begin ExecStrings( SynEditScript1.Lines ); end; end; end; |
- 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).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
procedure TMain.btnExecuteExample2Click(Sender: TObject); var pyObj : PPyObject; begin // Instantiate a new Python object TPyTable pyObj := PyDelphiWrapper.Wrap(tblCustomer); with GetPythonEngine do begin // Define a new variable "T" in the DB module modDBFireDac.SetVar( 'T', pyObj ); Py_XDecRef(pyObj); // Excecute the script ExecStrings( SynEditScript2.Lines ); end; end; |
- 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
procedure TMain.btnExecuteExample3Click(Sender: TObject); var pyObj : PPyObject; l_oTable: TFDTable; begin // connect the Datasource2 to the Python Table l_oTable := TFDTable.Create(Self); l_oTable.TableName := 'Customer'; l_oTable.Connection := Connection; Datasource2.Dataset := l_oTable; l_oTable.Open(); pyObj := PyDelphiWrapper.Wrap(l_oTable, soOwned); with GetPythonEngine do begin // Define a new variable "T" in the DB module modDBFireDac.SetVar( 'T', pyObj ); Py_XDecRef(pyObj); // Excecute the script ExecStrings( SynEditScript3.Lines ); end; end; |
- 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.
1 2 3 4 5 6 7 8 9 10 |
procedure TMain.btnExecuteExample4Click(Sender: TObject); var l_sConnName: String; begin l_sConnName := cobxConnSQLServer.Items[cobxConnSQLServer.ItemIndex]; if self.DBConnectionClosedCheck(l_sConnName) then begin with PythonEngine do ExecStrings( SynEditScript4.Lines ); end; end; |
Design. Code. Compile. Deploy.
Start Free Trial Upgrade Today
Free Delphi Community Edition Free C++Builder Community Edition