You can use the Python programming environment in your CalcTree pages. For example, parameter n below is defined on this page's content. It is then used to calculate the factorial of n in the Python environment of this page.
The parameter factorial is defined and calculated inside the Python environment and used above on the page content.
Below is a step-by-step guide to quickly add a Python environment to your page while creating the above example calculation.
How to add a Python source to your page
Please follow these four steps:
Step one: Add a Python source
First, you should add a Python source to your page. Click on Python on the Integrations sidebar:
Select Python from the Integrations sidebar
If you cannot see the Integrations sidebar, click on the plug icon, then click
on + Add :
Click on + Add icon to see the Python option
When you click on Python, an example Python source code will be added:
A Python source is added to the page
Step two: Add parameter n to page content
Type /p on the page to receive the command menu:
type /p to get the parameter commands
click on the Parameter field or press Enter to insert a parameter on the page. Change the default Untitled title of the parameter to n:
Insert a parameter and set the title to n
Step three: Add Python logic
Add Python code inside the code editor. Try adding the following sample code:
Type this sample Python code to calculate factorial
Note that here, you can use parameter n, defined on the page content. To do that, start typing the title of the parameter to receive an auto-completion:
Using parameters defined on the page content inside Python
Click on the suggestion to use that page parameter in your Python code.
👉 Note that at the moment, you must click on that suggestion to use the parameter n from the page content. Soon, our Python editor will recognise the page parameters by just typing or pasting their titles in your code without having to click on the auto-completion suggestion.
Step four: add calculated Python parameter to page content
After typing the above example code, you will notice that the Python variable factorial will appear as a parameter under your Python source on the Integrations pane:
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 factorial into page:
Drag and drop source parameter on page content
You can alternatively insert source parameter factorial using the keyboard 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.
Still have more questions?
Join the conversation on Slack below!