JSenkCC commited on
Commit
4f98e67
·
verified ·
1 Parent(s): 8058255

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +74 -59
app.py CHANGED
@@ -1,73 +1,88 @@
1
  import streamlit as st
2
- import os
3
- import shutil
4
- from git import Repo
5
 
6
- # Set up a directory for saving files
7
- SAVE_DIR = "saved_projects"
8
 
9
- if not os.path.exists(SAVE_DIR):
10
- os.makedirs(SAVE_DIR)
 
 
 
 
 
 
 
 
 
11
 
12
- def save_uploaded_files(uploaded_files):
13
- """Save uploaded files to the SAVE_DIR."""
14
- project_dir = os.path.join(SAVE_DIR, "uploaded_project")
15
- if os.path.exists(project_dir):
16
- shutil.rmtree(project_dir) # Clean up existing files
17
- os.makedirs(project_dir)
18
-
19
- for uploaded_file in uploaded_files:
20
- with open(os.path.join(project_dir, uploaded_file.name), "wb") as f:
21
- f.write(uploaded_file.getbuffer())
22
- return project_dir
23
 
24
- def clone_repository(repo_url):
25
- """Clone a GitHub repository into SAVE_DIR."""
26
- repo_name = repo_url.split("/")[-1].replace(".git", "")
27
- repo_dir = os.path.join(SAVE_DIR, repo_name)
28
- if os.path.exists(repo_dir):
29
- shutil.rmtree(repo_dir) # Clean up existing files
30
- Repo.clone_from(repo_url, repo_dir)
31
- return repo_dir
32
 
 
33
  def main():
34
- st.title("Project Manager App")
35
- st.write("Upload your project files or clone a GitHub repository.")
36
 
37
- # Sidebar for navigation
38
- option = st.sidebar.selectbox(
39
- "Choose an option:",
40
- ("Upload Files", "Clone GitHub Repository")
41
- )
42
 
43
- if option == "Upload Files":
44
- st.header("Upload Files")
45
- uploaded_files = st.file_uploader(
46
- "Upload your coding project files (multiple files allowed):",
47
- accept_multiple_files=True
48
- )
49
- if uploaded_files:
50
- saved_path = save_uploaded_files(uploaded_files)
51
- st.success(f"Files saved to {saved_path}")
 
 
 
 
52
 
53
- elif option == "Clone GitHub Repository":
54
- st.header("Clone GitHub Repository")
55
- repo_url = st.text_input("Enter the GitHub repository URL:")
56
- if repo_url:
57
- if st.button("Clone Repository"):
58
- try:
59
- repo_path = clone_repository(repo_url)
60
- st.success(f"Repository cloned to {repo_path}")
61
- except Exception as e:
62
- st.error(f"Error cloning repository: {e}")
63
 
64
- # Display saved projects
65
- st.header("Saved Projects")
66
- if os.listdir(SAVE_DIR):
67
- for project in os.listdir(SAVE_DIR):
68
- st.write(f"- {project}")
69
- else:
70
- st.write("No projects saved yet.")
 
 
 
 
 
 
 
 
 
71
 
72
  if __name__ == "__main__":
73
  main()
 
 
1
  import streamlit as st
2
+ import sqlite3
3
+ import hashlib
 
4
 
5
+ # Database setup
6
+ DB_FILE = "users.db"
7
 
8
+ def create_user_table():
9
+ conn = sqlite3.connect(DB_FILE)
10
+ cursor = conn.cursor()
11
+ cursor.execute("""
12
+ CREATE TABLE IF NOT EXISTS users (
13
+ username TEXT PRIMARY KEY,
14
+ password TEXT
15
+ )
16
+ """)
17
+ conn.commit()
18
+ conn.close()
19
 
20
+ def add_user(username, password):
21
+ conn = sqlite3.connect(DB_FILE)
22
+ cursor = conn.cursor()
23
+ hashed_password = hashlib.sha256(password.encode()).hexdigest()
24
+ try:
25
+ cursor.execute("INSERT INTO users (username, password) VALUES (?, ?)", (username, hashed_password))
26
+ conn.commit()
27
+ except sqlite3.IntegrityError:
28
+ st.error("Username already exists. Please choose a different username.")
29
+ conn.close()
 
30
 
31
+ def authenticate_user(username, password):
32
+ conn = sqlite3.connect(DB_FILE)
33
+ cursor = conn.cursor()
34
+ hashed_password = hashlib.sha256(password.encode()).hexdigest()
35
+ cursor.execute("SELECT * FROM users WHERE username = ? AND password = ?", (username, hashed_password))
36
+ user = cursor.fetchone()
37
+ conn.close()
38
+ return user
39
 
40
+ # Main application
41
  def main():
42
+ st.title("Welcome to Your Streamlit App")
 
43
 
44
+ # Initialize database
45
+ create_user_table()
 
 
 
46
 
47
+ # Authentication state
48
+ if "authenticated" not in st.session_state:
49
+ st.session_state.authenticated = False
50
+
51
+ # Central login/register interface
52
+ if not st.session_state.authenticated:
53
+ st.subheader("Please Log In or Register to Continue")
54
+ auth_mode = st.radio("Choose an Option", ["Log In", "Register"], horizontal=True)
55
+
56
+ if auth_mode == "Log In":
57
+ st.subheader("Log In")
58
+ username = st.text_input("Username", key="login_username")
59
+ password = st.text_input("Password", type="password", key="login_password")
60
 
61
+ if st.button("Log In"):
62
+ if authenticate_user(username, password):
63
+ st.success(f"Welcome back, {username}!")
64
+ st.session_state.authenticated = True
65
+ st.session_state.username = username
66
+ else:
67
+ st.error("Invalid username or password. Please try again.")
 
 
 
68
 
69
+ elif auth_mode == "Register":
70
+ st.subheader("Register")
71
+ username = st.text_input("Create Username", key="register_username")
72
+ password = st.text_input("Create Password", type="password", key="register_password")
73
+
74
+ if st.button("Register"):
75
+ if username and password:
76
+ add_user(username, password)
77
+ st.success("Account created successfully! You can now log in.")
78
+ else:
79
+ st.error("Please fill in all fields.")
80
+
81
+ # Authenticated user workspace
82
+ if st.session_state.authenticated:
83
+ st.subheader(f"Hello, {st.session_state.username}!")
84
+ st.write("This is your workspace. All your saved work will appear here.")
85
 
86
  if __name__ == "__main__":
87
  main()
88
+