yashm commited on
Commit
e2cd54f
·
verified ·
1 Parent(s): c50b1c5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -35
app.py CHANGED
@@ -1,98 +1,142 @@
1
  import streamlit as st
2
- import pandas as pd
3
- import seaborn as sns
4
- import plotly.express as px
5
- import os
6
 
7
  # Predefined analysis tasks and visualization types
8
  PREDEFINED_ANALYSIS = {
9
  "Basic Statistics": {
10
  "Description": "Generate basic statistics summary for the dataset.",
11
- "Code": "st.write(df.describe())"
 
 
 
 
12
  },
13
  "Correlation Heatmap": {
14
  "Description": "Generate a correlation heatmap for numeric columns.",
15
- "Code": "st.write(df.corr())\nst.write(sns.heatmap(df.corr(), annot=True, cmap='coolwarm'))"
 
 
 
 
 
 
16
  },
17
  "Histogram": {
18
  "Description": "Generate a histogram for a selected numeric column.",
 
19
  "Code": """
20
- selected_column = st.selectbox("Select a numeric column for the histogram", df.select_dtypes(include='number').columns)
21
- st.write(px.histogram(df, x=selected_column))
 
 
22
  """
23
  },
24
  "Box Plot": {
25
  "Description": "Generate a box plot for a selected numeric column.",
 
26
  "Code": """
27
- selected_column = st.selectbox("Select a numeric column for the box plot", df.select_dtypes(include='number').columns)
28
- st.write(px.box(df, y=selected_column))
 
 
29
  """
30
  },
31
  "Scatter Plot": {
32
  "Description": "Generate a scatter plot for two selected numeric columns.",
 
33
  "Code": """
34
- x_column = st.selectbox("Select X-axis column", df.select_dtypes(include='number').columns)
35
- y_column = st.selectbox("Select Y-axis column", df.select_dtypes(include='number').columns)
36
- st.write(px.scatter(df, x=x_column, y=y_column))
 
 
37
  """
38
  },
39
  "Line Plot": {
40
  "Description": "Generate a line plot for a selected numeric column.",
 
41
  "Code": """
42
- selected_column = st.selectbox("Select a numeric column for the line plot", df.select_dtypes(include='number').columns)
43
- st.write(px.line(df, x=df.index, y=selected_column))
 
 
44
  """
45
  },
46
  "Bar Chart": {
47
  "Description": "Generate a bar chart for a selected categorical column.",
 
48
  "Code": """
49
- selected_column = st.selectbox("Select a categorical column for the bar chart", df.select_dtypes(include='object').columns)
50
- st.write(px.bar(df, x=selected_column))
 
 
51
  """
52
  },
53
  "Pair Plot": {
54
  "Description": "Generate a pair plot for pairwise relationships between numeric columns.",
55
- "Code": "st.write(sns.pairplot(df))"
 
 
 
 
 
56
  },
57
  "Distribution Plot": {
58
  "Description": "Generate a distribution plot for a selected numeric column.",
 
59
  "Code": """
60
- selected_column = st.selectbox("Select a numeric column for the distribution plot", df.select_dtypes(include='number').columns)
61
- st.write(sns.displot(df[selected_column], kde=True))
 
 
62
  """
63
  },
64
  "Count Plot": {
65
  "Description": "Generate a count plot for a selected categorical column.",
 
66
  "Code": """
67
- selected_column = st.selectbox("Select a categorical column for the count plot", df.select_dtypes(include='object').columns)
68
- st.write(sns.countplot(data=df, x=selected_column))
 
 
69
  """
70
  },
71
  "Pie Chart": {
72
  "Description": "Generate a pie chart for a selected categorical column.",
 
73
  "Code": """
74
- selected_column = st.selectbox("Select a categorical column for the pie chart", df.select_dtypes(include='object').columns)
75
- st.write(px.pie(df, names=selected_column))
 
 
76
  """
77
  },
78
  "Area Plot": {
79
  "Description": "Generate an area plot for a selected numeric column.",
 
80
  "Code": """
81
- selected_column = st.selectbox("Select a numeric column for the area plot", df.select_dtypes(include='number').columns)
82
- st.write(px.area(df, x=df.index, y=selected_column))
 
 
83
  """
84
  },
85
  "Violin Plot": {
86
  "Description": "Generate a violin plot for a selected numeric column.",
 
87
  "Code": """
88
- selected_column = st.selectbox("Select a numeric column for the violin plot", df.select_dtypes(include='number').columns)
89
- st.write(px.violin(df, y=selected_column))
 
 
90
  """
91
  },
92
  }
