2023-12-27 12:17:43 +00:00
---
id: import-external-libraries-using-runpy
2024-01-12 11:08:07 +00:00
title: Import External Libraries Using RunPy
2023-12-27 12:17:43 +00:00
---
ToolJet allows you to utilize python packages in your app by importing them using the [RunPy query ](/docs/data-sources/run-py ).
2024-01-12 11:08:07 +00:00
In this how-to guide, we will import a few packages and use them in the application.
2023-12-27 12:17:43 +00:00
:::caution Unsupported modules
2024-01-12 11:08:07 +00:00
Modules with C/C++ extensions needing system libraries won't work in Pyodide, as it runs in a web browser without system library access. Pyodide, based on WebAssembly-compiled Python, also doesn't support certain system calls.
2023-12-27 12:17:43 +00:00
:::
2024-01-12 11:08:07 +00:00
- Start by creating a new application in ToolJet.
- From the Query Panel, add a new RunPy query - it will be named *runpy1* by default.
2023-12-27 12:17:43 +00:00
2024-01-12 11:08:07 +00:00
< div style = {{textAlign: ' left ' , marginBotton: ' 15px ' } } >
2023-12-27 12:17:43 +00:00
< img className = "screenshot-full" src = "/img/how-to/import-python/runpy.png" alt = "Import external libraries using RunPy" / >
2024-01-12 11:08:07 +00:00
< / div >
2023-12-27 12:17:43 +00:00
2024-01-12 11:08:07 +00:00
- Use micropip to install packages like Pandas and NumPy. **Run** the query to complete installation.
2023-12-27 12:17:43 +00:00
2024-01-12 11:08:07 +00:00
```python
import micropip
await micropip.install('pandas')
await micropip.install('numpy')
```
2023-12-27 12:17:43 +00:00
2024-01-12 11:08:07 +00:00
< div style = {{textAlign: ' center ' } } >
2023-12-27 12:17:43 +00:00
< img className = "screenshot-full" src = "/img/how-to/import-python/installing.png" alt = "Import external libraries using RunPy" / >
2024-01-12 11:08:07 +00:00
< / div >
2023-12-27 12:17:43 +00:00
2024-01-12 11:08:07 +00:00
- Enable `Run this query on application load?` to make these packages available every time the application loads.
2023-12-27 12:17:43 +00:00
2024-01-12 11:08:07 +00:00
## Generating Random Numbers with NumPy
2023-12-27 12:17:43 +00:00
2024-01-12 11:08:07 +00:00
- Create a RunPy query using NumPy's random module to generate random numbers.
2023-12-27 12:17:43 +00:00
2024-01-12 11:08:07 +00:00
```python
from numpy import random
x = random.binomial(n=10, p=0.5, size=10)
print(x)
```
2023-12-27 12:17:43 +00:00
2024-01-12 11:08:07 +00:00
< div style = {{textAlign: ' center ' } } >
2023-12-27 12:17:43 +00:00
< img className = "screenshot-full" src = "/img/how-to/import-python/random.gif" alt = "Import external libraries using RunPy" / >
2024-01-12 11:08:07 +00:00
< / div >
2023-12-27 12:17:43 +00:00
2024-01-12 11:08:07 +00:00
*You can check the output on the browser's console.*
2023-12-27 12:17:43 +00:00
2024-01-12 11:08:07 +00:00
## Parse CSV data
2023-12-27 12:17:43 +00:00
2024-01-12 11:08:07 +00:00
- Create a RunPy query to parse CSV data using `StringIO` , `csv` , and `Pandas` module.
2023-12-27 12:17:43 +00:00
2024-01-12 11:08:07 +00:00
```python
from io import StringIO
import csv
import pandas as pd
2023-12-27 12:17:43 +00:00
2024-01-12 11:08:07 +00:00
scsv = components.filepicker1.file[0].content
2023-12-27 12:17:43 +00:00
2024-01-12 11:08:07 +00:00
f = StringIO(scsv)
reader = csv.reader(f, delimiter=',')
2023-12-27 12:17:43 +00:00
2024-01-12 11:08:07 +00:00
df = pd.DataFrame(reader)
2023-12-27 12:17:43 +00:00
2024-01-12 11:08:07 +00:00
print(df.info())
print(df)
```
2023-12-27 12:17:43 +00:00
2024-01-12 11:08:07 +00:00
< div style = {{textAlign: ' center ' } } >
2023-12-27 12:17:43 +00:00
< img className = "screenshot-full" src = "/img/how-to/import-python/csvparse.png" alt = "Import external libraries using RunPy" / >
2024-01-12 11:08:07 +00:00
< / div >
2023-12-27 12:17:43 +00:00
2024-01-12 11:08:07 +00:00
- Add a **File Picker** component on the canvas
- Select `On File Loaded` as the Event and Run Query as the Action.
- Select the query we just created as the Query.
2023-12-27 12:17:43 +00:00
2024-01-12 11:08:07 +00:00
< div style = {{textAlign: ' center ' } } >
2023-12-27 12:17:43 +00:00
< img className = "screenshot-full" src = "/img/how-to/import-python/event.png" alt = "Import external libraries using RunPy" / >
2024-01-12 11:08:07 +00:00
< / div >
2023-12-27 12:17:43 +00:00
2024-01-12 11:08:07 +00:00
- Finally, load a csv file on the File Picker component, **Run** related RunPy query and check the output on the browser console.
2023-12-27 12:17:43 +00:00
2024-01-12 11:08:07 +00:00
< div style = {{textAlign: ' center ' } } >
2023-12-27 12:17:43 +00:00
< img className = "screenshot-full" src = "/img/how-to/import-python/console.gif" alt = "Import external libraries using RunPy" / >
2024-01-12 11:08:07 +00:00
< / div >
2023-12-27 12:17:43 +00:00