Update utils.py
Browse files
utils.py
CHANGED
|
@@ -1,18 +1,104 @@
|
|
| 1 |
-
from pathlib import Path
|
| 2 |
-
#import pandas as pd
|
| 3 |
-
import faicons as fa
|
| 4 |
-
from shiny import ui
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pathlib import Path
|
| 2 |
+
#import pandas as pd
|
| 3 |
+
import faicons as fa
|
| 4 |
+
from shiny import ui
|
| 5 |
+
from shared import app_dir
|
| 6 |
+
import matplotlib.pyplot as plt
|
| 7 |
+
import numpy as np
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
#app_dir = Path(__file__).parent
|
| 11 |
+
#tips = pd.read_csv(app_dir / "tips.csv")
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def create_nav_button(button_icon, button_label_text, button_link, button_cls = "btn btn-primary"):
|
| 15 |
+
return ui.tags.a(
|
| 16 |
+
ui.HTML(f"{fa.icon_svg(button_icon)} {button_label_text}"),
|
| 17 |
+
href=button_link,
|
| 18 |
+
class_=button_cls
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
def conlogo():
|
| 22 |
+
img = {
|
| 23 |
+
"src": app_dir / "images" / "logo.png",
|
| 24 |
+
"style": "width: 80%; height: auto; max-height: 150px; margin-bottom: 0px;"
|
| 25 |
+
}
|
| 26 |
+
return img
|
| 27 |
+
|
| 28 |
+
def vendorlogo():
|
| 29 |
+
img = {
|
| 30 |
+
"src": app_dir / "images" / "logo2.png",
|
| 31 |
+
"style": "width: 80%; height: auto; max-height: 150px; margin-bottom: 0px;"
|
| 32 |
+
}
|
| 33 |
+
return img
|
| 34 |
+
|
| 35 |
+
def create_bar_chart(categories, values, plottitle = "Bar Chart Example"):
|
| 36 |
+
|
| 37 |
+
fig, ax = plt.subplots()
|
| 38 |
+
#ax.bar(categories, values, color=["#1f77b4", "#ff7f0e", "#2ca02c"])
|
| 39 |
+
ax.bar(categories, values)
|
| 40 |
+
ax.set_title(plottitle)
|
| 41 |
+
ax.set_ylabel("Values")
|
| 42 |
+
ax.set_xlabel("Categories")
|
| 43 |
+
|
| 44 |
+
return fig
|
| 45 |
+
|
| 46 |
+
def create_pie_chart(labels, sizes, plottitle):
|
| 47 |
+
#sizes = [22, 41, 37]
|
| 48 |
+
#colors = ["#00b3b3", "#70d281", "#ff7f0e"]
|
| 49 |
+
#explode = (0.1, 0, 0) # Explode the first slice for emphasis
|
| 50 |
+
|
| 51 |
+
fig, ax = plt.subplots()
|
| 52 |
+
wedges, texts, autotexts = ax.pie(
|
| 53 |
+
sizes,
|
| 54 |
+
#explode=explode,
|
| 55 |
+
labels=labels,
|
| 56 |
+
autopct="%1.0f%%",
|
| 57 |
+
startangle=90,
|
| 58 |
+
#colors=colors,
|
| 59 |
+
textprops=dict(color="black"),
|
| 60 |
+
wedgeprops=dict(width=0.4) # Adjust width for the donut effect
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
# Customizing the labels
|
| 64 |
+
for text, label in zip(autotexts, labels):
|
| 65 |
+
text.set_color("black")
|
| 66 |
+
text.set_fontsize(12)
|
| 67 |
+
|
| 68 |
+
#ax.set_title("What Types of Trips are Occuring within Napa County on a Weekday?", fontsize=16, fontweight="bold")
|
| 69 |
+
ax.set_title(plottitle, fontsize=16, fontweight="bold")
|
| 70 |
+
return fig
|
| 71 |
+
|
| 72 |
+
def create_stacked_bar_chart(categories, intra_napa, into_napa, out_napa):
|
| 73 |
+
# Data for the stacked bar chart
|
| 74 |
+
#categories = ["Early AM", "AM Peak", "Mid-Day", "PM Peak", "Evening"]
|
| 75 |
+
#intra_napa = [2000, 16000, 14000, 12000, 5000]
|
| 76 |
+
#into_napa = [1000, 8000, 2000, 3000, 2000]
|
| 77 |
+
#out_napa = [2000, 4000, 2000, 5000, 1000]
|
| 78 |
+
|
| 79 |
+
bar_width = 0.5 # Width of the bars
|
| 80 |
+
x = np.arange(len(categories)) # x-axis positions
|
| 81 |
+
|
| 82 |
+
fig, ax = plt.subplots(figsize=(8, 6))
|
| 83 |
+
|
| 84 |
+
# Stacking the bars
|
| 85 |
+
ax.bar(x, intra_napa, bar_width, label="Intra-Napa County", color="#70d281")
|
| 86 |
+
ax.bar(x, into_napa, bar_width, bottom=intra_napa, label="Into Napa County", color="#ff7f0e")
|
| 87 |
+
ax.bar(x, out_napa, bar_width, bottom=np.array(intra_napa) + np.array(into_napa), label="Out of Napa County", color="#00b3b3")
|
| 88 |
+
|
| 89 |
+
# Customizing the plot
|
| 90 |
+
ax.set_xticks(x)
|
| 91 |
+
ax.set_xticklabels(categories)
|
| 92 |
+
ax.set_ylabel("Trips")
|
| 93 |
+
ax.set_title("Weekday Work Trip Types")
|
| 94 |
+
ax.legend(loc="upper right")
|
| 95 |
+
|
| 96 |
+
# Add value annotations
|
| 97 |
+
for i in range(len(categories)):
|
| 98 |
+
ax.text(x[i], intra_napa[i] / 2, f"{intra_napa[i]}", ha="center", va="center", color="white")
|
| 99 |
+
ax.text(x[i], intra_napa[i] + into_napa[i] / 2, f"{into_napa[i]}", ha="center", va="center", color="white")
|
| 100 |
+
ax.text(x[i], intra_napa[i] + into_napa[i] + out_napa[i] / 2, f"{out_napa[i]}", ha="center", va="center", color="white")
|
| 101 |
+
|
| 102 |
+
plt.tight_layout()
|
| 103 |
+
return fig
|
| 104 |
+
|