Skip to content

Build the Gradio wheel for Lite before packaging #11157

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/fair-olives-appear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@gradio/lite": minor
"gradio": minor
---

feat:Build the Gradio wheel for Lite before packaging
8 changes: 8 additions & 0 deletions .config/pycompile-lite-wheel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import subprocess

from hatchling.builders.hooks.plugin.interface import BuildHookInterface


class BuildHook(BuildHookInterface):
def finalize(self, version, build_data, artifact_path):
subprocess.run(["pyodide", "py-compile", "--keep", artifact_path], check=True)
4 changes: 3 additions & 1 deletion gradio/blocks_events.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

from gradio import wasm_utils
from gradio.component_meta import create_or_modify_pyi
from gradio.events import EventListener, Events

Expand All @@ -17,7 +18,8 @@ def __new__(cls, name, bases, attrs):
trigger.set_doc(component=name)
attrs[event] = trigger.listener
component_class = super().__new__(cls, name, bases, attrs)
create_or_modify_pyi(BlocksEvents, "BlocksEvents", BLOCKS_EVENTS)
if not wasm_utils.IS_WASM:
create_or_modify_pyi(BlocksEvents, "BlocksEvents", BLOCKS_EVENTS)
return component_class


Expand Down
4 changes: 3 additions & 1 deletion gradio/component_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from jinja2 import Template

from gradio import wasm_utils
from gradio.events import EventListener
from gradio.exceptions import ComponentDefinitionError
from gradio.utils import no_raise_exception
Expand Down Expand Up @@ -218,5 +219,6 @@ def __new__(cls, name, bases, attrs):
if "EVENTS" in attrs:
attrs["EVENTS"] = new_events
component_class = super().__new__(cls, name, bases, attrs)
create_or_modify_pyi(component_class, name, events)
if not wasm_utils.IS_WASM:
create_or_modify_pyi(component_class, name, events)
return component_class
11 changes: 6 additions & 5 deletions js/lite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
],
"scripts": {
"cssbuild": "python ../../scripts/generate_theme.py --outfile ./src/theme.css",
"pybuild:gradio": "cd ../../ && hatch build -t lite",
"pybuild:gradio": "cd ../../ && hatch build -t lite && pyodide py-compile --keep dist-lite/gradio-*-py3-none-any.whl",
"pybuild:gradio-client": "cd ../../client/python && python -m build",
"pybuild": "run-p pybuild:*",
"build": "pnpm pybuild && pnpm cssbuild && pnpm --filter @gradio/client build && pnpm --filter @gradio/wasm build && vite build --mode production",
Expand All @@ -23,12 +23,13 @@
"test:browser:dev": "GRADIO_E2E_TEST_LITE=1 pnpm --filter @self/spa test:browser:dev"
},
"devDependencies": {
"gradio": "workspace:^",
"@gradio/atoms": "workspace:^",
"@self/build": "workspace:^",
"@gradio/core": "workspace:^",
"@self/spa": "workspace:^",
"@gradio/theme": "workspace:^",
"@gradio/wasm": "workspace:^"
"@gradio/wasm": "workspace:^",
"@self/build": "workspace:^",
"@self/spa": "workspace:^",
"glob": "^11.0.2",
"gradio": "workspace:^"
}
}
17 changes: 13 additions & 4 deletions js/lite/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import global_data from "@csstools/postcss-global-data";
import prefixer from "postcss-prefix-selector";
import { readFileSync } from "fs";
import { resolve } from "path";
import { glob } from "glob";

const version_path = resolve(__dirname, "../../gradio/package.json");
const theme_token_path = resolve(__dirname, "../theme/src/tokens.css");
Expand Down Expand Up @@ -45,6 +46,17 @@ const client_version_raw = JSON.parse(

const client_python_version = convert_to_pypi_prerelease(client_version_raw);

const gradio_wheel_path = (() => {
const wheelPattern = `../../dist-lite/gradio-${python_version}-cp3*-none-any.whl`;
const matches = glob.sync(wheelPattern, { cwd: __dirname });

if (matches.length === 0) {
throw new Error(`No wheel file found matching pattern: ${wheelPattern}`);
}

return resolve(__dirname, matches[0]);
})();

import {
inject_ejs,
generate_cdn_entry,
Expand Down Expand Up @@ -186,10 +198,7 @@ export default defineConfig(({ mode }) => {
conditions: ["gradio"],
alias: {
// For the Wasm app to import the wheel file URLs.
"gradio.whl": resolve(
__dirname,
`../../dist-lite/gradio-${python_version}-py3-none-any.whl`
),
"gradio.whl": gradio_wheel_path,
"gradio_client.whl": resolve(
__dirname,
`../../client/python/dist/gradio_client-${client_python_version}-py3-none-any.whl`
Expand Down
82 changes: 46 additions & 36 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ artifacts = ["!gradio/templates", "!*.pyi", "!/gradio/test_data", "!/gradio/_fro
directory = "dist-lite"
dependencies = ["lite-builder @ {root:uri}/../.config/lite-builder"]

[tool.hatch.build.targets.lite.hooks.custom]
path = ".config/pycompile-lite-wheel.py"

[tool.hatch.build.targets.wheel.hooks.custom]
path = ".config/copy_frontend.py"

Expand Down
4 changes: 3 additions & 1 deletion scripts/run_lite.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ source scripts/helpers.sh
pnpm_required
jq_required

pip install pyodide-build

GRADIO_VERSION=$(jq -r .version ${ROOTDIR}/gradio/package.json)
GRADIO_CLIENT_VERSION=$(jq -r .version ${ROOTDIR}/client/python/gradio_client/package.json)
GRADIO_WHEEL_PATH="${ROOTDIR}/dist-lite/gradio-${GRADIO_VERSION}-py3-none-any.whl"
GRADIO_WHEEL_PATH="${ROOTDIR}/dist-lite/gradio-${GRADIO_VERSION}-cp312-none-any.whl"
GRADIO_CLIENT_FILE_PATH="${ROOTDIR}/client/python/dist/gradio_client-${GRADIO_CLIENT_VERSION}-py3-none-any.whl"

echo "Checking for gradio and gradio_client wheel files..."
Expand Down
Loading