Update server.py
Browse files
server.py
CHANGED
|
@@ -1,11 +1,18 @@
|
|
| 1 |
# server.py
|
| 2 |
import matplotlib.pyplot as plt
|
| 3 |
-
from shiny import render, ui
|
| 4 |
import faicons as fa
|
| 5 |
#from fontawesomefree import icons
|
| 6 |
import numpy as np
|
| 7 |
from utils import conlogo, vendorlogo
|
| 8 |
from utils import create_pie_chart, create_bar_chart, create_bar_chart, create_stacked_bar_chart
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
def create_server(app_dir):
|
| 11 |
def server(input, output, session):
|
|
@@ -49,5 +56,72 @@ def create_server(app_dir):
|
|
| 49 |
out_napa = [2000, 4000, 2000, 5000, 1000]
|
| 50 |
|
| 51 |
return create_stacked_bar_chart(categories, intra_napa, into_napa, out_napa)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
return server
|
|
|
|
| 1 |
# server.py
|
| 2 |
import matplotlib.pyplot as plt
|
| 3 |
+
from shiny import render, ui, reactive
|
| 4 |
import faicons as fa
|
| 5 |
#from fontawesomefree import icons
|
| 6 |
import numpy as np
|
| 7 |
from utils import conlogo, vendorlogo
|
| 8 |
from utils import create_pie_chart, create_bar_chart, create_bar_chart, create_stacked_bar_chart
|
| 9 |
+
import plotly.express as px
|
| 10 |
+
from utils import load_data, filter_trips
|
| 11 |
+
from shinywidgets import output_widget, render_widget
|
| 12 |
+
import plotly.graph_objects as go
|
| 13 |
+
|
| 14 |
+
# Load the data
|
| 15 |
+
zones, trips = load_data()
|
| 16 |
|
| 17 |
def create_server(app_dir):
|
| 18 |
def server(input, output, session):
|
|
|
|
| 56 |
out_napa = [2000, 4000, 2000, 5000, 1000]
|
| 57 |
|
| 58 |
return create_stacked_bar_chart(categories, intra_napa, into_napa, out_napa)
|
| 59 |
+
|
| 60 |
+
# Reactive values to track selections
|
| 61 |
+
selected_origin = reactive.Value(None)
|
| 62 |
+
selected_destination = reactive.Value(None)
|
| 63 |
+
|
| 64 |
+
@reactive.Effect
|
| 65 |
+
#@reactive.event(input)
|
| 66 |
+
@reactive.event(input.origin_map_click, input.destination_map_click)
|
| 67 |
+
def update_selections():
|
| 68 |
+
"""Update selections when a map is clicked."""
|
| 69 |
+
selected_origin.set(input.origin_map_click.get("customdata"))
|
| 70 |
+
selected_destination.set(input.destination_map_click.get("customdata"))
|
| 71 |
+
|
| 72 |
+
@reactive.Calc
|
| 73 |
+
def filtered_data():
|
| 74 |
+
"""Filter trips based on selected origin or destination."""
|
| 75 |
+
return filter_trips(trips, zones, selected_origin.get(), selected_destination.get())
|
| 76 |
+
|
| 77 |
+
@output
|
| 78 |
+
#@render.plot
|
| 79 |
+
#@render.ui
|
| 80 |
+
@render_widget
|
| 81 |
+
def origin_map():
|
| 82 |
+
filtered_origins, _ = filtered_data()
|
| 83 |
+
fig = px.choropleth(
|
| 84 |
+
filtered_origins,
|
| 85 |
+
geojson=filtered_origins.geometry,
|
| 86 |
+
locations=filtered_origins.index,
|
| 87 |
+
color="trip_count",
|
| 88 |
+
title="Trip Origins",
|
| 89 |
+
custom_data=["FPID"],
|
| 90 |
+
#mapbox_style="open-street-map",
|
| 91 |
+
#zoom=7.5,
|
| 92 |
+
#center={"lat": filtered_origins.geometry.centroid.y.mean(), "lon": filtered_origins.geometry.centroid.x.mean()},
|
| 93 |
+
#opacity=0.7,
|
| 94 |
+
# color_continuous_scale="amp",
|
| 95 |
+
# labels={
|
| 96 |
+
# "Zone ID": "FPID",
|
| 97 |
+
# "Trips": "trip_count"
|
| 98 |
+
# }
|
| 99 |
+
)
|
| 100 |
+
fig.update_geos(fitbounds="locations", visible=False)
|
| 101 |
+
#fig.update_layout(coloraxis_colorbar=dict(title="Trip Count")) # Add legend
|
| 102 |
+
return fig
|
| 103 |
+
#return ui.HTML(fig.to_html())
|
| 104 |
+
#return ui.HTML(fig.to_html(full_html=False, include_plotlyjs="cdn"))
|
| 105 |
+
|
| 106 |
+
@output
|
| 107 |
+
#@render.plot
|
| 108 |
+
#@render.ui
|
| 109 |
+
@render_widget
|
| 110 |
+
def destination_map():
|
| 111 |
+
_, filtered_destinations = filtered_data()
|
| 112 |
+
fig = px.choropleth(
|
| 113 |
+
filtered_destinations,
|
| 114 |
+
geojson=filtered_destinations.geometry,
|
| 115 |
+
locations=filtered_destinations.index,
|
| 116 |
+
color="trip_count",
|
| 117 |
+
title="Trip Destinations",
|
| 118 |
+
custom_data=["FPID"]
|
| 119 |
+
)
|
| 120 |
+
fig.update_geos(fitbounds="locations", visible=False)
|
| 121 |
+
#fig.update_layout(coloraxis_colorbar=dict(title="Trip Count")) # Add legend
|
| 122 |
+
return fig
|
| 123 |
+
#return ui.HTML(fig.to_html())
|
| 124 |
+
#return ui.HTML(fig.to_html(full_html=False, include_plotlyjs="cdn"))
|
| 125 |
+
|
| 126 |
|
| 127 |
return server
|