跳到主要内容
版本:v2.0.0

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 it in the application.

Unsupported modules

The modules that are not currently supported in Pyodide are those that have C or C++ extensions that rely on system libraries. These modules cannot be used in Pyodide because it runs in a web browser, which does not have access to the underlying system libraries that the C or C++ extensions rely on. Additionally, Pyodide uses a version of Python that has been compiled to WebAssembly, which does not support the same system calls as a regular version of Python. Therefore, any module that requires access to system libraries or system calls will not work in Pyodide.

  • Create a new application and then create a new RunPy query from the query panel.

    Import external libraries using RunPy
  • Let's write some code for importing packages. We will first import the micropip which is a package installer for Python and then we will install the Pandas and NumPy using micropip. Run the query to install the packages.

    import micropip
    await micropip.install('pandas')
    await micropip.install('numpy')
    Import external libraries using RunPy
提示

Enable the Run this query on application load? option to make the packages available throughout the application.

Examples

Array of random numbers of using NumPy

  • Let's create a RunPy query that will use random module from the NumPy package and the query will generate array of 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 using

  • Let's create a RunPy query that will parse the data from the csv file. In this query we will use 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 and set a event handler for On file loaded event to Run Query that we created for parsing the data.

    Import external libraries using RunPy
  • Finally, let's load a csv file on the file picker and check the output by the RunPy query on the browser console.

    Import external libraries using RunPy