ToolJet/docs/versioned_docs/version-2.33.0/how-to/import-external-lib-py.md
Aman Regu 72fa067550
[docs]: Update formatting for How To section (#9169)
* Updated the formatting of how to docs for the next and current versions

* Used the title cases in the heading

* Updated the formatting of how to docs for the next and current versions

* Used the title cases in the heading

* updated the formatting of inspector, form, cell colors

* resolved conflicts

* Updated the formatting of bulk update multiple rows, conditionaly format table, delete multiple rows, serverside pagination

* Updated access user groups, import external lib in js and python, use axios

* fix: border colour, blur

* Revert changes to versions.json

* Changed the formatting of how to docs

* add changes from docs/next to v2.34.0

* update: how to docs

* fix: image name create-new-query

* fix: image name import-successful.png

* fix: image name flatten-js

* fix: image name math-js-v2

* update: shorten page titles

---------

Co-authored-by: Asjad Ahmed Khan <iitasjad2001@gmail.com>
Co-authored-by: Karan Rathod <karan.altcampus@gmail.com>
2024-04-11 11:34:27 +05:30

3.1 KiB

id title
import-external-libraries-using-runpy Import External Libraries Using RunPy

ToolJet allows you to utilize python packages in your app by importing them using the RunPy query. In this how-to guide, we will import a few packages and use them in the application.

:::caution Unsupported modules 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. :::

  • Start by creating a new application in ToolJet.
  • From the Query Panel, add a new RunPy query - it will be named runpy1 by default.
Import external libraries using RunPy
  • Use micropip to install packages like Pandas and NumPy. Run the query to complete installation.
import micropip
await micropip.install('pandas')
await micropip.install('numpy')
Import external libraries using RunPy
  • Enable Run this query on application load? to make these packages available every time the application loads.

Generating Random Numbers with NumPy

  • Create a RunPy query using NumPy's random module to generate random numbers.
from numpy import random
x = random.binomial(n=10, p=0.5, size=10)
print(x)
Import external libraries using RunPy

You can check the output on the browser's console.

Parse CSV data

  • Create a RunPy query to parse CSV data using StringIO, csv, and Pandas module.
from io import StringIO
import csv
import pandas as pd

scsv = components.filepicker1.file[0].content

f = StringIO(scsv)
reader = csv.reader(f, delimiter=',')

df = pd.DataFrame(reader)

print(df.info())
print(df)
Import external libraries using RunPy
  • 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.
Import external libraries using RunPy
  • Finally, load a csv file on the File Picker component, Run related RunPy query and check the output on the browser console.
Import external libraries using RunPy