@gradio/core 0.19.3 → 0.21.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/src/init.js CHANGED
@@ -26,6 +26,9 @@ export function create_components(initial_layout) {
26
26
  let app;
27
27
  let keys_per_render_id = {};
28
28
  let _rootNode;
29
+ // Store current layout and root for dynamic visibility recalculation
30
+ let current_layout;
31
+ let current_root;
29
32
  function set_event_specific_args(dependencies) {
30
33
  dependencies.forEach((dep) => {
31
34
  dep.targets.forEach((target) => {
@@ -42,6 +45,14 @@ export function create_components(initial_layout) {
42
45
  // make sure the state is settled before proceeding
43
46
  flush();
44
47
  app = _app;
48
+ if (instance_map) {
49
+ // re-render in reload mode
50
+ components.forEach((c) => {
51
+ if (c.props.value == null && c.id in instance_map) {
52
+ c.props.value = instance_map[c.id].props.value;
53
+ }
54
+ });
55
+ }
45
56
  _components = components;
46
57
  inputs = new Set();
47
58
  outputs = new Set();
@@ -49,6 +60,9 @@ export function create_components(initial_layout) {
49
60
  constructor_map = new Map();
50
61
  _component_map = new Map();
51
62
  instance_map = {};
63
+ // Store current layout and root for dynamic visibility recalculation
64
+ current_layout = layout;
65
+ current_root = root;
52
66
  _rootNode = {
53
67
  id: layout.id,
54
68
  type: "column",
@@ -67,7 +81,7 @@ export function create_components(initial_layout) {
67
81
  get_inputs_outputs(dep, inputs, outputs);
68
82
  });
69
83
  target_map.set(_target_map);
70
- constructor_map = preload_all_components(components, root);
84
+ constructor_map = preload_visible_components(components, layout, root);
71
85
  instance_map = components.reduce((acc, c) => {
72
86
  acc[c.id] = c;
73
87
  return acc;
@@ -80,6 +94,9 @@ export function create_components(initial_layout) {
80
94
  * Rerender the layout when the config has been modified to attach new components
81
95
  */
82
96
  function rerender_layout({ render_id, components, layout, root, dependencies }) {
97
+ // Update current layout and root for dynamic visibility recalculation
98
+ current_layout = layout;
99
+ current_root = root;
83
100
  components.forEach((c) => {
84
101
  for (const prop in c.props) {
85
102
  if (c.props[prop] === null) {
@@ -97,7 +114,7 @@ export function create_components(initial_layout) {
97
114
  replacement_components.push(c);
98
115
  }
99
116
  });
100
- let _constructor_map = preload_all_components(new_components, root);
117
+ let _constructor_map = preload_visible_components(new_components, layout, root);
101
118
  _constructor_map.forEach((v, k) => {
102
119
  constructor_map.set(k, v);
103
120
  });
@@ -159,9 +176,19 @@ export function create_components(initial_layout) {
159
176
  async function walk_layout(node, root, components, parent) {
160
177
  const instance = instance_map[node.id];
161
178
  if (!instance.component) {
162
- instance.component = (await constructor_map.get(instance.component_class_id || instance.type))?.default;
179
+ const constructor_key = instance.component_class_id || instance.type;
180
+ let component_constructor = constructor_map.get(constructor_key);
181
+ // Only load component if it was preloaded (i.e., it's visible)
182
+ if (component_constructor) {
183
+ instance.component = (await component_constructor)?.default;
184
+ }
185
+ // If component wasn't preloaded, leave it unloaded for now
186
+ // It will be loaded later when/if it becomes visible
163
187
  }
164
188
  instance.parent = parent;
189
+ // if (instance.type === "timer") {
190
+ // console.log("timer", instance, constructor_map);
191
+ // }
165
192
  if (instance.type === "dataset") {
166
193
  instance.props.component_map = get_component(instance.type, instance.component_class_id, root, components, instance.props.components).example_components;
167
194
  }
@@ -191,7 +218,7 @@ export function create_components(initial_layout) {
191
218
  instance.props.initial_tabs = child_tab_items?.map((child) => ({
192
219
  label: child.props.label,
193
220
  id: child.props.id,
194
- visible: child.props.visible,
221
+ visible: typeof child.props.visible === "boolean" ? child.props.visible : true,
195
222
  interactive: child.props.interactive,
196
223
  order: child.props.order
197
224
  }));
@@ -206,7 +233,63 @@ export function create_components(initial_layout) {
206
233
  }
207
234
  let update_scheduled = false;
208
235
  let update_scheduled_store = writable(false);
236
+ /**
237
+ * Load newly visible components after visibility changes
238
+ * @param newly_visible_ids Set of component IDs that are now visible
239
+ */
240
+ async function load_newly_visible_components(newly_visible_ids) {
241
+ if (newly_visible_ids.size === 0)
242
+ return;
243
+ const components_to_load = _components.filter((c) => newly_visible_ids.has(c.id));
244
+ for (const component of components_to_load) {
245
+ const constructor_key = component.component_class_id || component.type;
246
+ // Only load if not already loaded
247
+ if (!constructor_map.has(constructor_key)) {
248
+ const { component: loadable_component, example_components } = get_component(component.type, component.component_class_id, current_root, _components);
249
+ constructor_map.set(constructor_key, loadable_component);
250
+ if (example_components) {
251
+ for (const [name, example_component] of example_components) {
252
+ constructor_map.set(name, example_component);
253
+ }
254
+ }
255
+ // Load the component if it doesn't exist yet
256
+ if (!component.component) {
257
+ component.component = (await loadable_component)?.default;
258
+ }
259
+ }
260
+ else {
261
+ component.component =
262
+ (await constructor_map.get(constructor_key))?.default ??
263
+ component.component;
264
+ }
265
+ }
266
+ }
267
+ /**
268
+ * Check if any visibility-affecting properties have changed
269
+ * @param updates Array of update transactions
270
+ * @returns True if visibility might have changed
271
+ */
272
+ function has_visibility_changes(updates) {
273
+ return updates.some((update_batch) => update_batch.some((update) => {
274
+ const instance = instance_map[update.id];
275
+ if (!instance)
276
+ return false;
277
+ // Check for visibility property changes
278
+ if (update.prop === "visible")
279
+ return true;
280
+ // Check for selected tab changes in tabs components
281
+ if (update.prop === "selected" && instance.type === "tabs")
282
+ return true;
283
+ return false;
284
+ }));
285
+ }
209
286
  function flush() {
287
+ const had_visibility_changes = has_visibility_changes(pending_updates);
288
+ let previous_visible_ids;
289
+ // Capture current visibility state before applying updates
290
+ if (had_visibility_changes && current_layout) {
291
+ previous_visible_ids = determine_visible_components(current_layout, _components);
292
+ }
210
293
  layout_store.update((layout) => {
211
294
  for (let i = 0; i < pending_updates.length; i++) {
212
295
  for (let j = 0; j < pending_updates[i].length; j++) {
@@ -234,6 +317,25 @@ export function create_components(initial_layout) {
234
317
  }
235
318
  return layout;
236
319
  });
320
+ // After applying updates, check if we need to load new components
321
+ if (had_visibility_changes && current_layout && previous_visible_ids) {
322
+ raf(async () => {
323
+ const new_visible_ids = determine_visible_components(current_layout, _components);
324
+ const newly_visible_ids = new Set();
325
+ // Find components that are now visible but weren't before
326
+ for (const id of new_visible_ids) {
327
+ if (!previous_visible_ids.has(id)) {
328
+ newly_visible_ids.add(id);
329
+ }
330
+ }
331
+ // Load the newly visible components
332
+ await load_newly_visible_components(newly_visible_ids);
333
+ // Trigger a layout update to render the newly loaded components
334
+ if (newly_visible_ids.size > 0) {
335
+ layout_store.update((layout) => layout);
336
+ }
337
+ });
338
+ }
237
339
  pending_updates = [];
238
340
  update_scheduled = false;
239
341
  update_scheduled_store.set(false);
@@ -467,7 +569,141 @@ export function get_component(type, class_id, root, components, example_componen
467
569
  };
468
570
  }
469
571
  /**
470
- * Preload all components
572
+ * Check if a tab item should be visible based on selection state
573
+ * @param component The tab item component
574
+ * @param component_visible Whether the component is visible
575
+ * @param parent_tabs_context Tab context from parent
576
+ * @returns Whether the tab item should be visible
577
+ */
578
+ function is_tab_item_visible(component, component_visible, parent_tabs_context) {
579
+ const is_selected_tab = parent_tabs_context?.selected_tab_id === component.id ||
580
+ parent_tabs_context?.selected_tab_id === component.props.id;
581
+ return component_visible && is_selected_tab;
582
+ }
583
+ /**
584
+ * Determine the selected tab ID for a tabs component
585
+ * @param component The tabs component
586
+ * @param layout The layout node
587
+ * @param components All components
588
+ * @returns The selected tab ID
589
+ */
590
+ function get_selected_tab_id(component, layout, components) {
591
+ // Check if selected prop is a string or number
592
+ const selected = component.props.selected;
593
+ if (typeof selected === "string" || typeof selected === "number") {
594
+ return selected;
595
+ }
596
+ // If no tab is explicitly selected, find the first visible and interactive tab
597
+ if (layout.children) {
598
+ for (const child of layout.children) {
599
+ const child_component = components.find((c) => c.id === child.id);
600
+ if (child_component?.type === "tabitem" &&
601
+ child_component.props.visible !== false &&
602
+ child_component.props.interactive !== false) {
603
+ return (child_component.id || child_component.props.id);
604
+ }
605
+ }
606
+ }
607
+ return undefined;
608
+ }
609
+ /**
610
+ * Process children components for visibility
611
+ * @param layout The layout node
612
+ * @param components All components
613
+ * @param parent_tabs_context Tab context
614
+ * @returns Set of visible child component IDs
615
+ */
616
+ function process_children_visibility(layout, components, parent_tabs_context) {
617
+ const visible_components = new Set();
618
+ if (layout.children) {
619
+ for (const child of layout.children) {
620
+ const child_visible = determine_visible_components(child, components, true, parent_tabs_context);
621
+ child_visible.forEach((id) => visible_components.add(id));
622
+ }
623
+ }
624
+ return visible_components;
625
+ }
626
+ /**
627
+ * Determine which components should be visible based on layout structure and visibility rules
628
+ * @param layout The layout tree
629
+ * @param components All component metadata
630
+ * @param parent_visible Whether the parent component is visible
631
+ * @param parent_tabs_context Information about parent tabs if any
632
+ * @returns Set of component IDs that should be visible
633
+ */
634
+ function determine_visible_components(layout, components, parent_visible = true, parent_tabs_context) {
635
+ const visible_components = new Set();
636
+ const component = components.find((c) => c.id === layout.id);
637
+ if (!component) {
638
+ return visible_components;
639
+ }
640
+ // Check if the component itself is visible
641
+ const component_visible = parent_visible &&
642
+ (typeof component.props.visible === "boolean"
643
+ ? component.props.visible
644
+ : true);
645
+ // Handle tab_item special case
646
+ if (component.type === "tabitem") {
647
+ if (is_tab_item_visible(component, component_visible, parent_tabs_context)) {
648
+ visible_components.add(component.id);
649
+ // Process children if this tab item is visible
650
+ const child_visible = process_children_visibility(layout, components, parent_tabs_context);
651
+ child_visible.forEach((id) => visible_components.add(id));
652
+ }
653
+ // If tab item is not visible, none of its children should be loaded
654
+ return visible_components;
655
+ }
656
+ // Handle tabs component
657
+ if (component.type === "tabs") {
658
+ if (component_visible) {
659
+ visible_components.add(component.id);
660
+ // Determine which tab should be selected
661
+ const selected_tab_id = get_selected_tab_id(component, layout, components);
662
+ // Process children with tabs context
663
+ const child_visible = process_children_visibility(layout, components, {
664
+ selected_tab_id
665
+ });
666
+ child_visible.forEach((id) => visible_components.add(id));
667
+ }
668
+ return visible_components;
669
+ }
670
+ // For regular components
671
+ if (component_visible) {
672
+ visible_components.add(component.id);
673
+ // Process children if this component is visible
674
+ const child_visible = process_children_visibility(layout, components, parent_tabs_context);
675
+ child_visible.forEach((id) => visible_components.add(id));
676
+ }
677
+ // If component is not visible, don't process children
678
+ return visible_components;
679
+ }
680
+ /**
681
+ * Preload only visible components
682
+ * @param components A list of component metadata
683
+ * @param layout The layout tree to determine visibility
684
+ * @param root The root url of the app
685
+ * @returns A map of component ids to their constructors
686
+ */
687
+ export function preload_visible_components(components, layout, root) {
688
+ let constructor_map = new Map();
689
+ // Determine which components should be visible
690
+ const visible_component_ids = determine_visible_components(layout, components);
691
+ // Only preload visible components
692
+ components.forEach((c) => {
693
+ if (visible_component_ids.has(c.id)) {
694
+ const { component, example_components } = get_component(c.type, c.component_class_id, root, components);
695
+ constructor_map.set(c.component_class_id || c.type, component);
696
+ if (example_components) {
697
+ for (const [name, example_component] of example_components) {
698
+ constructor_map.set(name, example_component);
699
+ }
700
+ }
701
+ }
702
+ });
703
+ return constructor_map;
704
+ }
705
+ /**
706
+ * Preload all components (legacy function, kept for backwards compatibility)
471
707
  * @param components A list of component metadata
472
708
  * @param root The root url of the app
473
709
  * @returns A map of component ids to their constructors
@@ -89,7 +89,9 @@
89
89
  "sort_ascending": "Sort ascending",
90
90
  "sort_descending": "Sort descending",
91
91
  "drop_to_upload": "Drop CSV or TSV files here to import data into dataframe",
92
- "clear_sort": "Clear sort"
92
+ "clear_sort": "Clear sort",
93
+ "filter": "Filter",
94
+ "clear_filter": "Clear filters"
93
95
  },
94
96
  "dropdown": {
95
97
  "dropdown": "Dropdown"
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,23 @@
1
+ //@ts-nocheck
2
+ import fs from "fs";
3
+ import path from "path";
4
+ import { fileURLToPath } from "url";
5
+
6
+ const __filename = fileURLToPath(import.meta.url);
7
+ const __dirname = path.dirname(__filename);
8
+
9
+ const langs = fs.readdirSync(path.join(__dirname, "..", "lang"));
10
+
11
+ let lang_names = {};
12
+ for (const lang of langs) {
13
+ if (lang.endsWith(".json")) {
14
+ const lang_text = fs.readFileSync(
15
+ path.join(__dirname, "..", "lang", lang),
16
+ "utf8"
17
+ );
18
+ const lang_data = JSON.parse(lang_text.trim());
19
+ lang_names[lang.split(".")[0]] = lang_data._name;
20
+ }
21
+ }
22
+
23
+ console.log(lang_names);
@@ -85,7 +85,9 @@
85
85
  "sort_ascending": "Ordenar em ordem crescente",
86
86
  "sort_descending": "Ordenar em ordem decrescente",
87
87
  "drop_to_upload": "Solte arquivos CSV ou TSV aqui para importar dados para o dataframe",
88
- "clear_sort": "Limpar classificação"
88
+ "clear_sort": "Limpar classificação",
89
+ "filter": "Filtrar",
90
+ "clear_filter": "Limpar filtros"
89
91
  },
90
92
  "dropdown": {
91
93
  "dropdown": "Menu suspenso"
@@ -74,7 +74,9 @@
74
74
  "sort_ascending": "Ordenar por ordem crescente",
75
75
  "sort_descending": "Ordenar por ordem decrescente",
76
76
  "drop_to_upload": "Solte ficheiros CSV ou TSV aqui para importar dados para o dataframe",
77
- "clear_sort": "Limpar ordenação"
77
+ "clear_sort": "Limpar ordenação",
78
+ "filter": "Filtrar",
79
+ "clear_filter": "Limpar filtros"
78
80
  },
79
81
  "dropdown": {
80
82
  "dropdown": "Lista suspensa"
package/package.json CHANGED
@@ -1,67 +1,67 @@
1
1
  {
2
2
  "name": "@gradio/core",
3
- "version": "0.19.3",
3
+ "version": "0.21.0",
4
4
  "type": "module",
5
5
  "devDependencies": {
6
- "@gradio/accordion": "^0.5.17",
7
- "@gradio/annotatedimage": "^0.9.22",
8
- "@gradio/atoms": "^0.16.2",
9
- "@gradio/audio": "^0.17.18",
10
- "@gradio/chatbot": "^0.26.15",
11
- "@gradio/box": "^0.2.20",
12
- "@gradio/button": "^0.5.4",
13
- "@gradio/checkbox": "^0.4.24",
14
- "@gradio/checkboxgroup": "^0.6.23",
15
- "@gradio/client": "^1.15.3",
16
- "@gradio/code": "^0.14.8",
17
- "@gradio/colorpicker": "^0.4.23",
18
- "@gradio/column": "^0.2.0",
19
- "@gradio/dataset": "^0.4.24",
20
- "@gradio/dataframe": "^0.17.17",
21
- "@gradio/datetime": "^0.3.16",
22
- "@gradio/downloadbutton": "^0.4.4",
23
- "@gradio/dropdown": "^0.9.23",
24
- "@gradio/fallback": "^0.4.23",
25
- "@gradio/file": "^0.12.21",
26
- "@gradio/form": "^0.2.20",
27
- "@gradio/fileexplorer": "^0.5.32",
6
+ "@gradio/accordion": "^0.5.19",
7
+ "@gradio/atoms": "^0.16.3",
8
+ "@gradio/audio": "^0.17.20",
9
+ "@gradio/box": "^0.2.21",
10
+ "@gradio/annotatedimage": "^0.9.24",
11
+ "@gradio/button": "^0.5.6",
12
+ "@gradio/chatbot": "^0.26.17",
13
+ "@gradio/checkbox": "^0.4.25",
14
+ "@gradio/checkboxgroup": "^0.6.24",
15
+ "@gradio/client": "^1.15.5",
16
+ "@gradio/colorpicker": "^0.4.24",
17
+ "@gradio/code": "^0.14.10",
18
+ "@gradio/column": "^0.2.1",
19
+ "@gradio/dataframe": "^0.18.1",
20
+ "@gradio/dataset": "^0.4.26",
21
+ "@gradio/datetime": "^0.3.17",
22
+ "@gradio/downloadbutton": "^0.4.6",
23
+ "@gradio/dropdown": "^0.9.24",
24
+ "@gradio/fallback": "^0.4.24",
25
+ "@gradio/file": "^0.12.23",
26
+ "@gradio/fileexplorer": "^0.5.34",
27
+ "@gradio/gallery": "^0.15.26",
28
+ "@gradio/form": "^0.2.21",
28
29
  "@gradio/group": "^0.2.0",
29
- "@gradio/gallery": "^0.15.24",
30
- "@gradio/highlightedtext": "^0.9.6",
31
- "@gradio/html": "^0.6.15",
32
30
  "@gradio/icons": "^0.12.0",
33
- "@gradio/image": "^0.22.10",
34
- "@gradio/imageeditor": "^0.15.4",
35
- "@gradio/imageslider": "^0.2.6",
36
- "@gradio/json": "^0.5.25",
37
- "@gradio/label": "^0.5.15",
31
+ "@gradio/highlightedtext": "^0.9.7",
32
+ "@gradio/html": "^0.6.16",
33
+ "@gradio/image": "^0.22.12",
34
+ "@gradio/imageeditor": "^0.16.0",
35
+ "@gradio/imageslider": "^0.2.8",
36
+ "@gradio/label": "^0.5.16",
37
+ "@gradio/json": "^0.5.26",
38
+ "@gradio/markdown": "^0.13.17",
39
+ "@gradio/model3d": "^0.14.19",
38
40
  "@gradio/browserstate": "^0.3.2",
39
- "@gradio/markdown": "^0.13.16",
40
- "@gradio/model3d": "^0.14.17",
41
- "@gradio/multimodaltextbox": "^0.10.10",
42
- "@gradio/nativeplot": "^0.6.2",
43
- "@gradio/number": "^0.5.23",
44
- "@gradio/plot": "^0.9.18",
45
- "@gradio/radio": "^0.7.6",
46
- "@gradio/paramviewer": "^0.7.11",
47
- "@gradio/sidebar": "^0.1.15",
48
- "@gradio/simpledropdown": "^0.3.23",
41
+ "@gradio/multimodaltextbox": "^0.10.12",
42
+ "@gradio/nativeplot": "^0.7.1",
43
+ "@gradio/number": "^0.6.1",
44
+ "@gradio/paramviewer": "^0.7.12",
45
+ "@gradio/plot": "^0.9.19",
49
46
  "@gradio/row": "^0.2.1",
50
- "@gradio/simpleimage": "^0.8.32",
51
- "@gradio/sketchbox": "^0.6.10",
52
- "@gradio/simpletextbox": "^0.3.24",
47
+ "@gradio/radio": "^0.7.7",
48
+ "@gradio/sidebar": "^0.1.17",
49
+ "@gradio/simpleimage": "^0.8.34",
50
+ "@gradio/simpledropdown": "^0.3.24",
51
+ "@gradio/simpletextbox": "^0.3.25",
52
+ "@gradio/sketchbox": "^0.6.12",
53
+ "@gradio/slider": "^0.6.13",
54
+ "@gradio/tabitem": "^0.5.0",
53
55
  "@gradio/state": "^0.1.2",
54
- "@gradio/slider": "^0.6.12",
55
- "@gradio/tabitem": "^0.4.5",
56
+ "@gradio/textbox": "^0.10.16",
56
57
  "@gradio/tabs": "^0.4.5",
57
- "@gradio/textbox": "^0.10.15",
58
58
  "@gradio/theme": "^0.4.0",
59
59
  "@gradio/timer": "^0.4.5",
60
- "@gradio/statustracker": "^0.10.13",
61
- "@gradio/uploadbutton": "^0.9.4",
60
+ "@gradio/upload": "^0.16.10",
61
+ "@gradio/uploadbutton": "^0.9.6",
62
+ "@gradio/statustracker": "^0.10.14",
62
63
  "@gradio/utils": "^0.10.2",
63
- "@gradio/upload": "^0.16.8",
64
- "@gradio/video": "^0.14.18",
64
+ "@gradio/video": "^0.14.20",
65
65
  "@gradio/wasm": "^0.18.1"
66
66
  },
67
67
  "msw": {