-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatavisualizationapp.py
More file actions
134 lines (110 loc) Β· 5.07 KB
/
Datavisualizationapp.py
File metadata and controls
134 lines (110 loc) Β· 5.07 KB
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
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
132
133
134
import streamlit as st
import pandas as pd
import plotly.express as px
import seaborn as sns
import matplotlib.pyplot as plt
# --- Page setup ---
st.set_page_config(page_title="Smart Data Visualizer", layout="wide")
st.title("π Smart Data Visualization Software (Advanced)")
st.markdown("Upload a dataset and explore powerful dynamic charts in one click!")
# --- Sidebar Header ---
st.sidebar.header("βοΈ Settings")
st.sidebar.write("Made for Business Intelligence, Market Analysis & π¬ Data Science")
# --- File upload ---
file = st.file_uploader("Upload your file (CSV, Excel, JSON, TXT)", type=['csv', 'xlsx', 'json', 'txt'])
# --- Load data ---
if file:
try:
# File info in sidebar
file_details = {
"π Filename": file.name,
"π Type": file.type,
"π Size (KB)": round(file.size / 1024, 2)
}
st.sidebar.subheader("π File Information")
st.sidebar.json(file_details)
# Read data based on extension
if file.name.endswith('.csv'):
df = pd.read_csv(file)
elif file.name.endswith('.xlsx'):
df = pd.read_excel(file)
elif file.name.endswith('.json'):
df = pd.read_json(file)
elif file.name.endswith('.txt'):
text = file.read().decode()
df = pd.DataFrame({'text': text.splitlines()})
else:
st.error("Unsupported file format.")
st.stop()
# Data summary in sidebar
st.sidebar.subheader("π Data Summary")
st.sidebar.write(f"**Rows:** {df.shape[0]}")
st.sidebar.write(f"**Columns:** {df.shape[1]}")
st.sidebar.write("**Column Types:**")
st.sidebar.write(df.dtypes.astype(str))
# Preview
st.subheader("π Data Preview")
st.dataframe(df.head())
numeric_cols = df.select_dtypes(include='number').columns.tolist()
categorical_cols = df.select_dtypes(exclude='number').columns.tolist()
all_cols = df.columns.tolist()
if all_cols:
st.subheader("π Select Chart Options")
chart_type = st.selectbox("Chart Type", [
"Scatter", "Line", "Bar", "Box", "Histogram", "Pie",
"Area", "Heatmap", "Violin", "Correlation Matrix", "KDE"
])
x_axis = st.selectbox("X-axis", all_cols, key="x_axis")
y_axis = st.selectbox("Y-axis", numeric_cols, key="y_axis") if chart_type not in ["Pie", "Histogram", "Correlation Matrix", "KDE"] else None
st.markdown("---")
# Chart rendering
st.subheader("π Chart Output")
if chart_type == "Scatter":
fig = px.scatter(df, x=x_axis, y=y_axis, color=categorical_cols[0] if categorical_cols else None)
st.plotly_chart(fig)
elif chart_type == "Line":
fig = px.line(df, x=x_axis, y=y_axis, color=categorical_cols[0] if categorical_cols else None)
st.plotly_chart(fig)
elif chart_type == "Bar":
fig = px.bar(df, x=x_axis, y=y_axis, color=categorical_cols[0] if categorical_cols else None)
st.plotly_chart(fig)
elif chart_type == "Box":
fig = px.box(df, x=x_axis, y=y_axis, color=categorical_cols[0] if categorical_cols else None)
st.plotly_chart(fig)
elif chart_type == "Histogram":
fig = px.histogram(df, x=x_axis)
st.plotly_chart(fig)
elif chart_type == "Pie":
if df[x_axis].nunique() <= 20:
pie_data = df[x_axis].value_counts().reset_index()
pie_data.columns = [x_axis, 'count']
fig = px.pie(pie_data, names=x_axis, values='count', title=f"{x_axis} Distribution")
st.plotly_chart(fig)
else:
st.warning("Too many unique values for pie chart.")
elif chart_type == "Area":
fig = px.area(df, x=x_axis, y=y_axis)
st.plotly_chart(fig)
elif chart_type == "Heatmap":
corr = df[numeric_cols].corr()
fig, ax = plt.subplots(figsize=(10, 6))
sns.heatmap(corr, annot=True, cmap="coolwarm", ax=ax)
st.pyplot(fig)
elif chart_type == "Violin":
fig = px.violin(df, x=x_axis, y=y_axis, box=True, points="all")
st.plotly_chart(fig)
elif chart_type == "Correlation Matrix":
st.subheader("π Correlation Matrix Table")
corr = df[numeric_cols].corr()
st.dataframe(corr)
fig, ax = plt.subplots(figsize=(10, 6))
sns.heatmap(corr, annot=True, cmap='viridis', ax=ax)
st.pyplot(fig)
elif chart_type == "KDE":
fig, ax = plt.subplots()
sns.kdeplot(data=df, x=x_axis, fill=True, ax=ax)
st.pyplot(fig)
except Exception as e:
st.error(f"β Error: {e}")
else:
st.info("π Upload a file from the sidebar to begin.")