Actions

Actions are one of the latest features incorporated. They provide simple but powerful tools to create code snippets that can be easily shared and executed in the browser.

Actions are ideal for applications that require real time interaction. Moreover, they are executed using WebAssembly, which provides a high level of freedom when it comes to sharing, using and creating them.


Defining an Action

In the simplest way possible, an action is defined as a function with the reserved name action and the client as a parameter.

def action(shimoku_client: Client):
    #code [...]
    shimoku_client.run()

Then, on your main Python file, it is necessary to add the following code to create the action instance.

indicator_action_id = shimoku_client.actions.create_action(
    name="Your action name",
    code=open("MyAction.py", "r").read(),
    overwrite=True,
)

Snackbars

Actions provide a snackbar based method of communication on the frontend. It is useful to express different stages in the process and even handling errors. These snackbars can portray information, success or an error.

def action(shimoku_client: Client):

    global_front_end_connection.snackbar_info(
        "Starting action"
    )
    #code [...]
    
    if variable1 is None: #let variable be any important value
        global_front_end_connection.snackbar_error("Variable1 must have a value!")
        return
    
    global_front_end_connection.snackbar_info("Plotting the results")
    #plotting code [...]
    
    shimoku_client.run()
    global_front_end_connection.snackbar_success(
        "End of action"
    )

Implementation Examples

Simple Indicator Update

In example, we will create one of the simplest features in order to really showcase how do actions work and why are they useful.

It is simply a counter that sums 1 every time it is clicked and updates an indicator. It is useful to demonstrate the interactivity upgrade actions provide, specially for future, more complex, features.


Firstly, we will create the action Python file. In this case, it will be named increment_indicator_action.py.

Next, it is important to create the instance of the action and, hence, its button on the main file.

Damping Harmonic Oscillator

In this example, a visual representation of the relation between zeta an omega in an 'Damping Harmonic Oscillator' (a spring) is showcased using an action. The physics simulation itself is besides our point, but demonstrates the power of the action tools.


Once again, start by creating the action Python file. In this case, it is named spring_action.py.

As previously explained, it will be necessary to add the instance of the action and its button on the main Python file.

Linear Regression

In this example, the creation of a Linear Regression will be shown, implemented using Sklearn libraries. Here, it is showcased how the actions can empower you to use AI tools.


In the same way as the other two examples, create the action Python file. This on is named linear_regression_action.py.

Intuitively, it will be necessary to add the action instance and button to the main Python file.

Requests

This example shows how you could use external requests in your actions:


First create the action file, name it requests_action.py.

To make it work in the platform you can use a button that calls the action:

Handling Errors

It is usual having errors when using a new tool. For that reason it has been created a series of common errors to help you understand the nomenclature and structure of an action, while also troubleshoot your code in case of error. If your specific error can not be found down below, visit our mockable tests guide on GitHub here for more detailed and technical guidance.


Actions must always have the client parameter, annotated and in this specific format: shimoku_client: Client

Furthermore, an action can not be defined in a class.

There has to exist an action and it can not be redefined in the same file.

In terms of libraries, always import the client using the following specific format: from shimoku import Client. Furthermore, the asyncio library and its functionalities can not be imported.

It is crucial to maintain the integrity of Client. Here are some errors that do not respect that integrity.

Last updated

Was this helpful?