When you add a Python script as a source to a page, CalcTree is ready to connect that source to the rest of your page via Parameters. That means if you have parameters defined on your page content, you can use them instantly in your Python code.
Additionally, Python code variables can be taken as source parameters, making them instantly available on your page..
How-To
Use parameters from the page inside the Python code
When working in Python, type the name of the page parameter to use auto-completion. For example, if the page has a parameter called n, simply type 'n' in your Python code:
Using parameters defined on the page content inside Python
Click on the suggestion to use that page parameter in your Python code.
Alternatively, you can reference a page parameter within Python using the _page_ prefix. For instance, to utilize a page parameter named R, use _page_R within Python:
🧐 Note that a page parameter can come from either inserting a parameter field through the building blocks menu (by typing "/"), or by manually mapping a single value from an Excel spreadsheet source. You can learn more about how to map excel parameters to the page here. Using an Excel table inside Python
Just like referencing a single value parameter, you can reference an Excel table from the page inside Python. It will be recognized as an array of arrays:
You can learn more about how to add a table from a spreadsheet to the page here. Use variables from Python code inside the page
After adding your code inside the code editor, you will notice that variables defined outside functions appear under the Python source on the Integrations pane. That means they have become source parameters, e.g:
Python variables appear as source parameters
🧐 Note that only variables defined at the global scope will be taken as source parameters. In other words, variables defined within a function won't be considered source parameters.
You can now drag and drop the source parameter into page:
Drag and drop source parameter on page content
You can alternatively insert source parameter factorial using the keyboard. For example, if the parameter is called factorial, you can insert it by typing the following: /p → Enter → fa → Down Arrow → Enter.
Inserting a source parameter by keyboard
🙌 Bonus: if you don't want a Python variable to be taken as a parameter, start it with an underline, e.g. _i instead of i.
Now, your Python variables are mapped as parameters on CalcTree. They can be used on your page like any other parameter in your workspace. Read more about CalcTree parameters here. You can even chain parameters from different sources to create multi-step calculations across Python, Spreadsheets and Mathematical expressions.
Still have more questions?
Join the conversation on Slack below!