Skip to content

Commit c7ce55b

Browse files
Lazy load the UI when trackio is imported (#431)
Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
1 parent 0c7b4ea commit c7ce55b

2 files changed

Lines changed: 16 additions & 1 deletion

File tree

.changeset/polite-pugs-kick.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"trackio": patch
3+
---
4+
5+
feat:Lazy load the UI when trackio is imported

trackio/__init__.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
from trackio.sqlite_storage import SQLiteStorage
2626
from trackio.table import Table
2727
from trackio.typehints import UploadEntry
28-
from trackio.ui.main import CSS, HEAD, demo
2928
from trackio.utils import TRACKIO_DIR, TRACKIO_LOGO_DIR
3029

3130
logging.getLogger("httpx").setLevel(logging.WARNING)
@@ -69,6 +68,14 @@
6968
config = {}
7069

7170

71+
def _get_demo():
72+
# Lazy import to avoid initializing Gradio Blocks (and FastAPI) at import time,
73+
# which causes import lock errors for libraries that just `import trackio`.
74+
from trackio.ui.main import CSS, HEAD, demo
75+
76+
return demo, CSS, HEAD
77+
78+
7279
def init(
7380
project: str,
7481
name: str | None = None,
@@ -159,6 +166,7 @@ def init(
159166
raise LocalTokenNotFoundError(
160167
f"You must be logged in to Hugging Face locally when `space_id` is provided to deploy to a Space. {e}"
161168
) from e
169+
demo, CSS, HEAD = _get_demo()
162170
url = context_vars.current_server.get()
163171
share_url = context_vars.current_share_server.get()
164172

@@ -518,6 +526,8 @@ def show(
518526
`share_url`: The public share URL of the dashboard.
519527
`full_url`: The full URL of the dashboard including the write token (will use the public share URL if launched publicly, otherwise the local URL).
520528
"""
529+
demo, CSS, HEAD = _get_demo()
530+
521531
if color_palette is not None:
522532
os.environ["TRACKIO_COLOR_PALETTE"] = ",".join(color_palette)
523533

0 commit comments

Comments
 (0)