Spaces:
Runtime error
Runtime error
File size: 5,367 Bytes
57be732 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
<script context="module">
let _id = 0;
</script>
<script lang="ts">
import type { Gradio } from "@gradio/utils";
import { Block, BlockTitle } from "@gradio/atoms";
import { StatusTracker } from "@gradio/statustracker";
import type { LoadingStatus } from "@gradio/statustracker";
import { afterUpdate } from "svelte";
export let gradio: Gradio<{
change: number[];
input: number[];
clear_status: LoadingStatus;
}>;
export let elem_id = "";
export let elem_classes: string[] = [];
export let visible = true;
export let value: number[];
export let label = gradio.i18n("slider.slider");
export let info: string | undefined = undefined;
export let container = true;
export let scale: number | null = null;
export let min_width: number | undefined = undefined;
export let minimum = 0;
export let maximum = 100;
export let step: number;
export let show_label: boolean;
export let interactive: boolean;
export let loading_status: LoadingStatus;
export let value_is_output = false;
function handle_change(selected_min, selected_max): void {
value = [selected_min, selected_max];
gradio.dispatch("change", [selected_min, selected_max]);
if (!value_is_output) {
gradio.dispatch("input", [selected_min, selected_max])
}
}
function handle_min_change(event) {
selected_min = parseInt(event.target.value);
if (selected_min > selected_max) {
selected_max = selected_min;
}
}
function handle_max_change(event) {
selected_max = parseInt(event.target.value);
if (selected_max < selected_min) {
selected_min = selected_max;
}
}
let old_value = value;
let [selected_min, selected_max] = value;
$: if (JSON.stringify(old_value) !== JSON.stringify(value)) {
[selected_min, selected_max] = value;
old_value = value;
}
$: handle_change(selected_min, selected_max);
$: rangeLine = `
left: ${( (selected_min - minimum) / (maximum - minimum)) * 100}%;
width: ${ ((selected_max - selected_min) / (maximum - minimum)) * 100}%;
`;
</script>
<Block {visible} {elem_id} {elem_classes} {container} {scale} {min_width}>
<StatusTracker
autoscroll={gradio.autoscroll}
i18n={gradio.i18n}
{...loading_status}
on:clear_status={() => gradio.dispatch("clear_status", loading_status)}
/>
<div class="wrap">
<div class="head">
<BlockTitle {show_label} {info}>{label}</BlockTitle>
<div class="numbers">
<input
aria-label={`max input for ${label}`}
data-testid="max-input"
type="number"
bind:value={selected_max}
min={minimum}
max={maximum}
disabled={!interactive}
/>
<input
aria-label={`min input for ${label}`}
data-testid="min-input"
type="number"
bind:value={selected_min}
min={minimum}
max={maximum}
disabled={!interactive}
/>
</div>
</div>
</div>
<div class="range-slider">
<div class="range-bg"></div>
<div class="range-line" style={rangeLine}></div>
<input type="range" disabled={!interactive} min={minimum} max={maximum} {step} bind:value={selected_min} on:input={handle_min_change} />
<input type="range" disabled={!interactive} min={minimum} max={maximum} {step} bind:value={selected_max} on:input={handle_max_change} />
</div>
</Block>
<style>
.wrap {
display: flex;
flex-direction: column;
width: 100%;
}
.head {
display: flex;
justify-content: space-between;
}
.numbers {
display: flex;
flex-direction: row-reverse;
max-width: var(--size-6);
}
input[type="number"] {
display: block;
position: relative;
outline: none !important;
box-shadow: var(--input-shadow);
border: var(--input-border-width) solid var(--input-border-color);
border-radius: var(--input-radius);
background: var(--input-background-fill);
padding: var(--size-2) var(--size-2);
height: var(--size-6);
color: var(--body-text-color);
font-size: var(--input-text-size);
line-height: var(--line-sm);
text-align: center;
}
.range-slider {
position: relative;
width: 100%;
height: 30px;
}
.range-slider input[type="range"] {
position: absolute;
left: 0;
bottom: 0;
width: 100%;
appearance: none;
outline: none;
background: transparent;
pointer-events: none;
}
.range-slider input[type="range"]::-webkit-slider-thumb {
appearance: none;
width: 20px;
height: 20px;
background: white;
border-radius: 50%;
border: solid 0.5px #ddd;
pointer-events: auto;
cursor: pointer;
}
.range-slider input[type="range"]::-moz-range-thumb {
width: 20px;
height: 20px;
background: white;
border-radius: 50%;
border: solid 0.5px #ddd;
pointer-events: auto;
cursor: pointer;
}
.range-line {
position: absolute;
left: 0;
bottom: 8px;
height: 4px;
background: var(--slider-color);
pointer-events: none;
}
.range-bg {
position: absolute;
left: 0;
width: 100%;
bottom: 8px;
height: 4px;
z-index: 0;
background: var(--neutral-200);
pointer-events: none;
}
</style>
|