import altair as alt
import pandas as pd
import solara
from vega_datasets import data
# Load the dataset from the provided file
file_path = 'merged_river_data.csv'
rivers = pd.read_csv(file_path)
# Print the column names to check their names
print("Column names in the dataset:", rivers.columns)
# Print the first few rows of the dataset for verification
print("Dataset loaded successfully.")
print(rivers.head())
# Ensure that the required columns are present
required_columns = ['XCoordinate', 'YCoordinate', 'Name', 'RiverNumber', 'ForecastData']
missing_columns = [col for col in required_columns if col not in rivers.columns]
if missing_columns:
raise ValueError(f"Missing columns in the dataset: {missing_columns}")
# Define the Solara component
@solara.component
def Page():
selected_datum = solara.reactive(None)
def on_click(event):
datum = event['datum'] # Access the datum directly from the event
print("Clicked datum:", datum) # Debug print statement
selected_datum.value = datum
# Load the geographic data for the background map
states = alt.topo_feature(data.us_10m.url, 'states')
# Create the background map
background = alt.Chart(states).mark_geoshape(
fill='lightgray',
stroke='white'
).properties(
width=800,
height=500
)
# Create the Altair chart for the river points
points = alt.Chart(rivers).mark_circle(size=100).encode(
longitude='XCoordinate:Q',
latitude='YCoordinate:Q',
tooltip=['Name:N', 'RiverNumber:Q', 'ForecastData:N'],
color='RiverNumber:Q'
).interactive()
# Combine the background map and the points
chart = background + points
with solara.Card("Rivers Data"):
solara.AltairChart(chart, on_click=on_click)
if selected_datum.value:
river_name = selected_datum.value['Name']
river_number = selected_datum.value['RiverNumber']
forecast_data = selected_datum.value['ForecastData']
solara.Markdown(f"**Selected River:** {river_name}\n\n**River Number:** {river_number}\n\n**Forecast Data:** {forecast_data}")
else:
solara.Markdown("Click on a river to see its details")