93
 
94
  def generate_streamlit_app_code(app_title, app_subtitle, side_panel_title, analysis_tasks, requirements):
95
  # Generate Python code for the Streamlit app
 
 
 
96
  code = f"""
97
  import streamlit as st
98
  import pandas as pd
@@ -115,7 +159,9 @@ def main():
115
 
116
  df = load_data(uploaded_file)
117
 
118
- {analysis_tasks}
 
 
119
 
120
  if __name__ == "__main__":
121
  main()
@@ -138,15 +184,10 @@ def main():
138
 
139
  if st.button("Generate and Download"):
140
  if app_title:
141
- # Include selected predefined analysis tasks in the app content
142
- analysis_tasks_code = ""
143
- for task in selected_tasks:
144
- analysis_tasks_code += f"\n# {PREDEFINED_ANALYSIS[task]['Description']}\n{PREDEFINED_ANALYSIS[task]['Code']}\n"
145
-
146
  # Write generated code to a .py file
147
  file_path = f"{app_title.replace(' ', '_').lower()}_app.py"
148
  with open(file_path, "w") as f:
149
- f.write(generate_streamlit_app_code(app_title, app_subtitle, side_panel_title, analysis_tasks_code, requirements))
150
 
151
  # Write requirements.txt file
152
  if requirements:
 
1
  import streamlit as st
 
 
 
 
2
 
3
  # Predefined analysis tasks and visualization types
4
  PREDEFINED_ANALYSIS = {
5
  "Basic Statistics": {
6
  "Description": "Generate basic statistics summary for the dataset.",
7
+ "Function": "show_basic_statistics",
8
+ "Code": """
9
+ def show_basic_statistics(df):
10
+ st.write(df.describe())
11
+ """
12
  },
13
  "Correlation Heatmap": {
14
  "Description": "Generate a correlation heatmap for numeric columns.",
15
+ "Function": "show_correlation_heatmap",
16
+ "Code": """
17
+ def show_correlation_heatmap(df):
18
+ import seaborn as sns
19
+ st.write(df.corr())
20
+ st.write(sns.heatmap(df.corr(), annot=True, cmap='coolwarm'))
21
+ """
22
  },
23
  "Histogram": {
24
  "Description": "Generate a histogram for a selected numeric column.",
25
+ "Function": "show_histogram",
26
  "Code": """
27
+ def show_histogram(df):
28
+ import plotly.express as px
29
+ selected_column = st.selectbox("Select a numeric column for the histogram", df.select_dtypes(include='number').columns)
30
+ st.write(px.histogram(df, x=selected_column))
31
  """
32
  },
33
  "Box Plot": {
34
  "Description": "Generate a box plot for a selected numeric column.",
35
+ "Function": "show_box_plot",
36
  "Code": """
37
+ def show_box_plot(df):
38
+ import plotly.express as px
39
+ selected_column = st.selectbox("Select a numeric column for the box plot", df.select_dtypes(include='number').columns)
40
+ st.write(px.box(df, y=selected_column))
41
  """
42
  },
43
  "Scatter Plot": {
44
  "Description": "Generate a scatter plot for two selected numeric columns.",
45
+ "Function": "show_scatter_plot",
46
  "Code": """
47
+ def show_scatter_plot(df):
48
+ import plotly.express as px
49
+ x_column = st.selectbox("Select X-axis column", df.select_dtypes(include='number').columns)
50
+ y_column = st.selectbox("Select Y-axis column", df.select_dtypes(include='number').columns)
51
+ st.write(px.scatter(df, x=x_column, y=y_column))
52
  """
53
  },
54
  "Line Plot": {
55
  "Description": "Generate a line plot for a selected numeric column.",
56
+ "Function": "show_line_plot",
57
  "Code": """
58
+ def show_line_plot(df):
59
+ import plotly.express as px
60
+ selected_column = st.selectbox("Select a numeric column for the line plot", df.select_dtypes(include='number').columns)
61
+ st.write(px.line(df, x=df.index, y=selected_column))
62
  """
63
  },
64
  "Bar Chart": {
65
  "Description": "Generate a bar chart for a selected categorical column.",
66
+ "Function": "show_bar_chart",
67
  "Code": """
