Upload folder using huggingface_hub
Browse files- 6.0.2/plot/Index.svelte +60 -0
- 6.0.2/plot/package.json +47 -0
- 6.0.2/plot/shared/Plot.svelte +71 -0
- 6.0.2/plot/shared/plot_types/AltairPlot.svelte +187 -0
- 6.0.2/plot/shared/plot_types/BokehPlot.svelte +89 -0
- 6.0.2/plot/shared/plot_types/MatplotlibPlot.svelte +24 -0
- 6.0.2/plot/shared/plot_types/PlotlyPlot.svelte +52 -0
- 6.0.2/plot/shared/plot_types/altair_utils.ts +111 -0
- 6.0.2/plot/types.ts +21 -0
6.0.2/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.2/plot/package.json
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "@gradio/plot",
|
| 3 |
+
"version": "0.9.25",
|
| 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.2/plot/shared/Plot.svelte
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
let type = value?.type;
|
| 31 |
+
untrack(() => {
|
| 32 |
+
key = key + 1;
|
| 33 |
+
if (type !== _type) {
|
| 34 |
+
PlotComponent = null;
|
| 35 |
+
}
|
| 36 |
+
if (type && type in plotTypeMapping && is_browser) {
|
| 37 |
+
if (loadedPlotTypeMapping[type]) {
|
| 38 |
+
PlotComponent = loadedPlotTypeMapping[type];
|
| 39 |
+
} else {
|
| 40 |
+
plotTypeMapping[type]().then((module) => {
|
| 41 |
+
PlotComponent = module.default;
|
| 42 |
+
loadedPlotTypeMapping[type] = PlotComponent;
|
| 43 |
+
});
|
| 44 |
+
}
|
| 45 |
+
}
|
| 46 |
+
_type = type;
|
| 47 |
+
});
|
| 48 |
+
gradio.dispatch("change");
|
| 49 |
+
});
|
| 50 |
+
</script>
|
| 51 |
+
|
| 52 |
+
{#if gradio.props.value && PlotComponent}
|
| 53 |
+
{#key key}
|
| 54 |
+
<PlotComponent
|
| 55 |
+
value={gradio.props.value}
|
| 56 |
+
colors={[]}
|
| 57 |
+
theme_mode={gradio.props.theme_mode}
|
| 58 |
+
show_label={gradio.shared.show_label}
|
| 59 |
+
caption={gradio.props.caption}
|
| 60 |
+
bokeh_version={gradio.props.bokeh_version}
|
| 61 |
+
show_actions_button={gradio.props.show_actions_button}
|
| 62 |
+
{gradio}
|
| 63 |
+
_selectable={gradio.props._selectable}
|
| 64 |
+
x_lim={gradio.props.x_lim}
|
| 65 |
+
bind:loaded_plotly_css
|
| 66 |
+
on:select
|
| 67 |
+
/>
|
| 68 |
+
{/key}
|
| 69 |
+
{:else}
|
| 70 |
+
<Empty unpadded_box={true} size="large"><PlotIcon /></Empty>
|
| 71 |
+
{/if}
|
6.0.2/plot/shared/plot_types/AltairPlot.svelte
ADDED
|
@@ -0,0 +1,187 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
let spec = $derived.by(() => {
|
| 39 |
+
if (!plot) return null;
|
| 40 |
+
let parsed_spec = JSON.parse(plot) as Spec;
|
| 41 |
+
|
| 42 |
+
// Filter out brush param if not selectable
|
| 43 |
+
if (parsed_spec && parsed_spec.params && !_selectable) {
|
| 44 |
+
parsed_spec.params = parsed_spec.params.filter(
|
| 45 |
+
(param) => param.name !== "brush"
|
| 46 |
+
);
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
untrack(() => {
|
| 50 |
+
if (old_spec !== parsed_spec) {
|
| 51 |
+
old_spec = parsed_spec;
|
| 52 |
+
spec_width = parsed_spec.width;
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
if (value.chart && parsed_spec) {
|
| 56 |
+
parsed_spec = set_config(
|
| 57 |
+
parsed_spec,
|
| 58 |
+
computed_style,
|
| 59 |
+
value.chart as string,
|
| 60 |
+
colors
|
| 61 |
+
);
|
| 62 |
+
}
|
| 63 |
+
});
|
| 64 |
+
|
| 65 |
+
return parsed_spec;
|
| 66 |
+
});
|
| 67 |
+
|
| 68 |
+
let fit_width_to_parent = $derived(
|
| 69 |
+
spec?.encoding?.column?.field ||
|
| 70 |
+
spec?.encoding?.row?.field ||
|
| 71 |
+
value.chart === undefined
|
| 72 |
+
? false
|
| 73 |
+
: true
|
| 74 |
+
);
|
| 75 |
+
|
| 76 |
+
const get_width = (): number => {
|
| 77 |
+
return Math.min(
|
| 78 |
+
parent_element.offsetWidth,
|
| 79 |
+
spec_width || parent_element.offsetWidth
|
| 80 |
+
);
|
| 81 |
+
};
|
| 82 |
+
let resize_callback = (): void => {};
|
| 83 |
+
const renderPlot = (): void => {
|
| 84 |
+
if (fit_width_to_parent) {
|
| 85 |
+
spec.width = get_width();
|
| 86 |
+
}
|
| 87 |
+
vegaEmbed(element, spec, { actions: show_actions_button }).then(
|
| 88 |
+
function (result): void {
|
| 89 |
+
view = result.view;
|
| 90 |
+
resize_callback = () => {
|
| 91 |
+
view.signal("width", get_width()).run();
|
| 92 |
+
};
|
| 93 |
+
|
| 94 |
+
if (!_selectable) return;
|
| 95 |
+
const callback = (event, item): void => {
|
| 96 |
+
const brushValue = view.signal("brush");
|
| 97 |
+
if (brushValue) {
|
| 98 |
+
if (Object.keys(brushValue).length === 0) {
|
| 99 |
+
gradio.dispatch("select", {
|
| 100 |
+
value: null,
|
| 101 |
+
index: null,
|
| 102 |
+
selected: false
|
| 103 |
+
});
|
| 104 |
+
} else {
|
| 105 |
+
const key = Object.keys(brushValue)[0];
|
| 106 |
+
let range: [number, number] = brushValue[key].map(
|
| 107 |
+
(x) => x / 1000
|
| 108 |
+
);
|
| 109 |
+
gradio.dispatch("select", {
|
| 110 |
+
value: brushValue,
|
| 111 |
+
index: range,
|
| 112 |
+
selected: true
|
| 113 |
+
});
|
| 114 |
+
}
|
| 115 |
+
}
|
| 116 |
+
};
|
| 117 |
+
view.addEventListener("mouseup", callback);
|
| 118 |
+
view.addEventListener("touchup", callback);
|
| 119 |
+
}
|
| 120 |
+
);
|
| 121 |
+
};
|
| 122 |
+
let resizeObserver = new ResizeObserver(() => {
|
| 123 |
+
if (fit_width_to_parent && spec.width !== parent_element.offsetWidth) {
|
| 124 |
+
resize_callback();
|
| 125 |
+
}
|
| 126 |
+
});
|
| 127 |
+
onMount(() => {
|
| 128 |
+
renderPlot();
|
| 129 |
+
resizeObserver.observe(parent_element);
|
| 130 |
+
});
|
| 131 |
+
onDestroy(() => {
|
| 132 |
+
resizeObserver.disconnect();
|
| 133 |
+
});
|
| 134 |
+
</script>
|
| 135 |
+
|
| 136 |
+
<div data-testid={"altair"} class="altair layout" bind:this={parent_element}>
|
| 137 |
+
<div bind:this={element}></div>
|
| 138 |
+
{#if caption}
|
| 139 |
+
<div class="caption layout">
|
| 140 |
+
{caption}
|
| 141 |
+
</div>
|
| 142 |
+
{/if}
|
| 143 |
+
</div>
|
| 144 |
+
|
| 145 |
+
<style>
|
| 146 |
+
.altair :global(canvas) {
|
| 147 |
+
padding: 6px;
|
| 148 |
+
}
|
| 149 |
+
.altair :global(.vega-embed) {
|
| 150 |
+
padding: 0px !important;
|
| 151 |
+
}
|
| 152 |
+
.altair :global(.vega-actions) {
|
| 153 |
+
right: 0px !important;
|
| 154 |
+
}
|
| 155 |
+
.layout {
|
| 156 |
+
display: flex;
|
| 157 |
+
flex-direction: column;
|
| 158 |
+
justify-content: center;
|
| 159 |
+
align-items: center;
|
| 160 |
+
width: var(--size-full);
|
| 161 |
+
height: var(--size-full);
|
| 162 |
+
color: var(--body-text-color);
|
| 163 |
+
}
|
| 164 |
+
.altair {
|
| 165 |
+
display: flex;
|
| 166 |
+
flex-direction: column;
|
| 167 |
+
justify-content: center;
|
| 168 |
+
align-items: center;
|
| 169 |
+
width: var(--size-full);
|
| 170 |
+
height: var(--size-full);
|
| 171 |
+
}
|
| 172 |
+
.caption {
|
| 173 |
+
font-size: var(--text-sm);
|
| 174 |
+
margin-bottom: 6px;
|
| 175 |
+
}
|
| 176 |
+
:global(#vg-tooltip-element) {
|
| 177 |
+
font-family: var(--font) !important;
|
| 178 |
+
font-size: var(--text-xs) !important;
|
| 179 |
+
box-shadow: none !important;
|
| 180 |
+
background-color: var(--block-background-fill) !important;
|
| 181 |
+
border: 1px solid var(--border-color-primary) !important;
|
| 182 |
+
color: var(--body-text-color) !important;
|
| 183 |
+
}
|
| 184 |
+
:global(#vg-tooltip-element .key) {
|
| 185 |
+
color: var(--body-text-color-subdued) !important;
|
| 186 |
+
}
|
| 187 |
+
</style>
|
6.0.2/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.2/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.2/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.2/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.2/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 |
+
}
|