Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,18 +1,103 @@
|
|
| 1 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
| 2 |
import os
|
| 3 |
|
| 4 |
-
# Predefined
|
| 5 |
-
|
| 6 |
-
"
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
}
|
| 11 |
|
| 12 |
-
def generate_streamlit_app_code(app_title, app_subtitle, side_panel_title,
|
| 13 |
# Generate Python code for the Streamlit app
|
| 14 |
code = f"""
|
| 15 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
{requirements}
|
| 18 |
|
|
@@ -20,7 +105,14 @@ def main():
|
|
| 20 |
st.title("{app_title}")
|
| 21 |
st.subheader("{app_subtitle}")
|
| 22 |
st.sidebar.title("{side_panel_title}")
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
if __name__ == "__main__":
|
| 26 |
main()
|
|
@@ -35,28 +127,23 @@ def main():
|
|
| 35 |
app_subtitle = st.text_input("Enter your Streamlit app subtitle:")
|
| 36 |
side_panel_title = st.text_input("Enter your Streamlit app side panel title:")
|
| 37 |
|
| 38 |
-
#
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
# Include predefined components
|
| 42 |
-
selected_components = st.multiselect("Select predefined components to include:", list(PREDEFINED_COMPONENTS.keys()))
|
| 43 |
|
| 44 |
# Generate requirements.txt file
|
| 45 |
requirements = st.text_area("Enter requirements.txt content (optional):")
|
| 46 |
|
| 47 |
-
# File uploader for CSV or Excel files
|
| 48 |
-
uploaded_file = st.file_uploader("Upload a CSV or Excel file", type=["csv", "xlsx"])
|
| 49 |
-
|
| 50 |
if st.button("Generate and Download"):
|
| 51 |
if app_title:
|
| 52 |
-
# Include selected predefined
|
| 53 |
-
|
| 54 |
-
|
|
|
|
| 55 |
|
| 56 |
# Write generated code to a .py file
|
| 57 |
file_path = f"{app_title.replace(' ', '_').lower()}_app.py"
|
| 58 |
with open(file_path, "w") as f:
|
| 59 |
-
f.write(generate_streamlit_app_code(app_title, app_subtitle, side_panel_title,
|
| 60 |
|
| 61 |
# Write requirements.txt file
|
| 62 |
if requirements:
|
|
|
|
| 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
|
| 99 |
+
import seaborn as sns
|
| 100 |
+
import plotly.express as px
|
| 101 |
|
| 102 |
{requirements}
|
| 103 |
|
|
|
|
| 105 |
st.title("{app_title}")
|
| 106 |
st.subheader("{app_subtitle}")
|
| 107 |
st.sidebar.title("{side_panel_title}")
|
| 108 |
+
|
| 109 |
+
@st.cache
|
| 110 |
+
def load_data():
|
| 111 |
+
return pd.read_csv("your_data.csv") # Replace "your_data.csv" with the path to your dataset
|
| 112 |
+
|
| 113 |
+
df = load_data()
|
| 114 |
+
|
| 115 |
+
{analysis_tasks}
|
| 116 |
|
| 117 |
if __name__ == "__main__":
|
| 118 |
main()
|
|
|
|
| 127 |
app_subtitle = st.text_input("Enter your Streamlit app subtitle:")
|
| 128 |
side_panel_title = st.text_input("Enter your Streamlit app side panel title:")
|
| 129 |
|
| 130 |
+
# Select predefined analysis tasks
|
| 131 |
+
selected_tasks = st.multiselect("Select predefined analysis tasks to include:", list(PREDEFINED_ANALYSIS.keys()))
|
|
|
|
|
|
|
|
|
|
| 132 |
|
| 133 |
# Generate requirements.txt file
|
| 134 |
requirements = st.text_area("Enter requirements.txt content (optional):")
|
| 135 |
|
|
|
|
|
|
|
|
|
|
| 136 |
if st.button("Generate and Download"):
|
| 137 |
if app_title:
|
| 138 |
+
# Include selected predefined analysis tasks in the app content
|
| 139 |
+
analysis_tasks_code = ""
|
| 140 |
+
for task in selected_tasks:
|
| 141 |
+
analysis_tasks_code += f"\n# {PREDEFINED_ANALYSIS[task]['Description']}\n{PREDEFINED_ANALYSIS[task]['Code']}\n"
|
| 142 |
|
| 143 |
# Write generated code to a .py file
|
| 144 |
file_path = f"{app_title.replace(' ', '_').lower()}_app.py"
|
| 145 |
with open(file_path, "w") as f:
|
| 146 |
+
f.write(generate_streamlit_app_code(app_title, app_subtitle, side_panel_title, analysis_tasks_code, requirements))
|
| 147 |
|
| 148 |
# Write requirements.txt file
|
| 149 |
if requirements:
|