import math import pandas as pd import gradio as gr import datetime import numpy as np def get_time(): return datetime.datetime.now() plot_end_1 = 2 * math.pi plot_end_2 = 2 * math.pi plot_end_3 = 2 * math.pi def get_plot_v1(period_v1=1,xscale_v1 = 1,yscale_v1 = 1): global plot_end_1 x = np.arange(plot_end_1 - 2 * math.pi, plot_end_1, 0.02) #y = np.sin(2 * math.pi * period_v1 * x) y = 2.02 vMean = 2.02 vPp = 0 #y = np.float64(0.5) update = gr.LinePlot.update( value=pd.DataFrame({"x": x, "y": y}), x="x", y="y", title= f"V mean = {vMean},Vpp = {vPp}" , width=500, height=350, y_lim= [ -4,4], y_title = 'V' ) plot_end_1 += 2 * math.pi if plot_end_1 > 1000: plot_end_1 = 2 * math.pi return update def get_plot_i1(period_i1=1,xscale_i1 = 1,yscale_i1 = 1): global plot_end_2 x = np.arange(plot_end_2 - 2 * math.pi, plot_end_2, 0.02) #y = np.sin(2 * math.pi * period_i1 * x) #y = 0 iMean = 0 iPp = 0 y = np.float64(0) update = gr.LinePlot.update( value=pd.DataFrame({"x": x, "y": y}), x="x", y="y", title= f"I mean = {iMean},Ipp = {iPp}" , width=500, height=350, y_lim= [ -100,100], y_title = 'mA' ) plot_end_2 += 2 * math.pi if plot_end_2 > 1000: plot_end_2 = 2 * math.pi return update def get_plot_3(period_3=1,xscale_3 = 1,yscale_3 = 1): global plot_end_3 x = np.arange(plot_end_3 - 2 * math.pi, plot_end_3, 0.02) y = 0 #y = np.sin(2 * math.pi * period_3 * x) xMean = 0 xPp = 0 #y = np.float64(0.5) update = gr.LinePlot.update( value=pd.DataFrame({"x": x, "y": y}), x="x", y="y", title= f" Mean Value = {xMean}, PP value = {xPp}" , width=600, height=350, y_lim= [ -4,4], y_title = 'V' ) plot_end_3 += 2 * math.pi if plot_end_3 > 1000: plot_end_3 = 2 * math.pi return update with gr.Blocks() as demo: with gr.Row(): with gr.Column(): gr.Textbox( "Voltage Channel 1", label="", ) period_v1 = gr.Slider( label="Period of plot", value=1, minimum=0, maximum=10, step=1 ) xscale_v1 = gr.Slider( label="xscale", value=1, minimum=0, maximum=10, step=1 ) yscale_v1 = gr.Slider( label="yscale", value=1, minimum=0, maximum=10, step=1 ) plot_v1 = gr.LinePlot(show_label= False ) with gr.Column(): gr.Textbox( "Current Channel 1", label="", ) period_i1 = gr.Slider( label="Period of plot", value=1, minimum=0, maximum=10, step=1 ) xscale_i1 = gr.Slider( label="xscale", value=1, minimum=0, maximum=10, step=1 ) yscale_i1 = gr.Slider( label="yscale", value=1, minimum=0, maximum=10, step=1 ) plot_i1 = gr.LinePlot(show_label= False ) with gr.Row(): with gr.Column(): #gr.inputs.Toggle(default=False, label="Select option A or B") gr.Dropdown( ["Voltage Channel 2","Current channel 2"], type = "index", multiselect= False, label="Select I or V") # gr.Textbox( # "I/V Channel 2", # label="", # ) period_3 = gr.Slider( label="Period of plot", value=1, minimum=0, maximum=10, step=1 ) xscale_3 = gr.Slider( label="xscale", value=1, minimum=0, maximum=10, step=1 ) yscale_3 = gr.Slider( label="yscale", value=1, minimum=0, maximum=10, step=1 ) plot_3 = gr.LinePlot(show_label= False ) demo.load(lambda: datetime.datetime.now(), None, every=1) dep1 = demo.load(get_plot_i1, None, plot_i1, every=1) period_i1.change(get_plot_i1, period_i1, plot_i1, every=1, cancels=[dep1]) xscale_i1.change(get_plot_i1, xscale_i1, plot_i1, every=1, cancels=[dep1]) yscale_i1.change(get_plot_i1, yscale_i1, plot_i1, every=1, cancels=[dep1]) demo.load(lambda: datetime.datetime.now(), None, every=1) dep = demo.load(get_plot_v1, None, plot_v1, every=1) period_v1.change(get_plot_v1, period_v1, plot_v1, every=1, cancels=[dep]) xscale_v1.change(get_plot_v1, xscale_v1, plot_v1, every=1, cancels=[dep]) yscale_v1.change(get_plot_v1, yscale_v1, plot_v1, every=1, cancels=[dep]) demo.load(lambda: datetime.datetime.now(), None, every=1) dep2 = demo.load(get_plot_3, None, plot_3, every=1) period_3.change(get_plot_3, period_3, plot_3, every=1, cancels=[dep2]) xscale_3.change(get_plot_3, xscale_3, plot_3, every=1, cancels=[dep2]) yscale_3.change(get_plot_3, yscale_3, plot_3, every=1, cancels=[dep2]) if __name__ == "__main__": demo.queue().launch(debug = True)