File size: 1,529 Bytes
94722df
1
{"cells": [{"cell_type": "markdown", "id": "302934307671667531413257853548643485645", "metadata": {}, "source": ["# Gradio Demo: sine_curve"]}, {"cell_type": "code", "execution_count": null, "id": "272996653310673477252411125948039410165", "metadata": {}, "outputs": [], "source": ["!pip install -q gradio numpy plotly "]}, {"cell_type": "code", "execution_count": null, "id": "288918539441861185822528903084949547379", "metadata": {}, "outputs": [], "source": ["import math\n", "import gradio as gr\n", "import plotly.express as px\n", "import numpy as np\n", "\n", "plot_end = 2 * math.pi\n", "\n", "def get_plot(period=1):\n", "    global plot_end\n", "    x = np.arange(plot_end - 2 * math.pi, plot_end, 0.02)\n", "    y = np.sin(2*math.pi*period * x)\n", "    fig = px.line(x=x, y=y)\n", "    plot_end += 2 * math.pi\n", "    if plot_end > 1000:\n", "        plot_end = 2 * math.pi\n", "    return fig\n", "\n", "with gr.Blocks() as demo:\n", "    with gr.Row():\n", "        with gr.Column():\n", "            gr.Markdown(\"Change the value of the slider to automatically update the plot\")\n", "            period = gr.Slider(label=\"Period of plot\", value=1, minimum=0, maximum=10, step=1)\n", "            plot = gr.Plot(label=\"Plot (updates every half second)\")\n", "\n", "    dep = demo.load(get_plot, None, plot, every=1)\n", "    period.change(get_plot, period, plot, every=1, cancels=[dep])\n", "\n", "if __name__ == \"__main__\":\n", "    demo.launch()\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5}