# Import: Generated UI from any Python Function

{% hint style="info" %}
&#x20;This is a [Mito Enterprise](https://trymito.io/plans) feature. [Upgrade](https://www.trymito.io/plans) to extend your spreadsheet with additional spreadsheet functions.
{% endhint %}

The Mito spreadsheet is built to be extensible to your teams use case. Mito users can augment the data analysis capabilities of their team by incorporating existing Python functions into their sheet.&#x20;

Namely, any function that returns a dataframe can be exposed to a Mito user within a spreadsheet, automatically. **This allows less-technical teammates to benefit from all the Python code written by the programmers in your org, without these less-technical folks needing to learn to code! Also, it requires no additional UI building by you!**

See also: [bring your own spreadsheet functions.](https://docs.trymito.io/how-to/interacting-with-your-data/bring-your-own-spreadsheet-functions)

## How to add a custom importer into Mito

Imagine we have a function that takes some inputs and returns a dataframe:

```python
def get_loan_data(date: str, include_duplicates: bool) -> pd.DataFrame:
    ...
```

You can simply pass this to the mitosheet call in a notebook or Streamlit:

```python
# In a notebook
mitosheet.sheet(importers=[get_loan_data])

# In Streamlit
spreadsheet(importers=[get_loan_data])
```

Then, this function will be accessible in the **Import** dropdown within the mitosheet, where an option for **Get Loan Data** will appear.

<figure><img src="https://2294704369-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MP_U5ZCmiamDOXEOOTC%2Fuploads%2Fl5dPjkMOROD0JgziNnXU%2FScreen%20Shot%202023-11-29%20at%2012.45.26%20PM.png?alt=media&#x26;token=17179630-eca1-467b-8be2-f344c0c84e3f" alt="" width="188"><figcaption><p>The new import option is created.</p></figcaption></figure>

Selecting this option will open a UI this is automatically generated for this function:

<figure><img src="https://2294704369-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MP_U5ZCmiamDOXEOOTC%2Fuploads%2FFKHxymU86X7UFND4Qtia%2FScreen%20Shot%202023-11-29%20at%2012.45.33%20PM.png?alt=media&#x26;token=a0f65b35-a75c-4d92-bd6f-648666f7f144" alt="" width="375"><figcaption><p>The interface generated automatically by this Python function.</p></figcaption></figure>

## Adding a custom importer to Mito in Streamlit

Simply use the `importers` parameter to the `spreadsheet` component in Streamlit. See more in the [Streamlit API reference.](https://docs.trymito.io/mito-for-streamlit/api-reference)

## Supported Types

For Mito to automatically generate the correct UI for your function, it should have Python types attached to it. The currently supported types:

<table><thead><tr><th width="161">Type</th><th>UI Element</th></tr></thead><tbody><tr><td>str</td><td>A string input.</td></tr><tr><td>int</td><td>A number input. The input will be cast with the <code>int</code> function.</td></tr><tr><td>float</td><td>A number input. The input will be cast with the <code>float</code> function.</td></tr><tr><td>bool</td><td>A toggle. The input will be cast with the <code>bool</code> function.</td></tr><tr><td>pd.DataFrame</td><td>A dataframe select, allowing users to select from the dataframes in Mito.</td></tr><tr><td>List[int]</td><td>A list of number inputs. Each input will be cast with the int function.</td></tr><tr><td>Dict[str, str]</td><td>A pair of string inputs.</td></tr><tr><td>ColumnHeader</td><td>A column select, referencing the nearest pd.DataFrame type. <a href="#columnheader">See below.</a></td></tr><tr><td><em>unlabeled</em></td><td>An input that will be evaluated with the Python <code>eval</code> function.</td></tr></tbody></table>

#### Understanding the ColumnHeader type

The `ColumnHeader` type can be imported from the Mito extension package, and tells Mito to generate a UI that allows a user to select a specific column from the preceding dataframe in the auto-generated UI.

```python
import pandas as pd
from mitosheet.extensions.v1 import ColumnHeader

def extension_function(df: pd.DataFrame, column_header: ColumnHeader):
    # insert function here
```

Notably, any ColumnHeader parameter will error if it is not preceeded by a `pd.DataFrame` parameter -- as it is unclear which dataframe the `ColumnHeader` is referencing!

## Examples

**Using the List\[int] data type to let users filter a dataframe to relevant rows.**

```python
import mitosheet
import pandas as pd
from typing import List

def get_fund_data(fund_ids: List[int]):
    fund_performances = pd.DataFrame({
        'Fund ID': [1,2,3,4,5,6,7,8,9],
        'June Returns': [.1,.2,.3,.4,.5,.6,.7,.8,.9],
        'July Returns': [.1,.2,.3,.4,.5,.6,.7,.8,.9]
    })
    
    # Filter the dataframe to only include the fund_ids set by the user
    filtered_fund_data = fund_performances[fund_performances['Fund ID'].isin(fund_ids)]
    
    return filtered_fund_data
    
mitosheet.sheet(importers=[get_fund_data])
```

<figure><img src="https://2294704369-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MP_U5ZCmiamDOXEOOTC%2Fuploads%2Fafqb5vq8BHBPUi7311Wi%2FScreenshot%202024-08-29%20at%2010.15.12%E2%80%AFAM.png?alt=media&#x26;token=a6679c65-94ac-451f-a0a3-bac5ab10f6e3" alt=""><figcaption></figcaption></figure>
