gradio-pr-bot commited on
Commit
11bce84
·
verified ·
1 Parent(s): c36a635

Upload folder using huggingface_hub

Browse files
6.0.0-dev.6/plot/Index.svelte ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script context="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.show_fullscreen_button}
43
+ <IconButtonWrapper>
44
+ <FullscreenButton
45
+ {fullscreen}
46
+ on:fullscreen={({ detail }) => {
47
+ fullscreen = detail;
48
+ }}
49
+ />
50
+ </IconButtonWrapper>
51
+ {/if}
52
+ <StatusTracker
53
+ autoscroll={gradio.shared.autoscroll}
54
+ i18n={gradio.i18n}
55
+ {...gradio.shared.loading_status}
56
+ on:clear_status={() =>
57
+ gradio.dispatch("clear_status", gradio.shared.loading_status)}
58
+ />
59
+ <Plot {gradio} />
60
+ </Block>
6.0.0-dev.6/plot/package.json ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "@gradio/plot",
3
+ "version": "0.9.25-dev.1",
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.43.4"
41
+ },
42
+ "repository": {
43
+ "type": "git",
44
+ "url": "git+https://github.com/gradio-app/gradio.git",
45
+ "directory": "js/plot"
46
+ }
47
+ }
6.0.0-dev.6/plot/shared/Plot.svelte ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ //@ts-nocheck
3
+ import { Plot as PlotIcon } from "@gradio/icons";
4
+ import { Empty } from "@gradio/atoms";
5
+ import type { Gradio } from "@gradio/utils";
6
+ import type { PlotEvents, PlotProps } from "../types";
7
+ import { untrack } from "svelte";
8
+
9
+ let { gradio }: { gradio: Gradio<PlotEvents, PlotProps> } = $props();
10
+
11
+ let PlotComponent: any = $state(null);
12
+ let loaded_plotly_css = $state(false);
13
+ let key = $state(0);
14
+
15
+ const plotTypeMapping = {
16
+ plotly: () => import("./plot_types/PlotlyPlot.svelte"),
17
+ bokeh: () => import("./plot_types/BokehPlot.svelte"),
18
+ matplotlib: () => import("./plot_types/MatplotlibPlot.svelte"),
19
+ altair: () => import("./plot_types/AltairPlot.svelte")
20
+ };
21
+
22
+ let loadedPlotTypeMapping = {};
23
+
24
+ const is_browser = typeof window !== "undefined";
25
+
26
+ let value = $derived(gradio.props.value);
27
+ let _type = $state(null);
28
+
29
+ $effect(() => {
30
+ console.log("Plot value changed:", value);
31
+ let type = value?.type;
32
+ untrack(() => {
33
+ key = key + 1;
34
+ if (type !== _type) {
35
+ PlotComponent = null;
36
+ }
37
+ if (type && type in plotTypeMapping && is_browser) {
38
+ if (loadedPlotTypeMapping[type]) {
39
+ PlotComponent = loadedPlotTypeMapping[type];
40
+ } else {
41
+ plotTypeMapping[type]().then((module) => {
42
+ PlotComponent = module.default;
43
+ loadedPlotTypeMapping[type] = PlotComponent;
44
+ });
45
+ }
46
+ }
47
+ _type = type;
48
+ });
49
+ gradio.dispatch("change");
50
+ });
51
+ </script>
52
+
53
+ {#if gradio.props.value && PlotComponent}
54
+ {#key key}
55
+ <PlotComponent
56
+ value={gradio.props.value}
57
+ colors={[]}
58
+ theme_mode={gradio.props.theme_mode}
59
+ show_label={gradio.shared.show_label}
60
+ caption={gradio.props.caption}
61
+ bokeh_version={gradio.props.bokeh_version}
62
+ show_actions_button={gradio.props.show_actions_button}
63
+ {gradio}
64
+ _selectable={gradio.props._selectable}
65
+ x_lim={gradio.props.x_lim}
66
+ bind:loaded_plotly_css
67
+ on:select
68
+ />
69
+ {/key}
70
+ {:else}
71
+ <Empty unpadded_box={true} size="large"><PlotIcon /></Empty>
72
+ {/if}
6.0.0-dev.6/plot/shared/plot_types/AltairPlot.svelte ADDED
@@ -0,0 +1,188 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ //@ts-nocheck
3
+ import { set_config } from "./altair_utils";
4
+ import { onMount, onDestroy, untrack } from "svelte";
5
+ import type { TopLevelSpec as Spec } from "vega-lite";
6
+ import vegaEmbed from "vega-embed";
7
+ import type { Gradio, SelectData } from "@gradio/utils";
8
+ import type { View } from "vega";
9
+
10
+ let {
11
+ value,
12
+ colors = [],
13
+ caption,
14
+ show_actions_button,
15
+ gradio,
16
+ _selectable
17
+ }: {
18
+ value: any;
19
+ colors?: string[];
20
+ caption: string;
21
+ show_actions_button: boolean;
22
+ gradio: Gradio<{
23
+ select: SelectData;
24
+ }>;
25
+ _selectable: boolean;
26
+ } = $props();
27
+
28
+ let element: HTMLElement;
29
+ let parent_element: HTMLElement;
30
+ let view: View;
31
+
32
+ let computed_style = window.getComputedStyle(document.body);
33
+
34
+ let old_spec: Spec = $state(null);
35
+ let spec_width: number = $state(0);
36
+
37
+ let plot = $derived(value?.plot);
38
+ console.log(0);
39
+ let spec = $derived.by(() => {
40
+ if (!plot) return null;
41
+ let parsed_spec = JSON.parse(plot) as Spec;
42
+
43
+ // Filter out brush param if not selectable
44
+ if (parsed_spec && parsed_spec.params && !_selectable) {
45
+ parsed_spec.params = parsed_spec.params.filter(
46
+ (param) => param.name !== "brush"
47
+ );
48
+ }
49
+
50
+ untrack(() => {
51
+ if (old_spec !== parsed_spec) {
52
+ old_spec = parsed_spec;
53
+ spec_width = parsed_spec.width;
54
+ }
55
+
56
+ if (value.chart && parsed_spec) {
57
+ parsed_spec = set_config(
58
+ parsed_spec,
59
+ computed_style,
60
+ value.chart as string,
61
+ colors
62
+ );
63
+ }
64
+ });
65
+
66
+ return parsed_spec;
67
+ });
68
+
69
+ let fit_width_to_parent = $derived(
70
+ spec?.encoding?.column?.field ||
71
+ spec?.encoding?.row?.field ||
72
+ value.chart === undefined
73
+ ? false
74
+ : true
75
+ );
76
+
77
+ const get_width = (): number => {
78
+ return Math.min(
79
+ parent_element.offsetWidth,
80
+ spec_width || parent_element.offsetWidth
81
+ );
82
+ };
83
+ let resize_callback = (): void => {};
84
+ const renderPlot = (): void => {
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
+ gradio.dispatch("select", {
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
+ gradio.dispatch("select", {
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
+ let resizeObserver = new ResizeObserver(() => {
124
+ if (fit_width_to_parent && spec.width !== parent_element.offsetWidth) {
125
+ resize_callback();
126
+ }
127
+ });
128
+ onMount(() => {
129
+ renderPlot();
130
+ resizeObserver.observe(parent_element);
131
+ });
132
+ onDestroy(() => {
133
+ resizeObserver.disconnect();
134
+ });
135
+ </script>
136
+
137
+ <div data-testid={"altair"} class="altair layout" bind:this={parent_element}>
138
+ <div bind:this={element}></div>
139
+ {#if caption}
140
+ <div class="caption layout">
141
+ {caption}
142
+ </div>
143
+ {/if}
144
+ </div>
145
+
146
+ <style>
147
+ .altair :global(canvas) {
148
+ padding: 6px;
149
+ }
150
+ .altair :global(.vega-embed) {
151
+ padding: 0px !important;
152
+ }
153
+ .altair :global(.vega-actions) {
154
+ right: 0px !important;
155
+ }
156
+ .layout {
157
+ display: flex;
158
+ flex-direction: column;
159
+ justify-content: center;
160
+ align-items: center;
161
+ width: var(--size-full);
162
+ height: var(--size-full);
163
+ color: var(--body-text-color);
164
+ }
165
+ .altair {
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
+ }
173
+ .caption {
174
+ font-size: var(--text-sm);
175
+ margin-bottom: 6px;
176
+ }
177
+ :global(#vg-tooltip-element) {
178
+ font-family: var(--font) !important;
179
+ font-size: var(--text-xs) !important;
180
+ box-shadow: none !important;
181
+ background-color: var(--block-background-fill) !important;
182
+ border: 1px solid var(--border-color-primary) !important;
183
+ color: var(--body-text-color) !important;
184
+ }
185
+ :global(#vg-tooltip-element .key) {
186
+ color: var(--body-text-color-subdued) !important;
187
+ }
188
+ </style>
6.0.0-dev.6/plot/shared/plot_types/BokehPlot.svelte ADDED
@@ -0,0 +1,89 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ //@ts-nocheck
3
+ import { onDestroy } from "svelte";
4
+
5
+ export let value;
6
+ export let bokeh_version: string | null;
7
+ const div_id = `bokehDiv-${Math.random().toString(5).substring(2)}`;
8
+ $: plot = value?.plot;
9
+
10
+ async function embed_bokeh(_plot: Record<string, any>): void {
11
+ if (document) {
12
+ if (document.getElementById(div_id)) {
13
+ document.getElementById(div_id).innerHTML = "";
14
+ }
15
+ }
16
+ if (window.Bokeh) {
17
+ load_bokeh();
18
+ let plotObj = JSON.parse(_plot);
19
+ await window.Bokeh.embed.embed_item(plotObj, div_id);
20
+ }
21
+ }
22
+
23
+ $: loaded && embed_bokeh(plot);
24
+
25
+ const main_src = `https://cdn.bokeh.org/bokeh/release/bokeh-${bokeh_version}.min.js`;
26
+
27
+ const plugins_src = [
28
+ `https://cdn.pydata.org/bokeh/release/bokeh-widgets-${bokeh_version}.min.js`,
29
+ `https://cdn.pydata.org/bokeh/release/bokeh-tables-${bokeh_version}.min.js`,
30
+ `https://cdn.pydata.org/bokeh/release/bokeh-gl-${bokeh_version}.min.js`,
31
+ `https://cdn.pydata.org/bokeh/release/bokeh-api-${bokeh_version}.min.js`
32
+ ];
33
+
34
+ let loaded = false;
35
+ async function load_plugins(): HTMLScriptElement[] {
36
+ await Promise.all(
37
+ plugins_src.map((src, i) => {
38
+ return new Promise((resolve) => {
39
+ const script = document.createElement("script");
40
+ script.onload = resolve;
41
+ script.src = src;
42
+ document.head.appendChild(script);
43
+ return script;
44
+ });
45
+ })
46
+ );
47
+
48
+ loaded = true;
49
+ }
50
+
51
+ let plugin_scripts = [];
52
+
53
+ function handle_bokeh_loaded(): void {
54
+ plugin_scripts = load_plugins();
55
+ }
56
+
57
+ function load_bokeh(): HTMLScriptElement {
58
+ const script = document.createElement("script");
59
+ script.onload = handle_bokeh_loaded;
60
+ script.src = main_src;
61
+ const is_bokeh_script_present = document.head.querySelector(
62
+ `script[src="${main_src}"]`
63
+ );
64
+ if (!is_bokeh_script_present) {
65
+ document.head.appendChild(script);
66
+ } else {
67
+ handle_bokeh_loaded();
68
+ }
69
+ return script;
70
+ }
71
+
72
+ const main_script = bokeh_version ? load_bokeh() : null;
73
+
74
+ onDestroy(() => {
75
+ if (main_script in document.children) {
76
+ document.removeChild(main_script);
77
+ plugin_scripts.forEach((child) => document.removeChild(child));
78
+ }
79
+ });
80
+ </script>
81
+
82
+ <div data-testid={"bokeh"} id={div_id} class="gradio-bokeh" />
83
+
84
+ <style>
85
+ .gradio-bokeh {
86
+ display: flex;
87
+ justify-content: center;
88
+ }
89
+ </style>
6.0.0-dev.6/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.0.0-dev.6/plot/shared/plot_types/PlotlyPlot.svelte ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ //@ts-nocheck
3
+ import Plotly from "plotly.js-dist-min";
4
+ import { afterUpdate } from "svelte";
5
+
6
+ export let value;
7
+ export let show_label: boolean;
8
+ export let loaded_plotly_css = false;
9
+
10
+ $: plot = value?.plot;
11
+
12
+ let plot_div;
13
+ let plotly_global_style;
14
+
15
+ function load_plotly_css(): void {
16
+ if (!loaded_plotly_css) {
17
+ plotly_global_style = document.getElementById("plotly.js-style-global");
18
+ const plotly_style_clone = plotly_global_style.cloneNode();
19
+ plot_div.appendChild(plotly_style_clone);
20
+ for (const rule of plotly_global_style.sheet.cssRules) {
21
+ plotly_style_clone.sheet.insertRule(rule.cssText);
22
+ }
23
+ loaded_plotly_css = true;
24
+ }
25
+ }
26
+
27
+ afterUpdate(async () => {
28
+ load_plotly_css();
29
+
30
+ let plotObj = JSON.parse(plot);
31
+
32
+ // the docs aren't very good but this works
33
+ plotObj.config = plotObj.config || {};
34
+ plotObj.config.responsive = true;
35
+ plotObj.responsive = true;
36
+ plotObj.layout.autosize = true;
37
+
38
+ if (plotObj.layout.margin == undefined) {
39
+ plotObj.layout.margin = {};
40
+ }
41
+ if (plotObj.layout.title && show_label) {
42
+ // so title does not get cut off by label
43
+ plotObj.layout.margin.t = Math.max(100, plotObj.layout.margin.t || 0);
44
+ }
45
+ plotObj.layout.margin.autoexpand = true;
46
+
47
+ Plotly.react(plot_div, plotObj.data, plotObj.layout, plotObj.config);
48
+ Plotly.Plots.resize(plot_div);
49
+ });
50
+ </script>
51
+
52
+ <div data-testid={"plotly"} bind:this={plot_div}></div>
6.0.0-dev.6/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.0.0-dev.6/plot/types.ts ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import type { SelectData } from "@gradio/utils";
2
+ import type { LoadingStatus } from "@gradio/statustracker";
3
+
4
+ export type ThemeMode = "system" | "light" | "dark";
5
+
6
+ export interface PlotProps {
7
+ value: null | string;
8
+ theme_mode: ThemeMode;
9
+ caption: string;
10
+ bokeh_version: string | null;
11
+ show_actions_button: boolean;
12
+ _selectable: boolean;
13
+ x_lim: [number, number] | null;
14
+ show_fullscreen_button: boolean;
15
+ }
16
+
17
+ export interface PlotEvents {
18
+ change: never;
19
+ select: SelectData;
20
+ clear_status: LoadingStatus;
21
+ }