68
+ def show_bar_chart(df):
69
+ import plotly.express as px
70
+ selected_column = st.selectbox("Select a categorical column for the bar chart", df.select_dtypes(include='object').columns)
71
+ st.write(px.bar(df, x=selected_column))
72
  """
73
  },
74
  "Pair Plot": {
75
  "Description": "Generate a pair plot for pairwise relationships between numeric columns.",
76
+ "Function": "show_pair_plot",
77
+ "Code": """
78
+ def show_pair_plot(df):
79
+ import seaborn as sns
80
+ st.write(sns.pairplot(df))
81
+ """
82
  },
83
  "Distribution Plot": {
84
  "Description": "Generate a distribution plot for a selected numeric column.",
85
+ "Function": "show_distribution_plot",
86
  "Code": """
87
+ def show_distribution_plot(df):
88
+ import seaborn as sns
89
+ selected_column = st.selectbox("Select a numeric column for the distribution plot", df.select_dtypes(include='number').columns)
90
+ st.write(sns.displot(df[selected_column], kde=True))
91
  """
92
  },
93
  "Count Plot": {
94
  "Description": "Generate a count plot for a selected categorical column.",
95
+ "Function": "show_count_plot",
96
  "Code": """
97
+ def show_count_plot(df):
98
+ import seaborn as sns
99
+ selected_column = st.selectbox("Select a categorical column for the count plot", df.select_dtypes(include='object').columns)
100
+ st.write(sns.countplot(data=df, x=selected_column))
101
  """
102
  },
103
  "Pie Chart": {
104
  "Description": "Generate a pie chart for a selected categorical column.",
105
+ "Function": "show_pie_chart",
106
  "Code": """
107
+ def show_pie_chart(df):
108
+ import plotly.express as px
109
+ selected_column = st.selectbox("Select a categorical column for the pie chart", df.select_dtypes(include='object').columns)
110
+ st.write(px.pie(df, names=selected_column))
111
  """
112
  },
113
  "Area Plot": {
114
  "Description": "Generate an area plot for a selected numeric column.",
115
+ "Function": "show_area_plot",
116
  "Code": """
117
+ def show_area_plot(df):
118
+ import plotly.express as px
119
+ selected_column = st.selectbox("Select a numeric column for the area plot", df.select_dtypes(include='number').columns)
120
+ st.write(px.area(df, x=df.index, y=selected_column))
121
  """
122
  },
123
  "Violin Plot": {
124
  "Description": "Generate a violin plot for a selected numeric column.",
125
+ "Function": "show_violin_plot",
126
  "Code": """
127
+ def show_violin_plot(df):
128
+ import plotly.express as px
129
+ selected_column = st.selectbox("Select a numeric column for the violin plot", df.select_dtypes(include='number').columns)
130
+ st.write(px.violin(df, y=selected_column))
131
  """
132
  },
133
  }
134
 
135
  def generate_streamlit_app_code(app_title, app_subtitle, side_panel_title, analysis_tasks, requirements):
136
  # Generate Python code for the Streamlit app
137
+ analysis_functions_code = "\n".join([PREDEFINED_ANALYSIS[task]['Code'] for task in analysis_tasks])
138
+ analysis_tasks_code = "\n ".join([f"{PREDEFINED_ANALYSIS[task]['Function']}(df)" for task in analysis_tasks])
139
+
140
  code = f"""
141
  import streamlit as st
142
  import pandas as pd
 
159
 
160
  df = load_data(uploaded_file)
161
 
162
+ {analysis_functions_code}
163
+
164
+ {analysis_tasks_code}
165
 
166
  if __name__ == "__main__":
167
  main()
 
184
 
185
  if st.button("Generate and Download"):
186
  if app_title:
 
 
 
 
 
187
  # Write generated code to a .py file
188
  file_path = f"{app_title.replace(' ', '_').lower()}_app.py"
189
  with open(file_path, "w") as f:
190
+ f.write(generate_streamlit_app_code(app_title, app_subtitle, side_panel_title, selected_tasks, requirements))
191
 
192
  # Write requirements.txt file
193
  if requirements: