You can utilize values entered by users from the UI in SQL and Python processes in data applications.
- When using the
load_data
function to chain processes, all variables are passed to all processes.
- Variables can be passed to SQL and Python from MDX using the
defineState
argument as shown below.
import { defineState } from "@morph-data/componnents";
export const dateRangeStart = defineState(undefined);
export const dateRangeEnd = defineState(undefined);
<DateRangePicker state={[dateRangeStart, dateRangeEnd]} />
<DataTable
alias="get_stock_data"
variables={{
start_date: dateRangeStart,
end_date: dateRangeEnd
}}
/>
The argument is actually executed with the following command in Morph’s CLI.
If executed in the Code tab, this argument is not added, so you need to specify the default value.
You can specify variables with -d
as follows.
morph run get_users -d user_type=admin
Use Variables in SQL/Python
Variables are passed to SQL as arguments enclosed in {{ }}
in Jinja format.
{{
config(
name="get_users",
)
}}
{% if user_type %}
select * from user where user_type = '{{ user_type }}'
{% else %}
select * from user
{% endif %}
Variables are passed to SQL as arguments enclosed in {{ }}
in Jinja format.
{{
config(
name="get_users",
)
}}
{% if user_type %}
select * from user where user_type = '{{ user_type }}'
{% else %}
select * from user
{% endif %}
Variables are passed to Python as context.vars[key] = value
.
@morph.func
def get_data_from_database(context):
var1_data = context.vars["var1"]
# write your code here using var1_data
return pd.DataFrame({})
You can set default values and add validation to variables using @morph.variables
.
@morph.func
@morph.variables("var1", default="value", required=True, type="str")
def get_data_from_database(context):
var1_data = context.vars["var1"]
# write your code here using var1_data
return pd.DataFrame({})