4.2 Run Python from Graph Object


Python code can be stored in a graphical object placed on a graph, worksheet or matrixsheet. The code gets executed if certain event of the object is triggered which is same as running Labtalk Script behind an object.

In order for the script to be recognized as Python, it should begin with either of the following: #,''' (triple single-quote), """ (triple double-quote), import.

Run Python Script From a Text Object

  1. Create a new workbook.
  2. Select the Text tool Button Text Tool.png from the Tools toolbar to the left side of the project window.
  3. Click on an open space on the worksheet (click in the gray area to the right of the last column). Type "Run" in the text object and click outside the object. You have now created a label for the button.
  4. Hold down the ALT key while double-clicking on the text object that you just created. The object dialog opens to the Programming tab.
  5. In the lower text box, enter this script:
  6. import pandas as pd
    import originpro as op
    
    data = {'Name':['Tom', 'Jack', 'Mike', 'Alice'],
            'Grade':[99, 98, 95, 90]}
    
    df = pd.DataFrame(data)
    wks = op.find_sheet()
    wks.from_df(df)
  7. From the Script Run After drop-down list, select Button Up, and click OK.
  8. Press the button and import data into the worksheet.
  9. Python Button.png