gradio/demo/blocks_kinematics/run.py
Freddy Boulton 8aba1c991b
Add info to docs on how to set default values for components (#1788)
* Explicitly set value and add to docs

* Fix wording a bit

* Remove sentence that's not adding much

* Remove extra word

* Delete screenshot
2022-07-19 11:16:56 -04:00

41 lines
901 B
Python

import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import numpy as np
import gradio as gr
def plot(v, a):
g = 9.81
theta = a / 180 * 3.14
tmax = ((2 * v) * np.sin(theta)) / g
timemat = tmax * np.linspace(0, 1, 40)[:, None]
x = (v * timemat) * np.cos(theta)
y = ((v * timemat) * np.sin(theta)) - ((0.5 * g) * (timemat**2))
fig = plt.figure()
plt.scatter(x=x, y=y, marker=".")
plt.xlim(0, 100)
plt.ylim(0, 60)
return fig
demo = gr.Blocks()
with demo:
gr.Markdown(
"Let's do some kinematics! Choose the speed and angle to see the trajectory."
)
with gr.Row():
speed = gr.Slider(1, 30, 25, label="Speed")
angle = gr.Slider(0, 90, 45, label="Angle")
output = gr.Plot()
btn = gr.Button(value="Run")
btn.click(plot, [speed, angle], output)
if __name__ == "__main__":
demo.launch()