I have been struggling to make this button work for my data analyze website. client code below
from anvil import *
import anvil.server
import plotly.graph_objects as go
from ._anvil_designer import Form1Template
class Form1(Form1Template):
def __init__(self, **properties):
# Initialize the form and set up variables
self.init_components(**properties)
self.columns = None # To store column names from the dataset
print("Form initialized.") # Debugging
def file_loader_1_change(self, file, **event_args):
"""Triggered when a file is uploaded."""
if file:
try:
# Call the server to process the file and retrieve column names
self.columns = anvil.server.call('process_file', file)
alert("Dataset uploaded successfully!", title="Success")
print(f"Columns in dataset: {self.columns}") # Debugging
except Exception as e:
alert(f"Error uploading dataset: {e}", title="Error")
print(f"Error: {e}") # Debugging
def analyze_button_click(self, **event_args):
"""Analyze a specific row of the dataset."""
print("Analyze button clicked!") # Debugging
alert("Analyze button clicked!", title="Debug")
if not self.columns:
alert("Please upload a dataset first.", title="Error")
return
row_index = input("Enter the row index to analyze:")
if not row_index.isdigit():
alert("Invalid row index. Please enter a valid integer.", title="Error")
return
try:
row_index = int(row_index)
row_data = anvil.server.call('get_row_data', row_index)
print(f"Row data: {row_data}") # Debugging
message = "\n".join([f"{col}: {value}" for col, value in zip(self.columns, row_data)])
alert(message, title=f"Data for Row {row_index}")
except Exception as e:
alert(f"Error analyzing row: {e}", title="Error")
print(f"Error: {e}") # Debugging
def display_button_click(self, **event_args):
"""Compare two variables visually."""
print("Display button clicked!") # Debugging
alert("Display button clicked!", title="Debug")
if not self.columns:
alert("Please upload a dataset first.", title="Error")
return
var1 = input(f"Enter the first variable to compare ({', '.join(self.columns)}):")
var2 = input(f"Enter the second variable to compare ({', '.join(self.columns)}):")
if var1 not in self.columns or var2 not in self.columns:
alert("Invalid column names. Please choose from the available columns.", title="Error")
return
try:
plot_data = anvil.server.call('get_comparison_data', var1, var2)
print(f"Plot data received: {plot_data}") # Debugging
fig = go.Figure(data=go.Scatter(x=plot_data["x"], y=plot_data["y"], mode="markers"))
fig.update_layout(title=f"Comparison of '{var1}' and '{var2}'",
xaxis_title=var1, yaxis_title=var2)
self.plot_1.data = fig
except Exception as e:
alert(f"Error displaying comparison: {e}", title="Error")
print(f"Error: {e}") # Debugging
server code below
import pandas as pd
from anvil import Media
import anvil.server
import pandas as pd
import io
# Global storage for the DataFrame
df_store = {}
@anvil.server.callable
def process_file(file):
"""Reads the uploaded file and stores it in memory."""
with io.BytesIO(file.get_bytes()) as file_content:
df = pd.read_csv(file_content)
df_store['df'] = df
return list(df.columns) # Return column names for user selection
@anvil.server.callable
def get_row_data(row_index):
"""Fetches data for a specific row."""
df = df_store.get('df')
if df is None:
raise Exception("No dataset uploaded.")
if row_index < 0 or row_index >= len(df):
raise Exception("Row index out of range.")
return df.iloc[row_index].tolist()
@anvil.server.callable
def get_comparison_data(var1, var2):
"""Fetches data for two variables for comparison."""
df = df_store.get('df')
if df is None:
raise Exception("No dataset uploaded.")
if var1 not in df.columns or var2 not in df.columns:
raise Exception("Invalid column names.")
return {"x": df[var1].tolist(), "y": df[var2].tolist()}
its just not working and i dont know why