{"cells": [{"cell_type": "markdown", "id": "302934307671667531413257853548643485645", "metadata": {}, "source": ["# Gradio Demo: bar_plot_demo"]}, {"cell_type": "code", "execution_count": null, "id": "272996653310673477252411125948039410165", "metadata": {}, "outputs": [], "source": ["!pip install -q gradio pandas "]}, {"cell_type": "code", "execution_count": null, "id": "288918539441861185822528903084949547379", "metadata": {}, "outputs": [], "source": ["import pandas as pd\n", "from random import randint, random\n", "import gradio as gr\n", "\n", "\n", "temp_sensor_data = pd.DataFrame(\n", " {\n", " \"time\": pd.date_range(\"2021-01-01\", end=\"2021-01-05\", periods=200),\n", " \"temperature\": [randint(50 + 10 * (i % 2), 65 + 15 * (i % 2)) for i in range(200)],\n", " \"humidity\": [randint(50 + 10 * (i % 2), 65 + 15 * (i % 2)) for i in range(200)],\n", " \"location\": [\"indoor\", \"outdoor\"] * 100,\n", " }\n", ")\n", "\n", "food_rating_data = pd.DataFrame(\n", " {\n", " \"cuisine\": [[\"Italian\", \"Mexican\", \"Chinese\"][i % 3] for i in range(100)],\n", " \"rating\": [random() * 4 + 0.5 * (i % 3) for i in range(100)],\n", " \"price\": [randint(10, 50) + 4 * (i % 3) for i in range(100)],\n", " \"wait\": [random() for i in range(100)],\n", " }\n", ")\n", "\n", "with gr.Blocks() as bar_plots:\n", " with gr.Row():\n", " start = gr.DateTime(\"2021-01-01 00:00:00\", label=\"Start\")\n", " end = gr.DateTime(\"2021-01-05 00:00:00\", label=\"End\")\n", " apply_btn = gr.Button(\"Apply\", scale=0)\n", " with gr.Row():\n", " group_by = gr.Radio([\"None\", \"30m\", \"1h\", \"4h\", \"1d\"], value=\"None\", label=\"Group by\")\n", " aggregate = gr.Radio([\"sum\", \"mean\", \"median\", \"min\", \"max\"], value=\"sum\", label=\"Aggregation\")\n", "\n", " temp_by_time = gr.BarPlot(\n", " temp_sensor_data,\n", " x=\"time\",\n", " y=\"temperature\",\n", " )\n", " temp_by_time_location = gr.BarPlot(\n", " temp_sensor_data,\n", " x=\"time\",\n", " y=\"temperature\",\n", " color=\"location\",\n", " )\n", "\n", " time_graphs = [temp_by_time, temp_by_time_location]\n", " group_by.change(\n", " lambda group: [gr.BarPlot(x_bin=None if group == \"None\" else group)] * len(time_graphs),\n", " group_by,\n", " time_graphs\n", " )\n", " aggregate.change(\n", " lambda aggregate: [gr.BarPlot(y_aggregate=aggregate)] * len(time_graphs),\n", " aggregate,\n", " time_graphs\n", " )\n", "\n", " def rescale(select: gr.SelectData):\n", " return select.index\n", " rescale_evt = gr.on([plot.select for plot in time_graphs], rescale, None, [start, end])\n", "\n", " for trigger in [apply_btn.click, rescale_evt.then]:\n", " trigger(\n", " lambda start, end: [gr.BarPlot(x_lim=[start, end])] * len(time_graphs), [start, end], time_graphs\n", " )\n", "\n", " with gr.Row():\n", " price_by_cuisine = gr.BarPlot(\n", " food_rating_data,\n", " x=\"cuisine\",\n", " y=\"price\",\n", " )\n", " with gr.Column(scale=0):\n", " gr.Button(\"Sort $ > $$$\").click(lambda: gr.BarPlot(sort=\"y\"), None, price_by_cuisine)\n", " gr.Button(\"Sort $$$ > $\").click(lambda: gr.BarPlot(sort=\"-y\"), None, price_by_cuisine)\n", " gr.Button(\"Sort A > Z\").click(lambda: gr.BarPlot(sort=[\"Chinese\", \"Italian\", \"Mexican\"]), None, price_by_cuisine)\n", "\n", " with gr.Row():\n", " price_by_rating = gr.BarPlot(\n", " food_rating_data,\n", " x=\"rating\",\n", " y=\"price\",\n", " x_bin=1,\n", " )\n", " price_by_rating_color = gr.BarPlot(\n", " food_rating_data,\n", " x=\"rating\",\n", " y=\"price\",\n", " color=\"cuisine\",\n", " x_bin=1,\n", " color_map={\"Italian\": \"red\", \"Mexican\": \"green\", \"Chinese\": \"blue\"},\n", " )\n", "\n", "if __name__ == \"__main__\":\n", " bar_plots.launch()\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5}