arielgom commited on
Commit
f9720a4
·
verified ·
1 Parent(s): 5226778

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +159 -0
  2. requirements.txt +3 -0
app.py ADDED
@@ -0,0 +1,159 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ import statsmodels.api as sm
3
+ import streamlit as st
4
+ from datetime import datetime
5
+ import os
6
+
7
+ # ---- App title ----
8
+ st.title("Mini Stata - Simplified Version")
9
+
10
+ # ---- Student login ----
11
+ st.subheader("Student Login")
12
+ student_name = st.text_input("Enter your name:")
13
+ student_id = st.text_input("Enter your student ID:")
14
+
15
+ if student_name and student_id:
16
+ logfile = f"log_{student_id}.csv"
17
+
18
+ # Ensure log file exists
19
+ if not os.path.exists(logfile):
20
+ pd.DataFrame(columns=["timestamp", "student_name", "student_id", "command", "result"]).to_csv(logfile, index=False)
21
+
22
+ st.success(f"Logged in as {student_name} (ID: {student_id})")
23
+
24
+ # ---- File upload ----
25
+ uploaded_file = st.file_uploader("Upload CSV file", type=["csv"])
26
+ if uploaded_file is not None:
27
+ df = pd.read_csv(uploaded_file)
28
+ st.success("File uploaded successfully!")
29
+ else:
30
+ st.info("No file uploaded. Using default sample dataset.")
31
+ df = pd.DataFrame({
32
+ "mpg": [21, 22, 18, 30],
33
+ "weight": [2500, 2800, 3200, 2100],
34
+ "cyl": [4, 4, 6, 4],
35
+ "horsepower": [90, 95, 110, 80]
36
+ })
37
+
38
+ # ---- summarize ----
39
+ def summarize(var=None):
40
+ try:
41
+ if var is None:
42
+ return df.describe().T
43
+ elif var in df.columns:
44
+ return df[var].describe().to_frame().T
45
+ else:
46
+ return f"Variable '{var}' not found."
47
+ except Exception as e:
48
+ return f"Error in summarize: {e}"
49
+
50
+ # ---- browse ----
51
+ def browse(n=None):
52
+ try:
53
+ if n is None:
54
+ return df
55
+ return df.head(n)
56
+ except Exception as e:
57
+ return f"Error in browse: {e}"
58
+
59
+ # ---- tab ----
60
+ def tab(var):
61
+ try:
62
+ if var not in df.columns:
63
+ return f"Variable '{var}' not found."
64
+ return df[var].value_counts().to_frame("Frequency")
65
+ except Exception as e:
66
+ return f"Error in tab: {e}"
67
+
68
+ # ---- reg (simplified like Stata) ----
69
+ def reg(dep_var, indep_vars):
70
+ try:
71
+ if dep_var not in df.columns:
72
+ return f"Dependent variable '{dep_var}' not found."
73
+ for v in indep_vars:
74
+ if v not in df.columns:
75
+ return f"Independent variable '{v}' not found."
76
+
77
+ X = sm.add_constant(df[indep_vars])
78
+ y = df[dep_var]
79
+ model = sm.OLS(y, X).fit()
80
+
81
+ # Create clean results table
82
+ results_table = pd.DataFrame({
83
+ 'Variable': model.params.index,
84
+ 'Coef.': model.params.values.round(4),
85
+ 'Std. Err.': model.bse.values.round(4),
86
+ 't': model.tvalues.values.round(3),
87
+ 'P>|t|': model.pvalues.values.round(3)
88
+ })
89
+
90
+ # Display concise summary
91
+ summary_stats = f"Number of obs = {int(model.nobs)} R-squared = {model.rsquared:.3f}"
92
+ return results_table, summary_stats
93
+ except Exception as e:
94
+ return f"Error in regression: {e}"
95
+
96
+ # ---- Command parser ----
97
+ def run_command(cmd):
98
+ parts = cmd.strip().split()
99
+ if not parts:
100
+ return "No command entered."
101
+ command = parts[0].lower()
102
+ args = parts[1:]
103
+
104
+ if command == "summarize":
105
+ return summarize(args[0]) if args else summarize()
106
+ elif command == "browse":
107
+ return browse()
108
+ elif command == "tab":
109
+ if not args:
110
+ return "Usage: tab varname"
111
+ return tab(args[0])
112
+ elif command == "reg":
113
+ if len(args) < 2:
114
+ return "Usage: reg depvar indepvar1 indepvar2 ..."
115
+ return reg(args[0], args[1:])
116
+ else:
117
+ return f"Unknown command: '{command}'. Available commands: summarize, browse, tab, reg."
118
+
119
+ # ---- Interface ----
120
+ st.markdown("""
121
+ ### Available commands
122
+ - `summarize`
123
+ - `summarize mpg`
124
+ - `browse`
125
+ - `tab cyl`
126
+ - `reg mpg weight horsepower`
127
+ """)
128
+
129
+ cmd = st.text_input("Enter command:")
130
+
131
+ if st.button("Run"):
132
+ result = run_command(cmd)
133
+
134
+ # Log
135
+ log_entry = {
136
+ "timestamp": datetime.now().isoformat(),
137
+ "student_name": student_name,
138
+ "student_id": student_id,
139
+ "command": cmd,
140
+ "result": str(result)[:500]
141
+ }
142
+ pd.DataFrame([log_entry]).to_csv(logfile, mode="a", header=False, index=False)
143
+
144
+ # Display result
145
+ if isinstance(result, tuple): # regression output
146
+ table, stats = result
147
+ st.text(stats)
148
+ st.table(table)
149
+ elif isinstance(result, pd.DataFrame):
150
+ st.dataframe(result)
151
+ else:
152
+ st.text(result)
153
+
154
+ if st.button("Download My Log"):
155
+ with open(logfile, "r") as f:
156
+ st.download_button("Click to download", f, file_name=logfile, mime="text/csv")
157
+
158
+ else:
159
+ st.warning("Please enter name and student ID to start.")
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ streamlit
2
+ pandas
3
+ statsmodels