| |
| import matplotlib.pyplot as plt |
| from shiny import render, ui, reactive |
| import faicons as fa |
| |
| import numpy as np |
| from utils import conlogo, vendorlogo |
| from utils import create_pie_chart, create_bar_chart, create_bar_chart, create_stacked_bar_chart |
| import plotly.express as px |
| from utils import load_data, filter_trips |
| from shinywidgets import output_widget, render_widget |
| import plotly.graph_objects as go |
| import pandas as pd |
| import json |
|
|
|
|
| |
| zones, trips = load_data() |
|
|
| |
| EXCEL_FILE = "data/trips_data.xlsx" |
| Json_title_FILE = "data/old_table_title.json" |
| Json_table_FILE = "data/old_table.json" |
| Q1_FILE = "data/q1.json" |
| Q2_FILE = "data/q2.json" |
| Q3_FILE = "data/q3.json" |
| Q4_FILE = "data/q4.json" |
|
|
|
|
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
|
|
| |
| with open(Json_table_FILE, "r") as file: |
| tables_json = json.load(file) |
|
|
| |
| with open(Json_title_FILE, "r") as file: |
| tables_title_json = json.load(file) |
|
|
| |
| with open(Q1_FILE, "r") as file: |
| q1 = json.load(file) |
|
|
| |
| with open(Q2_FILE, "r") as file: |
| q2 = json.load(file) |
|
|
| |
| with open(Q3_FILE, "r") as file: |
| q3 = json.load(file) |
|
|
| |
| with open(Q4_FILE, "r") as file: |
| q4 = json.load(file) |
|
|
|
|
| def create_server(app_dir): |
| def server(input, output, session): |
|
|
| @render.image |
| def rsglogo(): |
| return conlogo() |
| |
| @render.image |
| def clientlogo(): |
| return vendorlogo() |
| |
| @render.plot |
| def bar_chart(): |
| |
| categories = ["Category A", "Category B", "Category C"] |
| values = [10, 20, 30] |
|
|
| return create_bar_chart(categories, values, plottitle = "Bar Chart Example") |
|
|
| @render.plot |
| def pie_chart(): |
| |
| labels = ["Home-Based Work", "Home-Based Other", "Non-Home-Based"] |
| |
| |
| |
|
|
| sizes = [22, 41, 37] |
| colors = ["#00b3b3", "#70d281", "#ff7f0e"] |
| explode = (0.1, 0, 0) |
| |
| return create_pie_chart(labels, sizes, plottitle ="What Types of Trips are Occuring within Napa County on a Weekday?") |
|
|
| @render.plot |
| def stacked_bar_chart(): |
| |
| categories = ["Early AM", "AM Peak", "Mid-Day", "PM Peak", "Evening"] |
| intra_napa = [2000, 16000, 14000, 12000, 5000] |
| into_napa = [1000, 8000, 2000, 3000, 2000] |
| out_napa = [2000, 4000, 2000, 5000, 1000] |
|
|
| return create_stacked_bar_chart(categories, intra_napa, into_napa, out_napa) |
| |
| |
| selected_origin = reactive.Value(None) |
| selected_destination = reactive.Value(None) |
|
|
| @reactive.Effect |
| |
| @reactive.event(input.origin_map_click, input.destination_map_click) |
| def update_selections(): |
| """Update selections when a map is clicked.""" |
| selected_origin.set(input.origin_map_click.get("customdata")) |
| selected_destination.set(input.destination_map_click.get("customdata")) |
|
|
| @reactive.Calc |
| def filtered_data(): |
| """Filter trips based on selected origin or destination.""" |
| return filter_trips(trips, zones, selected_origin.get(), selected_destination.get()) |
|
|
| @output |
| |
| |
| @render_widget |
| def origin_map(): |
| filtered_origins, _ = filtered_data() |
| fig = px.choropleth( |
| filtered_origins, |
| geojson=filtered_origins.geometry, |
| locations=filtered_origins.index, |
| color="trip_count", |
| title="Trip Origins", |
| custom_data=["FPID"], |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| ) |
| fig.update_geos(fitbounds="locations", visible=False) |
| |
| return fig |
| |
| |
|
|
| @output |
| |
| |
| @render_widget |
| def destination_map(): |
| _, filtered_destinations = filtered_data() |
| fig = px.choropleth( |
| filtered_destinations, |
| geojson=filtered_destinations.geometry, |
| locations=filtered_destinations.index, |
| color="trip_count", |
| title="Trip Destinations", |
| custom_data=["FPID"] |
| ) |
| fig.update_geos(fitbounds="locations", visible=False) |
| |
| return fig |
| |
| |
|
|
| @reactive.Calc |
| def selected_sheet1(): |
| """Get the corresponding sheet name for the selected question.""" |
| question1 = input.question_selector1() |
| return q1.get(question1, None) |
| |
| @reactive.Calc |
| def sheet_data1(): |
| sheet_name = selected_sheet1() |
| data = pd.DataFrame(tables_json[sheet_name]) |
| title = tables_title_json[sheet_name] |
| return {"title": title, "data": data} |
|
|
| |
| @output |
| @render.ui |
| def table_title1(): |
| title = sheet_data1()["title"] |
| return ui.tags.h5(title, style="text-align: center; margin-top: 20px;") |
|
|
| |
| @output |
| @render.table |
| def trip_table1(): |
| data = sheet_data1()["data"] |
| if data.empty: |
| return pd.DataFrame({"Error": ["No data available"]}) |
| |
| return data.reset_index(drop=True).style.bar(color='#52bf89') |
|
|
| @reactive.Calc |
| def selected_sheet2(): |
| """Get the corresponding sheet name for the selected question.""" |
| question2 = input.question_selector2() |
| return q2.get(question2, None) |
| |
| @reactive.Calc |
| def sheet_data2(): |
| sheet_name = selected_sheet2() |
| data = pd.DataFrame(tables_json[sheet_name]) |
| title = tables_title_json[sheet_name] |
| return {"title": title, "data": data} |
|
|
| |
| @output |
| @render.ui |
| def table_title2(): |
| title = sheet_data2()["title"] |
| return ui.tags.h5(title, style="text-align: center; margin-top: 20px;") |
|
|
| |
| @output |
| @render.table |
| def trip_table2(): |
| data = sheet_data2()["data"] |
| if data.empty: |
| return pd.DataFrame({"Error": ["No data available"]}) |
| |
| return data.reset_index(drop=True).style.bar(color='#52bf89') |
|
|
| @reactive.Calc |
| def selected_sheet3(): |
| """Get the corresponding sheet name for the selected question.""" |
| question3 = input.question_selector3() |
| return q3.get(question3, None) |
| |
| @reactive.Calc |
| def sheet_data3(): |
| sheet_name = selected_sheet3() |
| data = pd.DataFrame(tables_json[sheet_name]) |
| title = tables_title_json[sheet_name] |
| return {"title": title, "data": data} |
|
|
| |
| @output |
| @render.ui |
| def table_title3(): |
| title = sheet_data3()["title"] |
| return ui.tags.h5(title, style="text-align: center; margin-top: 20px;") |
|
|
| |
| @output |
| @render.table |
| def trip_table3(): |
| data = sheet_data3()["data"] |
| if data.empty: |
| return pd.DataFrame({"Error": ["No data available"]}) |
| |
| return data.reset_index(drop=True).style.bar(color='#52bf89') |
|
|
| @reactive.Calc |
| def selected_sheet4(): |
| """Get the corresponding sheet name for the selected question.""" |
| question4 = input.question_selector4() |
| return q4.get(question4, None) |
| |
| @reactive.Calc |
| def sheet_data4(): |
| sheet_name = selected_sheet4() |
| data = pd.DataFrame(tables_json[sheet_name]) |
| title = tables_title_json[sheet_name] |
| return {"title": title, "data": data} |
|
|
| |
| @output |
| @render.ui |
| def table_title4(): |
| title = sheet_data4()["title"] |
| return ui.tags.h5(title, style="text-align: center; margin-top: 20px;") |
|
|
| |
| @output |
| @render.table |
| def trip_table4(): |
| data = sheet_data4()["data"] |
| if data.empty: |
| return pd.DataFrame({"Error": ["No data available"]}) |
| |
| return data.reset_index(drop=True).style.bar(color='#52bf89') |
| |
| return server |
|
|