forked from AIESEC-LK/analytics-starter-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
102 lines (81 loc) · 3.27 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
import streamlit as st
import pandas as pd
import matplotlib.pyplot as plt
# import plotly.express as px
# ------------------------- Fetch Data from Google Sheets -------------------------
@st.cache_data
def fetch_google_sheet_data(sheet_url):
"""
Fetches data from a public Google Sheet and returns it as a DataFrame.
:param sheet_url: URL of the public Google Sheet CSV export link.
:return: pandas DataFrame.
"""
return pd.read_csv(sheet_url)
# ------------------------- Functions for Data Operations -------------------------
def show_pie_chart(df: pd.DataFrame, column_name: str):
"""
Displays a pie chart for the specified column in a pandas DataFrame.
Args:
df (pd.DataFrame): The DataFrame containing the data.
column_name (str): The column name for which to create a pie chart.
"""
if column_name not in df.columns:
st.error(f"Column '{column_name}' not found in the DataFrame.")
return
# Count the occurrences of each unique value in the specified column
value_counts = df[column_name].value_counts()
if value_counts.empty:
st.warning("The column has no data to display.")
return
# Create the pie chart
fig, ax = plt.subplots()
ax.pie(value_counts, labels=value_counts.index, autopct='%1.1f%%', startangle=90)
ax.axis('equal') # Equal aspect ratio ensures that the pie is drawn as a circle.
# Display the chart in Streamlit
st.pyplot(fig)
def show_line_chart(df: pd.DataFrame, column_name: str):
"""
Displays a line chart for the specified column in a pandas DataFrame.
Args:
df (pd.DataFrame): The DataFrame containing the data.
column_name (str): The column name for which to create a line chart.
"""
if column_name not in df.columns:
st.error(f"Column '{column_name}' not found in the DataFrame.")
return
# Check if the column contains numeric data
if not pd.api.types.is_numeric_dtype(df[column_name]):
st.error(f"Column '{column_name}' is not numeric and cannot be used for a line chart.")
return
# Create the line chart
fig, ax = plt.subplots()
ax.plot(df[column_name], marker='o')
ax.set_title(f"Line Chart for {column_name}")
ax.set_xlabel("Index")
ax.set_ylabel(column_name)
# Display the chart in Streamlit
st.pyplot(fig)
def show_bar_chart(df: pd.DataFrame, column_name: str):
"""
Displays a bar chart for the specified column in a pandas DataFrame.
Args:
df (pd.DataFrame): The DataFrame containing the data.
column_name (str): The column name for which to create a bar chart.
"""
if column_name not in df.columns:
st.error(f"Column '{column_name}' not found in the DataFrame.")
return
# Count the occurrences of each unique value in the specified column
value_counts = df[column_name].value_counts()
if value_counts.empty:
st.warning("The column has no data to display.")
return
# Create the bar chart
fig, ax = plt.subplots()
ax.bar(value_counts.index, value_counts, color='skyblue')
ax.set_title(f"Bar Chart for {column_name}")
ax.set_xlabel("Categories")
ax.set_ylabel("Counts")
plt.xticks(rotation=45)
# Display the chart in Streamlit
st.pyplot(fig)