gradio-pr-bot commited on
Commit
939d07b
·
verified ·
1 Parent(s): 09817e8

Upload folder using huggingface_hub

Browse files
6.23.1/plot/Index.svelte ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script module lang="ts">
2
+ export { default as BasePlot } from "./shared/Plot.svelte";
3
+ </script>
4
+
5
+ <script lang="ts">
6
+ import { Gradio } from "@gradio/utils";
7
+ import Plot from "./shared/Plot.svelte";
8
+
9
+ import {
10
+ Block,
11
+ BlockLabel,
12
+ FullscreenButton,
13
+ IconButtonWrapper
14
+ } from "@gradio/atoms";
15
+ import { Plot as PlotIcon } from "@gradio/icons";
16
+
17
+ import { StatusTracker } from "@gradio/statustracker";
18
+ import type { PlotProps, PlotEvents } from "./types.ts";
19
+
20
+ let props = $props();
21
+ const gradio = new Gradio<PlotEvents, PlotProps>(props);
22
+
23
+ let fullscreen = $state(false);
24
+ </script>
25
+
26
+ <Block
27
+ padding={false}
28
+ elem_id={gradio.shared.elem_id}
29
+ elem_classes={gradio.shared.elem_classes}
30
+ visible={gradio.shared.visible}
31
+ container={gradio.shared.container}
32
+ scale={gradio.shared.scale}
33
+ min_width={gradio.shared.min_width}
34
+ allow_overflow={false}
35
+ bind:fullscreen
36
+ >
37
+ <BlockLabel
38
+ show_label={gradio.shared.show_label}
39
+ label={gradio.shared.label || gradio.i18n("plot.plot")}
40
+ Icon={PlotIcon}
41
+ />
42
+ {#if (gradio.props.buttons && gradio.props.buttons.length > 0) || gradio.props.show_fullscreen_button}
43
+ <IconButtonWrapper
44
+ buttons={gradio.props.buttons ?? []}
45
+ on_custom_button_click={(id) => {
46
+ gradio.dispatch("custom_button_click", { id });
47
+ }}
48
+ >
49
+ {#if gradio.props.show_fullscreen_button}
50
+ <FullscreenButton
51
+ {fullscreen}
52
+ onclick={(value) => {
53
+ fullscreen = value;
54
+ }}
55
+ />
56
+ {/if}
57
+ </IconButtonWrapper>
58
+ {/if}
59
+ <StatusTracker
60
+ autoscroll={gradio.shared.autoscroll}
61
+ i18n={gradio.i18n}
62
+ {...gradio.shared.loading_status}
63
+ on_clear_status={() =>
64
+ gradio.dispatch("clear_status", gradio.shared.loading_status)}
65
+ />
66
+ <Plot
67
+ value={gradio.props.value}
68
+ theme_mode={gradio.shared.theme_mode}
69
+ show_label={gradio.shared.show_label}
70
+ caption={gradio.props.caption}
71
+ bokeh_version={gradio.props.bokeh_version}
72
+ show_actions_button={gradio.props.show_actions_button}
73
+ _selectable={gradio.props._selectable}
74
+ x_lim={gradio.props.x_lim}
75
+ show_fullscreen_button={gradio.props.show_fullscreen_button}
76
+ on_change={() => gradio.dispatch("change")}
77
+ onselect={(data) => gradio.dispatch("select", data)}
78
+ />
79
+ </Block>
6.23.1/plot/package.json ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "@gradio/plot",
3
+ "version": "0.12.2",
4
+ "description": "Gradio UI packages",
5
+ "type": "module",
6
+ "author": "",
7
+ "license": "ISC",
8
+ "private": false,
9
+ "dependencies": {
10
+ "@gradio/atoms": "workspace:^",
11
+ "@gradio/icons": "workspace:^",
12
+ "@gradio/statustracker": "workspace:^",
13
+ "@gradio/theme": "workspace:^",
14
+ "@gradio/utils": "workspace:^",
15
+ "@rollup/plugin-json": "^6.1.0",
16
+ "plotly.js-dist-min": "^3.1.1",
17
+ "vega": "^5.23.0",
18
+ "vega-embed": "^6.25.0",
19
+ "vega-lite": "^5.12.0"
20
+ },
21
+ "devDependencies": {
22
+ "@gradio/preview": "workspace:^"
23
+ },
24
+ "main": "./Index.svelte",
25
+ "main_changeset": true,
26
+ "exports": {
27
+ "./package.json": "./package.json",
28
+ ".": {
29
+ "gradio": "./Index.svelte",
30
+ "svelte": "./dist/Index.svelte",
31
+ "types": "./dist/Index.svelte.d.ts"
32
+ },
33
+ "./base": {
34
+ "gradio": "./shared/Plot.svelte",
35
+ "svelte": "./dist/shared/Plot.svelte",
36
+ "types": "./dist/shared/Plot.svelte.d.ts"
37
+ }
38
+ },
39
+ "peerDependencies": {
40
+ "svelte": "^5.48.0"
41
+ },
42
+ "repository": {
43
+ "type": "git",
44
+ "url": "git+https://github.com/gradio-app/gradio.git",
45
+ "directory": "js/plot"
46
+ }
47
+ }
6.23.1/plot/shared/Plot.svelte ADDED
@@ -0,0 +1,101 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ //@ts-nocheck
3
+ import { Plot as PlotIcon } from "@gradio/icons";
4
+ import { Empty } from "@gradio/atoms";
5
+ import type { SelectData } from "@gradio/utils";
6
+ import type { ThemeMode } from "../types";
7
+ import { untrack } from "svelte";
8
+
9
+ let {
10
+ value,
11
+ theme_mode,
12
+ caption,
13
+ bokeh_version,
14
+ show_actions_button,
15
+ _selectable,
16
+ x_lim,
17
+ show_fullscreen_button,
18
+ show_label,
19
+ on_change,
20
+ onselect
21
+ }: {
22
+ value: null | string;
23
+ theme_mode: ThemeMode;
24
+ caption: string;
25
+ bokeh_version: string | null;
26
+ show_actions_button: boolean;
27
+ _selectable: boolean;
28
+ x_lim: [number, number] | null;
29
+ show_fullscreen_button: boolean;
30
+ show_label: boolean;
31
+ on_change: () => void;
32
+ onselect?: (data: SelectData) => void;
33
+ } = $props();
34
+
35
+ let PlotComponent: any = $state(null);
36
+ let loaded_plotly_css = $state(false);
37
+ let key = $state(0);
38
+
39
+ const plotTypeMapping = {
40
+ plotly: () => import("./plot_types/PlotlyPlot.svelte"),
41
+ bokeh: () => import("./plot_types/BokehPlot.svelte"),
42
+ matplotlib: () => import("./plot_types/MatplotlibPlot.svelte"),
43
+ altair: () => import("./plot_types/AltairPlot.svelte")
44
+ };
45
+
46
+ let loadedPlotTypeMapping: Record<string, any> = {};
47
+
48
+ const is_browser = typeof window !== "undefined";
49
+ let _type = $state(null);
50
+ let _plot = $state(null);
51
+ let _chart = $state(null);
52
+
53
+ $effect(() => {
54
+ let type = value?.type;
55
+ let plot = value?.plot;
56
+ let chart = value?.chart;
57
+ untrack(() => {
58
+ // Remount only on a payload change; the prop's identity churns (#10107).
59
+ if (type !== _type || plot !== _plot || chart !== _chart) {
60
+ key = key + 1;
61
+ }
62
+ if (type !== _type) {
63
+ PlotComponent = null;
64
+ }
65
+ if (type && type in plotTypeMapping && is_browser) {
66
+ if (loadedPlotTypeMapping[type]) {
67
+ PlotComponent = loadedPlotTypeMapping[type];
68
+ } else {
69
+ plotTypeMapping[type]().then((module) => {
70
+ PlotComponent = module.default;
71
+ loadedPlotTypeMapping[type] = PlotComponent;
72
+ });
73
+ }
74
+ }
75
+ _type = type;
76
+ _plot = plot;
77
+ _chart = chart;
78
+ });
79
+ on_change();
80
+ });
81
+ </script>
82
+
83
+ {#if value && PlotComponent}
84
+ {#key key}
85
+ <PlotComponent
86
+ {value}
87
+ colors={[]}
88
+ {theme_mode}
89
+ {show_label}
90
+ {caption}
91
+ {bokeh_version}
92
+ {show_actions_button}
93
+ {_selectable}
94
+ {x_lim}
95
+ bind:loaded_plotly_css
96
+ {onselect}
97
+ />
98
+ {/key}
99
+ {:else}
100
+ <Empty unpadded_box={true} size="large"><PlotIcon /></Empty>
101
+ {/if}
6.23.1/plot/shared/plot_types/AltairPlot.svelte ADDED
@@ -0,0 +1,197 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ //@ts-nocheck
3
+ import { set_config } from "./altair_utils";
4
+ import { untrack } from "svelte";
5
+ import type { TopLevelSpec as Spec } from "vega-lite";
6
+ import vegaEmbed from "vega-embed";
7
+ import type { SelectData } from "@gradio/utils";
8
+ import type { View } from "vega";
9
+
10
+ let {
11
+ value,
12
+ colors = [],
13
+ caption,
14
+ show_actions_button,
15
+ _selectable,
16
+ onselect
17
+ }: {
18
+ value: any;
19
+ colors?: string[];
20
+ caption: string;
21
+ show_actions_button: boolean;
22
+ _selectable: boolean;
23
+ onselect?: (data: SelectData) => void;
24
+ } = $props();
25
+
26
+ let element: HTMLElement;
27
+ let parent_element: HTMLElement;
28
+ let view: View | undefined;
29
+
30
+ let computed_style = window.getComputedStyle(document.body);
31
+
32
+ let old_spec: Spec = $state(null);
33
+ let spec_width: number = $state(0);
34
+
35
+ let plot = $derived(value?.plot);
36
+ let spec = $derived.by(() => {
37
+ if (!plot) return null;
38
+ let parsed_spec = JSON.parse(plot) as Spec;
39
+
40
+ if (parsed_spec && parsed_spec.params && !_selectable) {
41
+ parsed_spec.params = parsed_spec.params.filter(
42
+ (param) => param.name !== "brush"
43
+ );
44
+ }
45
+
46
+ untrack(() => {
47
+ if (old_spec !== parsed_spec) {
48
+ old_spec = parsed_spec;
49
+ spec_width = parsed_spec.width;
50
+ }
51
+
52
+ if (value.chart && parsed_spec) {
53
+ parsed_spec = set_config(
54
+ parsed_spec,
55
+ computed_style,
56
+ value.chart as string,
57
+ colors
58
+ );
59
+ }
60
+ });
61
+
62
+ return parsed_spec;
63
+ });
64
+
65
+ let fit_width_to_parent = $derived(
66
+ spec?.encoding?.column?.field ||
67
+ spec?.encoding?.row?.field ||
68
+ value.chart === undefined
69
+ ? false
70
+ : true
71
+ );
72
+
73
+ const get_width = (): number => {
74
+ return Math.min(
75
+ parent_element.offsetWidth,
76
+ spec_width || parent_element.offsetWidth
77
+ );
78
+ };
79
+ let resize_callback = (): void => {};
80
+ const renderPlot = (): void => {
81
+ if (view) {
82
+ view.finalize();
83
+ view = undefined;
84
+ }
85
+ if (fit_width_to_parent) {
86
+ spec.width = get_width();
87
+ }
88
+ vegaEmbed(element, spec, { actions: show_actions_button }).then(
89
+ function (result): void {
90
+ view = result.view;
91
+ resize_callback = () => {
92
+ view.signal("width", get_width()).run();
93
+ };
94
+
95
+ if (!_selectable) return;
96
+ const callback = (event, item): void => {
97
+ const brushValue = view.signal("brush");
98
+ if (brushValue) {
99
+ if (Object.keys(brushValue).length === 0) {
100
+ onselect?.({
101
+ value: null,
102
+ index: null,
103
+ selected: false
104
+ });
105
+ } else {
106
+ const key = Object.keys(brushValue)[0];
107
+ let range: [number, number] = brushValue[key].map(
108
+ (x) => x / 1000
109
+ );
110
+ onselect?.({
111
+ value: brushValue,
112
+ index: range,
113
+ selected: true
114
+ });
115
+ }
116
+ }
117
+ };
118
+ view.addEventListener("mouseup", callback);
119
+ view.addEventListener("touchup", callback);
120
+ }
121
+ );
122
+ };
123
+
124
+ const resizeObserver = new ResizeObserver(() => {
125
+ if (fit_width_to_parent && spec.width !== parent_element.offsetWidth) {
126
+ resize_callback();
127
+ }
128
+ });
129
+
130
+ $effect(() => {
131
+ if (!element || !parent_element || !spec) return;
132
+
133
+ renderPlot();
134
+ resizeObserver.observe(parent_element);
135
+
136
+ return () => {
137
+ if (view) {
138
+ view.finalize();
139
+ view = undefined;
140
+ }
141
+ resizeObserver.disconnect();
142
+ };
143
+ });
144
+ </script>
145
+
146
+ <div data-testid={"altair"} class="altair layout" bind:this={parent_element}>
147
+ <div bind:this={element}></div>
148
+ {#if caption}
149
+ <div class="caption layout">
150
+ {caption}
151
+ </div>
152
+ {/if}
153
+ </div>
154
+
155
+ <style>
156
+ .altair :global(canvas) {
157
+ padding: 6px;
158
+ }
159
+ .altair :global(.vega-embed) {
160
+ padding: 0px !important;
161
+ }
162
+ .altair :global(.vega-actions) {
163
+ right: 0px !important;
164
+ }
165
+ .layout {
166
+ display: flex;
167
+ flex-direction: column;
168
+ justify-content: center;
169
+ align-items: center;
170
+ width: var(--size-full);
171
+ height: var(--size-full);
172
+ color: var(--body-text-color);
173
+ }
174
+ .altair {
175
+ display: flex;
176
+ flex-direction: column;
177
+ justify-content: center;
178
+ align-items: center;
179
+ width: var(--size-full);
180
+ height: var(--size-full);
181
+ }
182
+ .caption {
183
+ font-size: var(--text-sm);
184
+ margin-bottom: 6px;
185
+ }
186
+ :global(#vg-tooltip-element) {
187
+ font-family: var(--font) !important;
188
+ font-size: var(--text-xs) !important;
189
+ box-shadow: none !important;
190
+ background-color: var(--block-background-fill) !important;
191
+ border: 1px solid var(--border-color-primary) !important;
192
+ color: var(--body-text-color) !important;
193
+ }
194
+ :global(#vg-tooltip-element .key) {
195
+ color: var(--body-text-color-subdued) !important;
196
+ }
197
+ </style>
6.23.1/plot/shared/plot_types/BokehPlot.svelte ADDED
@@ -0,0 +1,166 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ //@ts-nocheck
3
+ let { value, bokeh_version }: { value: any; bokeh_version: string | null } =
4
+ $props();
5
+
6
+ const div_id = `bokehDiv-${Math.random().toString(5).substring(2)}`;
7
+ let plot = $derived(value?.plot);
8
+
9
+ function static_base(version: string): string {
10
+ const root =
11
+ (typeof window !== "undefined" && window.gradio_config?.root) || "";
12
+ return `${root}/static/bokeh/${version}`;
13
+ }
14
+
15
+ function bokeh_local_url(version: string, filename: string): string {
16
+ return `${static_base(version)}/${filename}`;
17
+ }
18
+
19
+ function bokeh_cdn_url(version: string, filename: string): string {
20
+ if (filename === `bokeh-${version}.min.js`) {
21
+ return `https://cdn.bokeh.org/bokeh/release/${filename}`;
22
+ }
23
+ return `https://cdn.pydata.org/bokeh/release/${filename}`;
24
+ }
25
+
26
+ function load_script(
27
+ src: string,
28
+ fallback?: string
29
+ ): Promise<HTMLScriptElement> {
30
+ return new Promise((resolve, reject) => {
31
+ const script = document.createElement("script");
32
+ script.onload = () => resolve(script);
33
+ script.onerror = () => {
34
+ if (fallback) {
35
+ const fallback_script = document.createElement("script");
36
+ fallback_script.onload = () => resolve(fallback_script);
37
+ fallback_script.onerror = () => {
38
+ console.error(`Failed to load Bokeh script: ${src}`);
39
+ reject(new Error(`Failed to load ${src}`));
40
+ };
41
+ fallback_script.src = fallback;
42
+ document.head.appendChild(fallback_script);
43
+ } else {
44
+ console.error(`Failed to load Bokeh script: ${src}`);
45
+ reject(new Error(`Failed to load ${src}`));
46
+ }
47
+ };
48
+ script.src = src;
49
+ document.head.appendChild(script);
50
+ });
51
+ }
52
+
53
+ async function embed_bokeh(_plot: string): Promise<void> {
54
+ if (document) {
55
+ const el = document.getElementById(div_id);
56
+ if (el) {
57
+ el.innerHTML = "";
58
+ }
59
+ }
60
+ if (window.Bokeh) {
61
+ load_bokeh();
62
+ let plotObj = JSON.parse(_plot);
63
+ await window.Bokeh.embed.embed_item(plotObj, div_id);
64
+ }
65
+ }
66
+
67
+ const main_src = bokeh_version
68
+ ? bokeh_local_url(bokeh_version, `bokeh-${bokeh_version}.min.js`)
69
+ : null;
70
+
71
+ const main_fallback = bokeh_version
72
+ ? bokeh_cdn_url(bokeh_version, `bokeh-${bokeh_version}.min.js`)
73
+ : null;
74
+
75
+ const plugins_src = bokeh_version
76
+ ? [
77
+ bokeh_local_url(bokeh_version, `bokeh-widgets-${bokeh_version}.min.js`),
78
+ bokeh_local_url(bokeh_version, `bokeh-tables-${bokeh_version}.min.js`),
79
+ bokeh_local_url(bokeh_version, `bokeh-gl-${bokeh_version}.min.js`),
80
+ bokeh_local_url(bokeh_version, `bokeh-api-${bokeh_version}.min.js`)
81
+ ]
82
+ : [];
83
+
84
+ const plugins_fallback = bokeh_version
85
+ ? [
86
+ bokeh_cdn_url(bokeh_version, `bokeh-widgets-${bokeh_version}.min.js`),
87
+ bokeh_cdn_url(bokeh_version, `bokeh-tables-${bokeh_version}.min.js`),
88
+ bokeh_cdn_url(bokeh_version, `bokeh-gl-${bokeh_version}.min.js`),
89
+ bokeh_cdn_url(bokeh_version, `bokeh-api-${bokeh_version}.min.js`)
90
+ ]
91
+ : [];
92
+
93
+ let loaded = $state(false);
94
+ let plugin_scripts: HTMLScriptElement[] = $state([]);
95
+
96
+ async function load_plugins(): Promise<void> {
97
+ plugin_scripts = await Promise.all(
98
+ plugins_src.map((src, i) => load_script(src, plugins_fallback[i]))
99
+ );
100
+
101
+ loaded = true;
102
+ }
103
+
104
+ function handle_bokeh_loaded(): void {
105
+ load_plugins();
106
+ }
107
+
108
+ let added_main_script = false;
109
+
110
+ function load_bokeh(): HTMLScriptElement | null {
111
+ const script = document.createElement("script");
112
+ script.onload = handle_bokeh_loaded;
113
+ script.onerror = () => {
114
+ if (main_fallback) {
115
+ load_script(main_fallback)
116
+ .then(handle_bokeh_loaded)
117
+ .catch(() => {});
118
+ }
119
+ };
120
+ script.src = main_src;
121
+ const is_bokeh_script_present = document.head.querySelector(
122
+ `script[src="${main_src}"]`
123
+ );
124
+ if (!is_bokeh_script_present) {
125
+ document.head.appendChild(script);
126
+ added_main_script = true;
127
+ return script;
128
+ }
129
+ handle_bokeh_loaded();
130
+ return null;
131
+ }
132
+
133
+ const main_script = bokeh_version ? load_bokeh() : null;
134
+
135
+ $effect(() => {
136
+ if (loaded && plot) {
137
+ embed_bokeh(plot);
138
+ }
139
+ });
140
+
141
+ $effect(() => {
142
+ return () => {
143
+ if (
144
+ added_main_script &&
145
+ main_script &&
146
+ document.head.contains(main_script)
147
+ ) {
148
+ document.head.removeChild(main_script);
149
+ }
150
+ plugin_scripts.forEach((child) => {
151
+ if (document.head.contains(child)) {
152
+ document.head.removeChild(child);
153
+ }
154
+ });
155
+ };
156
+ });
157
+ </script>
158
+
159
+ <div data-testid={"bokeh"} id={div_id} class="gradio-bokeh" />
160
+
161
+ <style>
162
+ .gradio-bokeh {
163
+ display: flex;
164
+ justify-content: center;
165
+ }
166
+ </style>
6.23.1/plot/shared/plot_types/MatplotlibPlot.svelte ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ let { value }: { value: any } = $props();
3
+
4
+ let plot = $derived(value?.plot);
5
+ </script>
6
+
7
+ <div data-testid={"matplotlib"} class="matplotlib layout">
8
+ <img src={plot} alt={`${value.chart} plot visualising provided data`} />
9
+ </div>
10
+
11
+ <style>
12
+ .layout {
13
+ display: flex;
14
+ flex-direction: column;
15
+ justify-content: center;
16
+ align-items: center;
17
+ width: var(--size-full);
18
+ height: var(--size-full);
19
+ color: var(--body-text-color);
20
+ }
21
+ .matplotlib img {
22
+ object-fit: contain;
23
+ }
24
+ </style>
6.23.1/plot/shared/plot_types/PlotlyPlot.svelte ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ //@ts-nocheck
3
+ import Plotly from "plotly.js-dist-min";
4
+ import { untrack } from "svelte";
5
+
6
+ let {
7
+ value,
8
+ show_label,
9
+ loaded_plotly_css = $bindable(false)
10
+ }: {
11
+ value: any;
12
+ show_label: boolean;
13
+ loaded_plotly_css?: boolean;
14
+ } = $props();
15
+
16
+ let plot = $derived(value?.plot);
17
+
18
+ let plot_div: HTMLDivElement;
19
+ let plotly_global_style: HTMLElement;
20
+
21
+ function load_plotly_css(): void {
22
+ if (!loaded_plotly_css) {
23
+ plotly_global_style = document.getElementById("plotly.js-style-global");
24
+ const plotly_style_clone = plotly_global_style.cloneNode();
25
+ plot_div.appendChild(plotly_style_clone);
26
+ for (const rule of plotly_global_style.sheet.cssRules) {
27
+ plotly_style_clone.sheet.insertRule(rule.cssText);
28
+ }
29
+ loaded_plotly_css = true;
30
+ }
31
+ }
32
+
33
+ $effect(() => {
34
+ if (!plot_div || !plot) return;
35
+
36
+ // load_plotly_css writes loaded_plotly_css; untrack it so this effect
37
+ // doesn't re-run and collapse an autosize plot to zero height.
38
+ untrack(() => load_plotly_css());
39
+
40
+ let plotObj = JSON.parse(plot);
41
+
42
+ plotObj.config = plotObj.config || {};
43
+ plotObj.config.responsive = true;
44
+ plotObj.responsive = true;
45
+ plotObj.layout.autosize = true;
46
+
47
+ if (plotObj.layout.margin == undefined) {
48
+ plotObj.layout.margin = {};
49
+ }
50
+ if (plotObj.layout.title && show_label) {
51
+ plotObj.layout.margin.t = Math.max(100, plotObj.layout.margin.t || 0);
52
+ }
53
+ plotObj.layout.margin.autoexpand = true;
54
+
55
+ Plotly.react(plot_div, plotObj.data, plotObj.layout, plotObj.config);
56
+ Plotly.Plots.resize(plot_div);
57
+ });
58
+ </script>
59
+
60
+ <div data-testid={"plotly"} bind:this={plot_div}></div>
6.23.1/plot/shared/plot_types/altair_utils.ts ADDED
@@ -0,0 +1,111 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { colors as color_palette } from "@gradio/theme";
2
+ import { get_next_color } from "@gradio/utils";
3
+ import type { Config, TopLevelSpec as Spec } from "vega-lite";
4
+
5
+ export function set_config(
6
+ spec: Spec,
7
+ computed_style: CSSStyleDeclaration,
8
+ chart_type: string,
9
+ colors: string[]
10
+ ): Spec {
11
+ let accentColor = computed_style.getPropertyValue("--color-accent");
12
+ let bodyTextColor = computed_style.getPropertyValue("--body-text-color");
13
+ let borderColorPrimary = computed_style.getPropertyValue(
14
+ "--border-color-primary"
15
+ );
16
+ let fontFamily = computed_style.fontFamily;
17
+ let titleWeight = computed_style.getPropertyValue(
18
+ "--block-title-text-weight"
19
+ ) as "bold" | "normal" | 100 | 200 | 300 | 400 | 500 | 600 | 700 | 800 | 900;
20
+ const fontToPxVal = (font: string): number => {
21
+ return font.endsWith("px") ? parseFloat(font.slice(0, -2)) : 12;
22
+ };
23
+ let textSizeMd = fontToPxVal(computed_style.getPropertyValue("--text-md"));
24
+ let textSizeSm = fontToPxVal(computed_style.getPropertyValue("--text-sm"));
25
+ let config: Config = {
26
+ autosize: { type: "fit", contains: "padding" },
27
+ axis: {
28
+ labelFont: fontFamily,
29
+ labelColor: bodyTextColor,
30
+ titleFont: fontFamily,
31
+ titleColor: bodyTextColor,
32
+ tickColor: borderColorPrimary,
33
+ labelFontSize: textSizeSm,
34
+ gridColor: borderColorPrimary,
35
+ titleFontWeight: "normal",
36
+ titleFontSize: textSizeSm,
37
+ labelFontWeight: "normal",
38
+ domain: false,
39
+ labelAngle: 0
40
+ },
41
+ legend: {
42
+ labelColor: bodyTextColor,
43
+ labelFont: fontFamily,
44
+ titleColor: bodyTextColor,
45
+ titleFont: fontFamily,
46
+ titleFontWeight: "normal",
47
+ titleFontSize: textSizeSm,
48
+ labelFontWeight: "normal",
49
+ offset: 2
50
+ },
51
+ title: {
52
+ color: bodyTextColor,
53
+ font: fontFamily,
54
+ fontSize: textSizeMd,
55
+ fontWeight: titleWeight,
56
+ anchor: "middle"
57
+ },
58
+ view: {
59
+ stroke: borderColorPrimary
60
+ }
61
+ };
62
+ spec.config = config;
63
+ // @ts-ignore (unsure why the following are not typed in Spec)
64
+ let encoding: any = spec.encoding;
65
+ // @ts-ignore
66
+ let layer: any = spec.layer;
67
+ switch (chart_type) {
68
+ case "scatter":
69
+ spec.config.mark = { stroke: accentColor };
70
+ if (encoding.color && encoding.color.type == "nominal") {
71
+ encoding.color.scale.range = encoding.color.scale.range.map(
72
+ (_: string, i: number) => get_color(colors, i)
73
+ );
74
+ } else if (encoding.color && encoding.color.type == "quantitative") {
75
+ encoding.color.scale.range = ["#eff6ff", "#1e3a8a"];
76
+ encoding.color.scale.range.interpolate = "hsl";
77
+ }
78
+ break;
79
+ case "line":
80
+ spec.config.mark = { stroke: accentColor, cursor: "crosshair" };
81
+ layer.forEach((d: any) => {
82
+ if (d.encoding.color) {
83
+ d.encoding.color.scale.range = d.encoding.color.scale.range.map(
84
+ (_: any, i: any) => get_color(colors, i)
85
+ );
86
+ }
87
+ });
88
+ break;
89
+ case "bar":
90
+ spec.config.mark = { opacity: 0.8, fill: accentColor };
91
+ if (encoding.color) {
92
+ encoding.color.scale.range = encoding.color.scale.range.map(
93
+ (_: any, i: any) => get_color(colors, i)
94
+ );
95
+ }
96
+ break;
97
+ }
98
+ return spec;
99
+ }
100
+
101
+ function get_color(colors: string[], index: number): string {
102
+ let current_color = colors[index % colors.length];
103
+
104
+ if (current_color && current_color in color_palette) {
105
+ return color_palette[current_color as keyof typeof color_palette]?.primary;
106
+ } else if (!current_color) {
107
+ return color_palette[get_next_color(index) as keyof typeof color_palette]
108
+ .primary;
109
+ }
110
+ return current_color;
111
+ }
6.23.1/plot/types.ts ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import type { SelectData, CustomButton } from "@gradio/utils";
2
+ import type { ILoadingStatus as LoadingStatus } from "@gradio/statustracker";
3
+
4
+ export type ThemeMode = "system" | "light" | "dark";
5
+
6
+ export interface PlotProps {
7
+ value: null | string;
8
+ caption: string;
9
+ bokeh_version: string | null;
10
+ show_actions_button: boolean;
11
+ _selectable: boolean;
12
+ x_lim: [number, number] | null;
13
+ show_fullscreen_button: boolean;
14
+ buttons: (string | CustomButton)[] | null;
15
+ }
16
+
17
+ export interface PlotEvents {
18
+ change: never;
19
+ select: SelectData;
20
+ clear_status: LoadingStatus;
21
+ custom_button_click: { id: number };
22
+ }