> ## 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.

# 変数を使う (variable関数)

<Warning>
  variable()関数は v0.1.0 で廃止されました。より柔軟で広範な用途に対応した、 defineState() 関数を使用してください。
</Warning>

Morphでは、Python関数やSQLファイルの中で変数を定義することができますが、MDXファイルから実行する場合にも変数を利用することができます。

Python, SQLファイル中での変数の利用については、[変数の使い方](/data-application/ja/variables)を参照してください。

## `variable` 関数

変数の宣言には `variable` を使用してください。

variableの型は、 string | undefined です。

```tsx theme={"dark"}
const dateRangeStart = variable(undefined);
const dateRangeEnd = variable(undefined);
```

## コンポーネントに渡す

コンポーネントに渡す際には、オブジェクト形式で、PythonまたはSQLファイル内での変数名をキーとして渡してください。

以下の例では、Pythonファイル中で start\_date と end\_date を変数として利用しているため、それらをキーとして渡しています。

<CodeGroup>
  ```tsx index.mdx theme={"dark"}
  export const name = 'index';
  export const title = 'Top page';

  # Stock prices with dates

  export const dateRangeStart = variable(undefined);
  export const dateRangeEnd = variable(undefined);

  <Grid cols="1">
    <VariableDatePicker
      type="range"
      variables={[
        dateRangeStart,
        dateRangeEnd
      ]}
    />
    <div className="h-[600px] w-full">
      <DataTable
        alias="stocks"
        variables={{
          start_date: dateRangeStart,
          end_date: dateRangeEnd
        }}
      />
    </div>
  </Grid>
  ```

  ```python stocks.py theme={"dark"}
  import pandas as pd

  import morph
  from morph import MorphGlobalContext

  import pandas as pd

  @morph.func(
      name="stocks",
      description="stock prices with dates",
      output_type="dataframe",
  )
  @morph.variables('start_date')
  @morph.variables('end_date')
  def main(context: MorphGlobalContext) -> pd.DataFrame:
      df = pd.read_csv('./src/stocks.csv')

      start_date = context.var.get('start_date', None)
      end_date = context.var.get('end_date', None)

      df['date'] = pd.to_datetime(df['date'], format='%b %d %Y')

      if start_date is None:
          start_date = df['date'].min()
      else:
          start_date = pd.to_datetime(start_date, format='%Y-%m-%d')

      if end_date is None:
          end_date = df['date'].max()
      else:
          end_date = pd.to_datetime(end_date, format='%Y-%m-%d')

      filtered_df = df[(df['date'] >= start_date) & (df['date'] <= end_date)]
      return filtered_df
  ```
</CodeGroup>


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.