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 no executed in the SDK, 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.
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.
defaction(shimoku_client: Client): global_front_end_connection.snackbar_info("Starting action" )#code [...]if variable1 isNone:#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 Example
In this example, a visual representation of the relation between zeta an omega in an 'Damping Harmonic Oscillator' is showcased using an action.
import numpy as npfrom numpy import array, piimport pandas as pd#shimoku necessary librariesfrom shimoku import Clientfrom shimoku.actions_execution.front_connection import global_front_end_connection# physics calculationsNt =1000t = np.linspace(0.0, 10.0, Nt)dt = t[1]- t[0]# Time stepdefode(X: array,zeta:float=0.05,omega0:float=2.0* pi) -> array: x, dotx = X ddotx =-2* zeta * omega0 * dotx - omega0**2* xreturnarray([dotx, ddotx])defrk4_step(X: array,zeta:float,omega0:float,loc_dt: array) -> array: k1 =ode(X, zeta, omega0) k2 =ode(X +0.5* k1 * loc_dt, zeta, omega0) k3 =ode(X +0.5* k2 * loc_dt, zeta, omega0) k4 =ode(X + k3 * loc_dt, zeta, omega0)return X + (k1 +2* k2 +2* k3 + k4) * loc_dt /6defupdate(zeta:float=0.05,omega0:float=2.0* pi): X = np.array([1.0, 0.0]) values = [X[0]]for _ inrange(1, Nt): X =rk4_step(X, zeta, omega0, dt) values.append(X[0])return values# defining the actiondefaction(shimoku_client: Client): global_front_end_connection.snackbar_info("Beginning action of Damping Harmonic Oscillator" ) shimoku_client.set_menu_path("Actions", "Damping Harmonic Oscillator") input_form_values = shimoku_client.plt.get_input_forms()[0]["data"] zeta =float(input_form_values["zeta"]) omega0 =float(input_form_values["omega0"])if zeta <0: global_front_end_connection.snackbar_error("Zeta can not be negative")returnif omega0 <0: global_front_end_connection.snackbar_error("Omega can not be negative")return y =update(zeta=zeta, omega0=omega0) df = pd.DataFrame(t, columns=["Time"]) df["Value"]= y global_front_end_connection.snackbar_info("Plotting the results") shimoku_client.plt.line( data=df, order=2, x="Time", y="Value", rows_size=2, cols_size=12 ) shimoku_client.run() global_front_end_connection.snackbar_success("End of action of Damping Harmonic Oscillator" )
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.
#CORRECT WAYdefaction(shimoku_client: Client): pass#INCORRECT WAYS#ERROR1defaction(): # always add the client parameterpass#ERROR2defaction(shimoku_client): # always annotate the clietn variablepass#ERROR3 defaction(shimoku_client:int): # the client variable must be of type Clientpass#ERROR4defaction(shimo: Client): # the client variable must be named shimoku_clientpass#ERROR5defaction(shimoku_client: Client,value1): # cannot use arbitrary variables as paramspass#ERROR6classUsesClient:defaction(self,shimoku_client: Client): # cannot declare the action in a classpass
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.
#ERROR12defaction(shimoku_client: Client):use_s(other_param=shimoku_client)#cannot use other params for the client variable#ERROR13defuse_s(shimoku_client: Client):return shimoku_client #cannot return the client#ERROR14shimoku_client =6#cannot reassign the client variabledefaction(shimoku_client: Client):pass#ERROR15variable =Client()#cannot create another clientdefaction(shimoku_client: Client):pass#ERROR16from shimoku import Client as shimo # cannot rename the clientdefaction(shimoku_client: shimo):pass