3 Running Python Code
The following sections document various ways to store and execute Python code in Origin.
From Python Console
The Python Console is ideal if you have just a few lines of Python code to execute. Intellisense is supported in the console, so it is also very useful to explore objects and methods available in the originpro package.
Use the Connectivity: Python Console... menu in Origin to open Python Console window.
From Code Builder
You can develop your Python code using Code Builder. Code highlighting, intellisense, and debugging are supported.
Use the Connectivity: Open utitled.py... menu in Origin to open Code Builder. Once you have edited your code, you can run the code by pressing the F5 key. It is not even necessary to save the code to a file prior to executing. The Tools menu in Code Builder has three entries specific for Python:
- Python Packages: Install, update or uninstall packages
- Intellisense: Turn intellisense on/off
- Clean Python Packages: Clean/remove compiled PYC files in User Files Folder
From External .py File
You can run code saved in a .py file from anywhere in Origin where LabTalk Script is supported, such as the Script Window, using the Run command.
// Run code from file test.py saved in your User-Files Folder
run -pyf test.py;
// Run code from file saved in a specific folder
string str$ = "C:\temp\test.py";
run -pyf %(str$);
You can pass variables to your Python code from LabTalk script. Try the following:
- From Origin, use the menu Connectivity: Open untitled.py....
- In Code Builder, on the right side, you will see a tab named untitled.py. Paste the following Python code:
if __name__ == '__main__':
- Use the File:Save As... menu in Code Builder, and save the file as test.py to the default location, which is your User Files Folder.
- Go back to Origin and open the script window using the Origin menu Window: Script Window. Then paste the following lines of LabTalk script:
double var1 = 123.456;
string var2$ = 'hello';
run -pyf test.py "$(var1)" "%(var2$)";
- Drag your mouse and select all three lines of the script code and press Enter key to execute.
- You should see the output:
From .py File Attached to Project
The .py file containing your Python code can be attached to the Origin project. Once it is attached, you can execute the code using the script command:
// Execute Python code stored in file test.py attached to project
run -pyp test.py;
// Can leave out the .py extension
run -pyp test;
To create and attach a file to the project, do the following:
- Edit your Pyton code in Code Builder and save it to a file with desired name.
- Use the Code Builder menu File: Add to Workspace. Your file will now appear under the User branch of the Workspace tree on the left.
- Drag and drop your file from the User branch to the Project branch in the Workspace tree.
- Now the file is attached to the project.
- Double click on this entry under the Project branch to open the code in a new tab on the right side to perform further edits.
- From Origin, save your project. The code file is now attached to the project and the code can be executed from anywhere in Origin where Labtalk script is supported.
From Text Object
Python code can be stored in a text object placed on a graph, worksheet or matrixsheet. The code can then be executed using script command from another object such as a button, using the run command:
// Run the python code stored in text object named 'pycode'
run -pyb pycode;
Try the following:
- In Origin, use the menu File: New: Workbook...
- In the dialog the New Workbook dialog that opens, select the template named Lomb-Scargle Periodogram and click the OK button to open the template in Origin.
- Hold down the ALT key and double-click on the button object named Compute Periodogram. The Text Object dialog will open with the Programming tab active, and you will see the LabTalk script code:
pip -check scipy pandas;
run -pyb pycode;
- The first line uses the LabTalk pip command to check and install the packages required for this sample, which are scipy and pandas. The second line calls the Python code stored in the text label named pycode. You can now close this dialog.
- Now right-click on the text object named Python Code and select Properties... from the context menu.
- In the Text Object dialog, switch to the Programming tab. You will notice that the name property is set as pycode which is what the script behind the button was referring to. In the edit box below, you will see the Python code. You can now close this dialog.
- Now click the Lomb-Scargle Periodogram button in the worksheet. The python code will be executed and the peridogram result will appear in the C and D columns of the worksheet, and the graph will update to display the curve.
Calling Python Functions
Instead of running Python code, you can define Python functions and call the functions from Origin such as from Column Formula, Import Wizard and from Fitting Functions.