Quickly Evaluate A Python Expression In Delphi Using Python4Delphi Sample App

Want to know how to evaluate a Python Expression in Delphi using Python4Delphi? Python for Delphi (P4D) is a set of free components that wrap up the Python DLL into Delphi and C++Builder. They let you easily execute Python scripts, create new Python modules and new Python types. This post will guide you on how to use these components, create a VCL application, run a simple python expression in it, and shows the output message.

Python4Delphi Demo2 Sample App shows how to Evaluate a Python Expression by typing the python expression in a Memo, execute and show the message. You can find the Demo2 source on GitHub. Check Demo1 on how to run a simple python script in Delphi.

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 Demo2 App:

  1. TPythonEngine: A collection of relatively low-level routines for communicating with Python, creating Python types in Delphi, etc. It’s a Singleton class. Some of the key properties for the component listed here.
  • AutoLoad: Enable this property to load the python DLL automatically. It defaults to true.
  • AutoUnload: Enable this property to unload the python DLL automatically. It defaults to true.
  • DllName: By default, it reflects the latest e.g. python39.dll. But you can provide your registered version.
  • DllPath: Physical location of the Python DLL.
  • InitScript: You can provide the python script here to execute implicitly.
  • IO: Associate the TPythonGUIInputOutput component to this property.
  • RegVersion: Registered python version e.g 3.9.
  • UseLastKnownVersion: If you want to use the latest registered Python version installed set this property to True.

2.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.

  • MaxLineLength: Maximum character length per line.
  • MaxLines: Maximum python script lines can be executed using this component.
  • OutPut: Associate the Memo component to show the output.
  • RawOutput: Produce the result as raw output.
  • UniCodeIO: Set true to provide Input and output as Unicode.

3.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.

You can find the Python4Delphi Demo2 sample project from the extracted GitHub repository ..Python4DelphiDemosDemo02.dproj. Open this project in RAD Studio 10.4.1 and run the application.

Implementation Details:

  • PythonEngine1 provides the connection to Python or rather the Python API. This project uses Python3.9 which can be seen in TPythonEngine DllName property.
  • PythonGUIInputOutput1 provides a conduit for routing input and output between the Graphical User Interface (GUI) and the currently
    executing Python script.
  • Memo1, used for providing the Python Expression and Memo2 for showing the output(instead Memo2 used ShowMessage to show the expression result) Buttons to perform the execution, load script from, and save the script to file.
  • On Clicking Execute Button the script expression is executed using the below code.

EvalStrings will evaluate the Mathematical expression provided in the Memo1 and return the Python Object(PPyObject). We can convert the resulted Python object as a string using PyObjectAsString. Earlier in Demo1, we use ExecStrings to run the python script, so what’s the difference between the two? EvalStrings can only execute or evaluate expressions, whereas ExecStrings can execute any piece of Python code. To understand more about python expression check here.

Load the script to the Memo1 from a file using the code,

Save the Script to the file using the code,

Python4Delphi Demo02

Check some of the tutorials available for Python4Delphi here