Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,6 +3,7 @@ import pandas as pd
|
|
| 3 |
import seaborn as sns
|
| 4 |
import matplotlib.pyplot as plt
|
| 5 |
import numpy as np
|
|
|
|
| 6 |
|
| 7 |
# Function to upload the data from the user
|
| 8 |
def load_data():
|
|
@@ -30,125 +31,98 @@ def load_data():
|
|
| 30 |
# Function to calculate delay
|
| 31 |
def calculate_delay(data):
|
| 32 |
if 'ืฉืขืช ืืืฆืืข (ืืืืง)' in data.columns and 'ืฉืขืช ืืืขื (ืืืืง)' in data.columns:
|
| 33 |
-
# Ensure that the columns are converted to datetime if not already
|
| 34 |
data['ืฉืขืช ืืืฆืืข (ืืืืง)'] = pd.to_datetime(data['ืฉืขืช ืืืฆืืข (ืืืืง)'], errors='coerce')
|
| 35 |
data['ืฉืขืช ืืืขื (ืืืืง)'] = pd.to_datetime(data['ืฉืขืช ืืืขื (ืืืืง)'], errors='coerce')
|
| 36 |
-
|
| 37 |
-
# Calculate the delay in minutes
|
| 38 |
data['Delay'] = (data['ืฉืขืช ืืืขื (ืืืืง)'] - data['ืฉืขืช ืืืฆืืข (ืืืืง)']).dt.total_seconds() / 60
|
| 39 |
-
|
| 40 |
-
else:
|
| 41 |
-
st.warning("Columns for delay calculation are missing.")
|
| 42 |
-
return data
|
| 43 |
|
| 44 |
-
# Function
|
| 45 |
def bi_questions_analysis(data):
|
| 46 |
st.title("Business Intelligence Dashboard: 60 Questions and Plots")
|
| 47 |
|
| 48 |
-
#
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
st.subheader("10. Delay Distribution by Trip Status")
|
| 132 |
-
st.write(status_delay)
|
| 133 |
-
fig, ax = plt.subplots()
|
| 134 |
-
sns.barplot(x='ืกืืืืก ื ืกืืขื', y='Delay', data=status_delay, ax=ax)
|
| 135 |
-
ax.set_title("Average Delay by Trip Status")
|
| 136 |
-
st.pyplot(fig)
|
| 137 |
-
|
| 138 |
-
# Additional 50 plots (from 11 to 60)
|
| 139 |
-
for i in range(11, 61):
|
| 140 |
-
# Here, we will generate random or calculated plots based on data that may exist
|
| 141 |
-
# The below are just placeholders for additional plotting. You can adjust them as needed.
|
| 142 |
-
st.subheader(f"{i}. Sample Question {i}")
|
| 143 |
-
fig, ax = plt.subplots()
|
| 144 |
-
# Plotting with random data (or replace with real analysis logic)
|
| 145 |
-
sns.histplot(np.random.rand(100), kde=True, ax=ax)
|
| 146 |
-
ax.set_title(f"Sample Plot {i}")
|
| 147 |
-
st.pyplot(fig)
|
| 148 |
-
|
| 149 |
-
# The plots from 11 to 60 can be customized according to your needs.
|
| 150 |
-
# If you want to use specific attributes from the data for those plots, just adjust the logic accordingly.
|
| 151 |
-
|
| 152 |
# Main Page Navigation with buttons for each section
|
| 153 |
def main_page_navigation(data):
|
| 154 |
pages = {
|
|
|
|
| 3 |
import seaborn as sns
|
| 4 |
import matplotlib.pyplot as plt
|
| 5 |
import numpy as np
|
| 6 |
+
import random
|
| 7 |
|
| 8 |
# Function to upload the data from the user
|
| 9 |
def load_data():
|
|
|
|
| 31 |
# Function to calculate delay
|
| 32 |
def calculate_delay(data):
|
| 33 |
if 'ืฉืขืช ืืืฆืืข (ืืืืง)' in data.columns and 'ืฉืขืช ืืืขื (ืืืืง)' in data.columns:
|
|
|
|
| 34 |
data['ืฉืขืช ืืืฆืืข (ืืืืง)'] = pd.to_datetime(data['ืฉืขืช ืืืฆืืข (ืืืืง)'], errors='coerce')
|
| 35 |
data['ืฉืขืช ืืืขื (ืืืืง)'] = pd.to_datetime(data['ืฉืขืช ืืืขื (ืืืืง)'], errors='coerce')
|
|
|
|
|
|
|
| 36 |
data['Delay'] = (data['ืฉืขืช ืืืขื (ืืืืง)'] - data['ืฉืขืช ืืืฆืืข (ืืืืง)']).dt.total_seconds() / 60
|
| 37 |
+
return data
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
+
# Function for a generic set of BI questions and plots
|
| 40 |
def bi_questions_analysis(data):
|
| 41 |
st.title("Business Intelligence Dashboard: 60 Questions and Plots")
|
| 42 |
|
| 43 |
+
# Ensure Delay column is created
|
| 44 |
+
data = calculate_delay(data)
|
| 45 |
+
|
| 46 |
+
# Generic function to display random visualizations
|
| 47 |
+
def plot_random_visualization(i):
|
| 48 |
+
random_choice = random.choice(["bar", "line", "scatter", "box", "hist", "heatmap", "pie", "count"])
|
| 49 |
+
|
| 50 |
+
# Plot bar chart
|
| 51 |
+
if random_choice == "bar" and 'Delay' in data.columns:
|
| 52 |
+
st.subheader(f"Plot {i}: Bar Chart - Delay by Direction")
|
| 53 |
+
direction_delay = data.groupby('Direction')['Delay'].mean().reset_index()
|
| 54 |
+
fig, ax = plt.subplots()
|
| 55 |
+
sns.barplot(x='Direction', y='Delay', data=direction_delay, ax=ax)
|
| 56 |
+
ax.set_title(f"Delay by Direction")
|
| 57 |
+
st.pyplot(fig)
|
| 58 |
+
|
| 59 |
+
# Plot line chart
|
| 60 |
+
elif random_choice == "line" and 'Plantime' in data.columns and 'Delay' in data.columns:
|
| 61 |
+
st.subheader(f"Plot {i}: Line Chart - Delay over Time")
|
| 62 |
+
data['Plantime'] = pd.to_datetime(data['Plantime'], errors='coerce')
|
| 63 |
+
data['hour_of_day'] = data['Plantime'].dt.hour
|
| 64 |
+
daily_delay = data.groupby('hour_of_day')['Delay'].mean()
|
| 65 |
+
fig, ax = plt.subplots()
|
| 66 |
+
daily_delay.plot(kind="line", ax=ax)
|
| 67 |
+
ax.set_title(f"Delay over Time")
|
| 68 |
+
st.pyplot(fig)
|
| 69 |
+
|
| 70 |
+
# Plot scatter plot
|
| 71 |
+
elif random_choice == "scatter" and 'Delay' in data.columns:
|
| 72 |
+
st.subheader(f"Plot {i}: Scatter Plot - Delay vs. Plantime")
|
| 73 |
+
fig, ax = plt.subplots()
|
| 74 |
+
sns.scatterplot(x=data['Plantime'], y=data['Delay'], ax=ax)
|
| 75 |
+
ax.set_title(f"Delay vs. Plantime")
|
| 76 |
+
st.pyplot(fig)
|
| 77 |
+
|
| 78 |
+
# Plot histogram
|
| 79 |
+
elif random_choice == "hist" and 'Delay' in data.columns:
|
| 80 |
+
st.subheader(f"Plot {i}: Histogram - Distribution of Delay")
|
| 81 |
+
fig, ax = plt.subplots()
|
| 82 |
+
sns.histplot(data['Delay'], kde=True, ax=ax)
|
| 83 |
+
ax.set_title(f"Distribution of Delay")
|
| 84 |
+
st.pyplot(fig)
|
| 85 |
+
|
| 86 |
+
# Plot box plot
|
| 87 |
+
elif random_choice == "box" and 'Delay' in data.columns:
|
| 88 |
+
st.subheader(f"Plot {i}: Box Plot - Delay by Status")
|
| 89 |
+
fig, ax = plt.subplots()
|
| 90 |
+
sns.boxplot(x='ืกืืืืก', y='Delay', data=data, ax=ax)
|
| 91 |
+
ax.set_title(f"Delay by Status")
|
| 92 |
+
st.pyplot(fig)
|
| 93 |
+
|
| 94 |
+
# Plot pie chart
|
| 95 |
+
elif random_choice == "pie" and 'Delay' in data.columns:
|
| 96 |
+
st.subheader(f"Plot {i}: Pie Chart - Delay Categories")
|
| 97 |
+
delay_status = data['Delay'].apply(lambda x: 'Delayed' if x > 0 else 'On Time').value_counts()
|
| 98 |
+
fig, ax = plt.subplots()
|
| 99 |
+
delay_status.plot(kind="pie", ax=ax, autopct='%1.1f%%', startangle=90)
|
| 100 |
+
ax.set_title(f"Delay Categories")
|
| 101 |
+
st.pyplot(fig)
|
| 102 |
+
|
| 103 |
+
# Plot count plot
|
| 104 |
+
elif random_choice == "count" and 'ืกืืืืก' in data.columns:
|
| 105 |
+
st.subheader(f"Plot {i}: Count Plot - Status Frequency")
|
| 106 |
+
fig, ax = plt.subplots()
|
| 107 |
+
sns.countplot(x='ืกืืืืก', data=data, ax=ax)
|
| 108 |
+
ax.set_title(f"Frequency of Status")
|
| 109 |
+
st.pyplot(fig)
|
| 110 |
+
|
| 111 |
+
# Plot heatmap
|
| 112 |
+
elif random_choice == "heatmap":
|
| 113 |
+
st.subheader(f"Plot {i}: Heatmap of Correlations")
|
| 114 |
+
corr = data.corr()
|
| 115 |
+
fig, ax = plt.subplots(figsize=(10, 8))
|
| 116 |
+
sns.heatmap(corr, annot=True, fmt=".2f", cmap="coolwarm", ax=ax)
|
| 117 |
+
ax.set_title(f"Correlation Heatmap")
|
| 118 |
+
st.pyplot(fig)
|
| 119 |
+
|
| 120 |
+
# Loop through the 12 plots for each page
|
| 121 |
+
for page in range(1, 6):
|
| 122 |
+
st.header(f"Page {page} - 12 Plots")
|
| 123 |
+
for i in range((page - 1) * 12 + 1, page * 12 + 1):
|
| 124 |
+
plot_random_visualization(i)
|
| 125 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
# Main Page Navigation with buttons for each section
|
| 127 |
def main_page_navigation(data):
|
| 128 |
pages = {
|