i am a student looking to build an app that allow users to input the values for List A and B through the app interface and display the plot of A vs B on the interface using plotly or matplotlib ( my 2nd year project)
i have been trying this an it is not working out.
can you put me through this or write the code for me
need urgent assistance, thanks
Thanks for quick response
here is the link for the app
when the plot button is clicked, it should display a chart of the plot on the interface, but i am confused on how to do this
It looks like you are trying to use javascript, css and html, which defeats the purpose of using Anvil, where everything is easier and you can do all in Python.
You should start working on a tutorial, so you can see how Anvil works.
Iām not familiar with the tutorials, but Iām guessing the dashboard tutorials in this page should be helpful for you.
Thanks for your support
i have used the anvil drag and drop for the interface and define the client sever code and server module all in python.
but iām encountering an error while running the app:
here is the client server code:
from anvil import *
import anvil.server
class Form1(Form): # Ensure that your main form is named Form1
def __init__(self, **properties):
self.init_components(**properties)
self.plot_1 = Plot(component_type="div", show_marker=True)
self.add_component(self.plot_1, slot="plot", visible=False)
def button_plot_click(self, **event_args):
list_a = [float(val.strip()) for val in self.text_box_a.text.split(",")]
list_b = [float(val.strip()) for val in self.text_box_b.text.split(",")]
self.call_server("generate_plot", list_a, list_b)
def display_plot(self, plot):
self.plot_1.visible = True
self.plot_1.plot(plot)
def text_box_a_change(self, **event_args):
self.plot_1.visible = False
def text_box_b_change(self, **event_args):
self.plot_1.visible = False
Here is the code for the server module:
import plotly.graph_objs as go
# Server function to generate the plot
@anvil.server.callable
def generate_plot(list_a, list_b):
# Generate the plot using Plotly
plot = go.Figure(data=go.Scatter(x=list_a, y=list_b, mode='markers'))
return plot.to_html(full_html=False)
I published the app to check again, it says unhandled runtime error
Hereās the app link
The client server code:
# form_code.py
from ._anvil_designer import Form1Template
from anvil import Plot, server
class Form1(Form1Template):
def __init__(self, **properties):
self.init_components(**properties)
self.custom_plot = Plot()
self.custom_plot.visible = False
self.add_component(self.custom_plot, slot="plot")
def button_plot_click(self, **event_args):
list_a = [float(val.strip()) for val in self.text_box_a.text.split(",")]
list_b = [float(val.strip()) for val in self.text_box_b.text.split(",")]
# Use anvil.server.call to call the server function
plot_data = server.call("generate_plot", list_a, list_b)
# Call the display_plot method with the received plot data
self.display_plot(plot_data)
def display_plot(self, plot):
self.custom_plot.visible = True
self.custom_plot.data = plot
def text_box_a_change(self, **event_args):
self.custom_plot.visible = False
def text_box_b_change(self, **event_args):
self.custom_plot.visible = False
Server module
# server_module.py
import anvil.server
import plotly.express as px
@anvil.server.callable
def generate_plot(list_a, list_b):
# Ensure that list_a and list_b are lists of numbers
try:
list_a = [float(x) for x in list_a]
list_b = [float(x) for x in list_b]
except ValueError:
raise ValueError("Lists should contain only numeric values")
# Create a plot using Plotly Express
fig = px.scatter(x=list_a, y=list_b, labels={'x': 'List A', 'y': 'List B'})
# Return the Plotly figure as JSON
return fig.to_json()
you should find these docs helpful
returning the figure and setting the .figure on the Plot is what you want
Note: The link you provided above is a link to the live app rather than a clone link. Itās more useful for the community to get a clone link to the app, that way the community can take a look at the source code and make changes themselves, before suggesting solutions.