description |
---|
Seamlessly integrate your Python code with your favorite APIs and databases, using WayScript. |
{% hint style="info" %} See Code Editors for more information on composing code in WayScript. {% endhint %}
WayScript's python editor lets you reference Variables that exist in your program. To do this, you can reference them through the variables dictionary.
As an example, let's create a variable called "Location" and set a value of "Brooklyn, NY"
You can reference the Location variable in your python script with this code:
location = variables[ "Location" ]
More abstractly, you reference any variable with the format:
var = variables[ "<var_name>" ]
{% hint style="info" %} You can drag your WayScript variables into your Python code to reference them that way, as well. {% endhint %}
You can also output data from your python scripts and turn these into variables that can be used by other Modules in your program.
You can do this with the variables dictionary in the following format:
variables[ "<Var Name>" ] = variable
# example
from datetime import datetime
variables[ "Date" ] = str( datetime.today() )
{% hint style="warning" %} WayScript outputs can only be data structures such as strings, lists, dictionaries, etc., but cannot be class objects. {% endhint %}
While working on your script, you can press the "Run Code" button inside the Python module to run the Python code and see updated results.
The Python module comes with many pre-installed Python Libraries. However, if you would like to use a module that isn't already installed, you can declare those requirements in the "Requirements.txt" file.
Declare these requirements as you would in a pip Requirements File, using the Requirements File Format.
{% hint style="info" %} For faster code execution, only add requirements that aren't already pre-installed in the Python module. {% endhint %}
To consume a Python GitHub repo as a package in your code, use the following format in your requirements.txt
file:
git+https://github.com/wayscript/wayscript-python.git#egg=wayscript
This example will install the wayscript-python
package from GitHub, with the egg named "wayscript".
{% hint style="warning" %}
In order for this to work properly, you may also need to add all Python requirements from the project you are referencing to your requirements.txt
file as well.
{% endhint %}
You can import code from the other Python files in your File Browser. For example, suppose you have a file called foo.py
, which has a method bar
.
You can import and use the bar
method in scratch.py
by using a relative import:
from .foo import bar
See the page below for snippets of example code for achieving different tasks using the Python module:
{% page-ref page="python-code-snippets.md" %}
{% embed url="https://www.youtube.com/watch?v=qKmv-6CcVvY" caption="" %}