We’ll start this example covering the basic concept of a tool in Py-Engineering.com. In this basic example we will, step by step, create a simple tool to calculate the length of the sides of a triangle using the Pythagoras theorem.
This can be done in the platform menu directly or by clicking ‘add tool’ in Platform ‣ Tools.
Menu for accounts with 'edit-tool' permissions.
The least you have to provide is a name and a slug. The name will appear as title of your tool. Slug has to be unique and defines the URL that can be used to access the tool. In this example, we’ll name our tool Pythagoras.
Go to Platform ‣ Tools ‣ Add tool
Name | Pythagoras | Defines name of tool |
Slug |
pythagoras
|
https://py-engineering.com/examples/
pythagoras
|
A tool on Py-Engineering takes user input and runs a python function to print and/or return some output. Say we want to use side A and B of a right triangle to determine the length of the side C, opposite the right angle with the Pythagorean theorem. We need to write a very basic python function like this:
import math
def main_function(input_dict):
a = input_dict['a']
b = input_dict['b']
c = math.sqrt(a ** 2 + b ** 2)
print(f'a = {a}')
print(f'b = {b}')print(f'c = {c:.1f}')
return {'c': c}
You’re free to define the python
script
with many other functions or classes, but the function that runs by clicking ‘Calculate’
has
to be called main_function
and takes a dictionary called
input_dict
as argument. Everything printed by the python script will show
up in
the results tab of the tool.
The return value of
main_function
will not be shown in
the Results tab of the GUI and might as well be None
. However, the tool
can
also be accessed through an API where the return value is retrieved in case it is a
dictionary. Read more about this in the tutorial .
All keys as expected to be present in
the
input_dict
have to be defined as input parameters. The least you have to
provide is a code which corresponds to the key in the input_dict
as used in
the
python function. In this example, that’s a
and b
. The
parameter
can also have a name and/or a unit.
Input_type is an integer in this example and determines the type of the
value in the input_dict
.
Input parameter and graphical user interface configuration.
By now, the tool is ready to be used. Just to provide the user some background information, we’ll add an extra tab that will appear in the GUI next to the ‘Results’ tab. These so called info tabs can be defined using HTML. The first tab, based on order will be visible by default. We’ll use the following html for a tab called ‘Info’.
Go to Platform ‣ Info tabs ‣ Add info tab
Name | Information |
Information [en] | see HTML below |
<p>
In mathematics, the Pythagorean theorem, also known as Pythagoras' theorem,
is a fundamental relation in Euclidean geometry among the three sides of a right triangle.
</p>
<img class="img-fluid" style="display: block; margin-left: auto; margin-right: auto;" src="https://pye-fileserver.s3.eu-central-1.amazonaws.com/media/public/images/pythagoras_AgL1OaF.png" alt="Pythagoras" width="262" height="131" />
<p>
It states that the square of the hypotenuse (the side opposite the right angle)
is equal to the sum of the squares of the other two sides. The theorem can be written
as an equation relating the lengths of the sides A, B and C, often called the "
Pythagorean equation" where C represents the length of the hypotenuse and A and B
the lengths of the triangle's other two sides.
</p>
Fill the form and click ‘Calculate’ in the embedded tool below or click here to view the tool on Py-Engineering.com.