Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from vega_datasets import data
|
| 3 |
+
|
| 4 |
+
cars = data.cars()
|
| 5 |
+
iris = data.iris()
|
| 6 |
+
|
| 7 |
+
# # Or generate your own fake data
|
| 8 |
+
|
| 9 |
+
# import pandas as pd
|
| 10 |
+
# import random
|
| 11 |
+
|
| 12 |
+
# cars_data = {
|
| 13 |
+
# "Name": ["car name " + f" {int(i/10)}" for i in range(400)],
|
| 14 |
+
# "Miles_per_Gallon": [random.randint(10, 30) for _ in range(400)],
|
| 15 |
+
# "Origin": [random.choice(["USA", "Europe", "Japan"]) for _ in range(400)],
|
| 16 |
+
# "Horsepower": [random.randint(50, 250) for _ in range(400)],
|
| 17 |
+
# }
|
| 18 |
+
|
| 19 |
+
# iris_data = {
|
| 20 |
+
# "petalWidth": [round(random.uniform(0, 2.5), 2) for _ in range(150)],
|
| 21 |
+
# "petalLength": [round(random.uniform(0, 7), 2) for _ in range(150)],
|
| 22 |
+
# "species": [
|
| 23 |
+
# random.choice(["setosa", "versicolor", "virginica"]) for _ in range(150)
|
| 24 |
+
# ],
|
| 25 |
+
# }
|
| 26 |
+
|
| 27 |
+
# cars = pd.DataFrame(cars_data)
|
| 28 |
+
# iris = pd.DataFrame(iris_data)
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def scatter_plot_fn(dataset):
|
| 32 |
+
if dataset == "iris":
|
| 33 |
+
return gr.ScatterPlot(
|
| 34 |
+
value=iris,
|
| 35 |
+
x="petalWidth",
|
| 36 |
+
y="petalLength",
|
| 37 |
+
color="species",
|
| 38 |
+
title="Iris Dataset",
|
| 39 |
+
color_legend_title="Species",
|
| 40 |
+
x_title="Petal Width",
|
| 41 |
+
y_title="Petal Length",
|
| 42 |
+
tooltip=["petalWidth", "petalLength", "species"],
|
| 43 |
+
caption="",
|
| 44 |
+
)
|
| 45 |
+
else:
|
| 46 |
+
return gr.ScatterPlot(
|
| 47 |
+
value=cars,
|
| 48 |
+
x="Horsepower",
|
| 49 |
+
y="Miles_per_Gallon",
|
| 50 |
+
color="Origin",
|
| 51 |
+
tooltip="Name",
|
| 52 |
+
title="Car Data",
|
| 53 |
+
y_title="Miles per Gallon",
|
| 54 |
+
color_legend_title="Origin of Car",
|
| 55 |
+
caption="MPG vs Horsepower of various cars",
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
with gr.Blocks() as scatter_plot:
|
| 60 |
+
with gr.Row():
|
| 61 |
+
with gr.Column():
|
| 62 |
+
dataset = gr.Dropdown(choices=["cars", "iris"], value="cars")
|
| 63 |
+
with gr.Column():
|
| 64 |
+
plot = gr.ScatterPlot()
|
| 65 |
+
dataset.change(scatter_plot_fn, inputs=dataset, outputs=plot)
|
| 66 |
+
scatter_plot.load(fn=scatter_plot_fn, inputs=dataset, outputs=plot)
|
| 67 |
+
|
| 68 |
+
if __name__ == "__main__":
|
| 69 |
+
scatter_plot.launch()
|