This guide will give you a brief overview of package management in a browser-based Python environment and how you can specify custom dependencies for your Gradio apps via the quarto-gradio extension.
Available Python Modules
Letโs say we want to display a Plotly chart in a Gradio app. However, by default, we are only allowed to import Python modules that come with the Pyodide distribution used by Gradio Lite and plotly is not one of them.
Installation via gradio.requirements
You can specify any number of Python packages in the gradio.requirements metadata.
plotly==5.24.0
import gradio as gr
import plotly.express as px
datasets = {
"iris": px.data.iris(),
"gapminder": px.data.gapminder(),
"tips": px.data.tips()
}
default_dataset = list(datasets.keys())[0]
def plot_figure(dataset):
if not dataset:
return None
df = datasets[dataset]
return px.scatter(
df,
x=df.columns[0],
y=df.columns[1],
color=df.columns[-1],
title=f"{dataset.title()} Dataset"
)
gr.Interface(
fn=plot_figure,
inputs=gr.Dropdown(
choices=list(datasets.keys()),
value=default_dataset,
),
outputs="plot",
flagging_mode="never"
).launch()
You can read about supported Python requirement formats in the Reference section.
Installation via micropip
It is also possible to install Python packages via the micropip library at runtime. In the interactive example below, we install the faker package to generate mock data.
Code
# Read more about micropip at https://micropip.pyodide.org/en/v0.2.2/project/api.html#micropip.installimport micropipawait micropip.install("faker")import gradio as grfrom faker import Fakerimport pandas as pdfake = Faker()data_types = {"Name": fake.name, "Address": fake.address, "Email": fake.email, "Phone": fake.phone_number, "Job": fake.job}def generate_data(data_type, count): data = [{"#": i+1, "Data": data_types[data_type]()} for i inrange(count)]return pd.DataFrame(data)demo = gr.Interface( fn=generate_data, inputs=[gr.Dropdown(list(data_types.keys())), gr.Slider(1, 10, value=3, step=1)], outputs=gr.Dataframe(wrap=True),)demo.launch()
plotly==5.24.0
# Read more about micropip at https://micropip.pyodide.org/en/v0.2.2/project/api.html#micropip.install
import micropip
await micropip.install("faker")
import gradio as gr
from faker import Faker
import pandas as pd
fake = Faker()
data_types = {"Name": fake.name, "Address": fake.address, "Email": fake.email,
"Phone": fake.phone_number, "Job": fake.job}
def generate_data(data_type, count):
data = [{"#": i+1, "Data": data_types[data_type]()} for i in range(count)]
return pd.DataFrame(data)
demo = gr.Interface(
fn=generate_data,
inputs=[gr.Dropdown(list(data_types.keys())), gr.Slider(1, 10, value=3, step=1)],
outputs=gr.Dataframe(wrap=True),
)
demo.launch()
Limitations
Warning
Not every Python package is supported by Pyodide. While gradio and many other popular packages (including numpy, scikit-learn, and transformers-js) can be installed in Pyodide, if your app has many dependencies, its worth checking whether whether the dependencies are included in Pyodide, or can be installed with micropip.