aliabd HF Staff commited on
Commit
4e88563
·
1 Parent(s): 97b1184

Upload folder using huggingface_hub

Browse files
Files changed (4) hide show
  1. README.md +6 -7
  2. requirements.txt +2 -0
  3. run.ipynb +1 -0
  4. run.py +106 -0
README.md CHANGED
@@ -1,12 +1,11 @@
 
1
  ---
2
- title: Line Plot
3
- emoji: 😻
4
- colorFrom: red
5
- colorTo: red
6
  sdk: gradio
7
  sdk_version: 3.36.0
8
- app_file: app.py
9
  pinned: false
10
  ---
11
-
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
+
2
  ---
3
+ title: line_plot
4
+ emoji: 🔥
5
+ colorFrom: indigo
6
+ colorTo: indigo
7
  sdk: gradio
8
  sdk_version: 3.36.0
9
+ app_file: run.py
10
  pinned: false
11
  ---
 
 
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ vega_datasets
2
+ pandas
run.ipynb ADDED
@@ -0,0 +1 @@
 
 
1
+ {"cells": [{"cell_type": "markdown", "id": 302934307671667531413257853548643485645, "metadata": {}, "source": ["# Gradio Demo: line_plot"]}, {"cell_type": "code", "execution_count": null, "id": 272996653310673477252411125948039410165, "metadata": {}, "outputs": [], "source": ["!pip install -q gradio vega_datasets pandas"]}, {"cell_type": "code", "execution_count": null, "id": 288918539441861185822528903084949547379, "metadata": {}, "outputs": [], "source": ["import gradio as gr\n", "from vega_datasets import data\n", "\n", "stocks = data.stocks()\n", "gapminder = data.gapminder()\n", "gapminder = gapminder.loc[\n", " gapminder.country.isin([\"Argentina\", \"Australia\", \"Afghanistan\"])\n", "]\n", "climate = data.climate()\n", "seattle_weather = data.seattle_weather()\n", "\n", "## Or generate your own fake data, here's an example for stocks:\n", "#\n", "# import pandas as pd\n", "# import random\n", "#\n", "# stocks = pd.DataFrame(\n", "# {\n", "# \"symbol\": [\n", "# random.choice(\n", "# [\n", "# \"MSFT\",\n", "# \"AAPL\",\n", "# \"AMZN\",\n", "# \"IBM\",\n", "# \"GOOG\",\n", "# ]\n", "# )\n", "# for _ in range(120)\n", "# ],\n", "# \"date\": [\n", "# pd.Timestamp(year=2000 + i, month=j, day=1)\n", "# for i in range(10)\n", "# for j in range(1, 13)\n", "# ],\n", "# \"price\": [random.randint(10, 200) for _ in range(120)],\n", "# }\n", "# )\n", "\n", "\n", "def line_plot_fn(dataset):\n", " if dataset == \"stocks\":\n", " return gr.LinePlot.update(\n", " stocks,\n", " x=\"date\",\n", " y=\"price\",\n", " color=\"symbol\",\n", " color_legend_position=\"bottom\",\n", " title=\"Stock Prices\",\n", " tooltip=[\"date\", \"price\", \"symbol\"],\n", " height=300,\n", " width=500,\n", " )\n", " elif dataset == \"climate\":\n", " return gr.LinePlot.update(\n", " climate,\n", " x=\"DATE\",\n", " y=\"HLY-TEMP-NORMAL\",\n", " y_lim=[250, 500],\n", " title=\"Climate\",\n", " tooltip=[\"DATE\", \"HLY-TEMP-NORMAL\"],\n", " height=300,\n", " width=500,\n", " )\n", " elif dataset == \"seattle_weather\":\n", " return gr.LinePlot.update(\n", " seattle_weather,\n", " x=\"date\",\n", " y=\"temp_min\",\n", " tooltip=[\"weather\", \"date\"],\n", " overlay_point=True,\n", " title=\"Seattle Weather\",\n", " height=300,\n", " width=500,\n", " )\n", " elif dataset == \"gapminder\":\n", " return gr.LinePlot.update(\n", " gapminder,\n", " x=\"year\",\n", " y=\"life_expect\",\n", " color=\"country\",\n", " title=\"Life expectancy for countries\",\n", " stroke_dash=\"cluster\",\n", " x_lim=[1950, 2010],\n", " tooltip=[\"country\", \"life_expect\"],\n", " stroke_dash_legend_title=\"Country Cluster\",\n", " height=300,\n", " width=500,\n", " )\n", "\n", "\n", "with gr.Blocks() as line_plot:\n", " with gr.Row():\n", " with gr.Column():\n", " dataset = gr.Dropdown(\n", " choices=[\"stocks\", \"climate\", \"seattle_weather\", \"gapminder\"],\n", " value=\"stocks\",\n", " )\n", " with gr.Column():\n", " plot = gr.LinePlot()\n", " dataset.change(line_plot_fn, inputs=dataset, outputs=plot)\n", " line_plot.load(fn=line_plot_fn, inputs=dataset, outputs=plot)\n", "\n", "\n", "if __name__ == \"__main__\":\n", " line_plot.launch()\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5}
run.py ADDED
@@ -0,0 +1,106 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from vega_datasets import data
3
+
4
+ stocks = data.stocks()
5
+ gapminder = data.gapminder()
6
+ gapminder = gapminder.loc[
7
+ gapminder.country.isin(["Argentina", "Australia", "Afghanistan"])
8
+ ]
9
+ climate = data.climate()
10
+ seattle_weather = data.seattle_weather()
11
+
12
+ ## Or generate your own fake data, here's an example for stocks:
13
+ #
14
+ # import pandas as pd
15
+ # import random
16
+ #
17
+ # stocks = pd.DataFrame(
18
+ # {
19
+ # "symbol": [
20
+ # random.choice(
21
+ # [
22
+ # "MSFT",
23
+ # "AAPL",
24
+ # "AMZN",
25
+ # "IBM",
26
+ # "GOOG",
27
+ # ]
28
+ # )
29
+ # for _ in range(120)
30
+ # ],
31
+ # "date": [
32
+ # pd.Timestamp(year=2000 + i, month=j, day=1)
33
+ # for i in range(10)
34
+ # for j in range(1, 13)
35
+ # ],
36
+ # "price": [random.randint(10, 200) for _ in range(120)],
37
+ # }
38
+ # )
39
+
40
+
41
+ def line_plot_fn(dataset):
42
+ if dataset == "stocks":
43
+ return gr.LinePlot.update(
44
+ stocks,
45
+ x="date",
46
+ y="price",
47
+ color="symbol",
48
+ color_legend_position="bottom",
49
+ title="Stock Prices",
50
+ tooltip=["date", "price", "symbol"],
51
+ height=300,
52
+ width=500,
53
+ )
54
+ elif dataset == "climate":
55
+ return gr.LinePlot.update(
56
+ climate,
57
+ x="DATE",
58
+ y="HLY-TEMP-NORMAL",
59
+ y_lim=[250, 500],
60
+ title="Climate",
61
+ tooltip=["DATE", "HLY-TEMP-NORMAL"],
62
+ height=300,
63
+ width=500,
64
+ )
65
+ elif dataset == "seattle_weather":
66
+ return gr.LinePlot.update(
67
+ seattle_weather,
68
+ x="date",
69
+ y="temp_min",
70
+ tooltip=["weather", "date"],
71
+ overlay_point=True,
72
+ title="Seattle Weather",
73
+ height=300,
74
+ width=500,
75
+ )
76
+ elif dataset == "gapminder":
77
+ return gr.LinePlot.update(
78
+ gapminder,
79
+ x="year",
80
+ y="life_expect",
81
+ color="country",
82
+ title="Life expectancy for countries",
83
+ stroke_dash="cluster",
84
+ x_lim=[1950, 2010],
85
+ tooltip=["country", "life_expect"],
86
+ stroke_dash_legend_title="Country Cluster",
87
+ height=300,
88
+ width=500,
89
+ )
90
+
91
+
92
+ with gr.Blocks() as line_plot:
93
+ with gr.Row():
94
+ with gr.Column():
95
+ dataset = gr.Dropdown(
96
+ choices=["stocks", "climate", "seattle_weather", "gapminder"],
97
+ value="stocks",
98
+ )
99
+ with gr.Column():
100
+ plot = gr.LinePlot()
101
+ dataset.change(line_plot_fn, inputs=dataset, outputs=plot)
102
+ line_plot.load(fn=line_plot_fn, inputs=dataset, outputs=plot)
103
+
104
+
105
+ if __name__ == "__main__":
106
+ line_plot.launch()