General functions for handling events in Gradio Svelte components. This utility library provides essential infrastructure for the Gradio ecosystem, including event data structures, file sharing capabilities, component management, and common utility functions that enable consistent behavior across all Gradio UI components.
npm install @gradio/utilsimport {
get_next_color,
uploadToHuggingFace,
copy,
format_time,
css_units,
Gradio,
ShareError,
type ValueData,
type SelectData,
type LikeData,
type KeyUpData,
type ShareData,
type CopyData,
type I18nFormatter
} from "@gradio/utils";For CommonJS:
const {
get_next_color,
uploadToHuggingFace,
copy,
format_time,
css_units,
Gradio,
ShareError
} = require("@gradio/utils");import {
get_next_color,
uploadToHuggingFace,
copy,
format_time,
Gradio,
type SelectData
} from "@gradio/utils";
// Color cycling for UI components
const color1 = get_next_color(0); // First color in theme
const color2 = get_next_color(1); // Second color in theme
// Time formatting
const formatted = format_time(3665); // "1:01:05"
// Component management
const gradio = new Gradio(
1, // component id
document.body, // container element
"light", // theme
"1.0.0", // version
"/", // root path
true, // autoscroll
null, // max file size
(x) => x, // i18n formatter
client // Gradio client instance
);
// Event dispatching
gradio.dispatch("select", { index: 0, value: "item1" });
// Copy-to-clipboard action (Svelte)
<div use:copy>
<button class="copy_code_button">Copy</button>
<div>Content to copy</div>
</div>@gradio/utils is organized around several key functional areas:
Standardized TypeScript interfaces for all component event types in the Gradio ecosystem. Essential for type-safe event handling.
interface ValueData {
value: any;
is_value_data: boolean;
}
interface SelectData {
row_value?: any[];
col_value?: any[];
index: number | [number, number];
value: any;
selected?: boolean;
}
interface LikeData {
index: number | [number, number];
value: any;
liked?: boolean | string;
}
interface KeyUpData {
key: string;
input_value: string;
}
interface ShareData {
description: string;
title?: string;
}
interface CopyData {
value: string;
}Upload content to HuggingFace Spaces with support for base64 data and URL sources. Includes custom error handling for sharing operations.
function uploadToHuggingFace(
data: string | { url?: string; path?: string },
type: "base64" | "url"
): Promise<string>;
class ShareError extends Error {
constructor(message: string);
name: "ShareError";
}Core Gradio class for managing component instances, handling initialization, event dispatching, and component loading.
class Gradio<T extends Record<string, any> = Record<string, any>> {
constructor(
id: number,
el: HTMLElement,
theme: string,
version: string,
root: string,
autoscroll: boolean,
max_file_size: number | null,
i18n?: I18nFormatter,
client: Client,
virtual_component_loader?: component_loader
);
dispatch<E extends keyof T>(event_name: E, data?: T[E]): void;
load_component(name: string, variant?: "component" | "example" | "base"): ReturnType<component_loader>;
}
type I18nFormatter = any;Common utility functions for color cycling, time formatting, CSS units, and clipboard operations.
function get_next_color(index: number): keyof typeof colors;
function format_time(seconds: number): string;
function css_units(dimension_value: string | number): string;
function copy(node: HTMLDivElement): ActionReturn;The package includes a custom ShareError class for handling sharing operation failures:
try {
await uploadToHuggingFace(data, "base64");
} catch (error) {
if (error instanceof ShareError) {
console.error("Sharing failed:", error.message);
}
}