Documentation Index
Fetch the complete documentation index at: https://docs.morph-data.io/llms.txt
Use this file to discover all available pages before exploring further.
データアプリでUIからユーザーが入力した値をSQLとPythonの処理の中で活用することができます。
load_data関数を使用して処理を連続させた場合は、全ての変数が全ての処理に対して渡されます。
- SQlやPythonには、MDXから以下のように
defineStateという引数で変数を渡す事ができます。
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.value,
end_date: dateRangeEnd.value
}}
/>
引数は実際にMorphのCLIでは以下のようなコマンドで実行されます。
Codeのタブで実行された場合には、この引数は追加されないのでデフォルトの値を指定する必要があります。
以下のように-dでvariablesを指定することができます。
morph run get_users -d user_type=admin
SQL/Pythonで変数を受け取る
jinjaの形式で{{ }}で囲うと変数とみなされ渡された引数が入ります。{{
config(
name="get_users",
)
}}
{% if user_type %}
select * from user where user_type = '{{ user_type }}'
{% else %}
select * from user
{% endif %}
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({})
@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({})