Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,61 +4,11 @@ import subprocess
|
|
| 4 |
from streamlit_pdf_viewer import pdf_viewer
|
| 5 |
from github import Github
|
| 6 |
import shutil # Make sure this is imported at the top
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
def pull_from_github():
|
| 10 |
-
"""
|
| 11 |
-
1. Wipes the local 'topics' folder.
|
| 12 |
-
2. Downloads a fresh copy from GitHub.
|
| 13 |
-
"""
|
| 14 |
-
try:
|
| 15 |
-
# 1. WIPE LOCAL FOLDER CLEAN
|
| 16 |
-
if os.path.exists(TOPICS_DIR):
|
| 17 |
-
shutil.rmtree(TOPICS_DIR) # Deletes the folder and everything inside
|
| 18 |
-
|
| 19 |
-
# Re-create the empty folder
|
| 20 |
-
os.makedirs(TOPICS_DIR, exist_ok=True)
|
| 21 |
-
|
| 22 |
-
# 2. CONNECT TO GITHUB
|
| 23 |
-
g = Github(st.secrets["github_token"])
|
| 24 |
-
repo = g.get_repo(st.secrets["github_repo"])
|
| 25 |
-
|
| 26 |
-
# 3. DOWNLOAD EVERYTHING
|
| 27 |
-
contents = repo.get_contents(TOPICS_DIR, ref=st.secrets["github_branch"])
|
| 28 |
-
count = 0
|
| 29 |
-
|
| 30 |
-
while contents:
|
| 31 |
-
file_content = contents.pop(0)
|
| 32 |
-
if file_content.type == "dir":
|
| 33 |
-
contents.extend(repo.get_contents(file_content.path, ref=st.secrets["github_branch"]))
|
| 34 |
-
else:
|
| 35 |
-
# Calculate where to save it locally
|
| 36 |
-
# We strip the TOPICS_DIR prefix from the GitHub path to ensure correct nesting
|
| 37 |
-
local_path = file_content.path
|
| 38 |
-
|
| 39 |
-
# Ensure the subfolder exists
|
| 40 |
-
os.makedirs(os.path.dirname(local_path), exist_ok=True)
|
| 41 |
-
|
| 42 |
-
# Write the file
|
| 43 |
-
with open(local_path, "wb") as f:
|
| 44 |
-
f.write(file_content.decoded_content)
|
| 45 |
-
count += 1
|
| 46 |
-
|
| 47 |
-
return True, f"Clean sync complete! Downloaded {count} files."
|
| 48 |
-
except Exception as e:
|
| 49 |
-
return False, str(e)
|
| 50 |
-
|
| 51 |
-
# Auto-load files if the container restarted
|
| 52 |
-
if not os.path.exists(TOPICS_DIR) or not os.listdir(TOPICS_DIR):
|
| 53 |
-
print("System restart detected. Auto-pulling files from GitHub...")
|
| 54 |
-
pull_from_github()
|
| 55 |
-
|
| 56 |
# --- Configuration ---
|
| 57 |
TOPICS_DIR = "topics"
|
| 58 |
TEMP_DIR = "temp_build"
|
| 59 |
os.makedirs(TEMP_DIR, exist_ok=True)
|
| 60 |
|
| 61 |
-
|
| 62 |
st.set_page_config(page_title="Physics Topics", layout="wide")
|
| 63 |
|
| 64 |
# ==========================================
|
|
@@ -126,7 +76,47 @@ def push_to_github(local_path, content, commit_message):
|
|
| 126 |
except Exception as e:
|
| 127 |
return False, f"GitHub Error: {str(e)}"
|
| 128 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
|
| 131 |
|
| 132 |
|
|
@@ -178,7 +168,7 @@ def compile_latex(file_path):
|
|
| 178 |
st.sidebar.title(f"Physics Archive ({current_role.title()})")
|
| 179 |
|
| 180 |
# ADMIN ONLY: Sync Button
|
| 181 |
-
if current_role == 'admin':
|
| 182 |
if st.sidebar.button("🔄 Pull from GitHub"):
|
| 183 |
with st.spinner("Downloading latest files..."):
|
| 184 |
success, msg = pull_from_github()
|
|
|
|
| 4 |
from streamlit_pdf_viewer import pdf_viewer
|
| 5 |
from github import Github
|
| 6 |
import shutil # Make sure this is imported at the top
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
# --- Configuration ---
|
| 8 |
TOPICS_DIR = "topics"
|
| 9 |
TEMP_DIR = "temp_build"
|
| 10 |
os.makedirs(TEMP_DIR, exist_ok=True)
|
| 11 |
|
|
|
|
| 12 |
st.set_page_config(page_title="Physics Topics", layout="wide")
|
| 13 |
|
| 14 |
# ==========================================
|
|
|
|
| 76 |
except Exception as e:
|
| 77 |
return False, f"GitHub Error: {str(e)}"
|
| 78 |
|
| 79 |
+
def pull_from_github():
|
| 80 |
+
"""
|
| 81 |
+
1. Wipes the local 'topics' folder.
|
| 82 |
+
2. Downloads a fresh copy from GitHub.
|
| 83 |
+
"""
|
| 84 |
+
try:
|
| 85 |
+
# 1. WIPE LOCAL FOLDER CLEAN
|
| 86 |
+
if os.path.exists(TOPICS_DIR):
|
| 87 |
+
shutil.rmtree(TOPICS_DIR) # Deletes the folder and everything inside
|
| 88 |
+
|
| 89 |
+
# Re-create the empty folder
|
| 90 |
+
os.makedirs(TOPICS_DIR, exist_ok=True)
|
| 91 |
|
| 92 |
+
# 2. CONNECT TO GITHUB
|
| 93 |
+
g = Github(st.secrets["github_token"])
|
| 94 |
+
repo = g.get_repo(st.secrets["github_repo"])
|
| 95 |
+
|
| 96 |
+
# 3. DOWNLOAD EVERYTHING
|
| 97 |
+
contents = repo.get_contents(TOPICS_DIR, ref=st.secrets["github_branch"])
|
| 98 |
+
count = 0
|
| 99 |
+
|
| 100 |
+
while contents:
|
| 101 |
+
file_content = contents.pop(0)
|
| 102 |
+
if file_content.type == "dir":
|
| 103 |
+
contents.extend(repo.get_contents(file_content.path, ref=st.secrets["github_branch"]))
|
| 104 |
+
else:
|
| 105 |
+
# Calculate where to save it locally
|
| 106 |
+
# We strip the TOPICS_DIR prefix from the GitHub path to ensure correct nesting
|
| 107 |
+
local_path = file_content.path
|
| 108 |
+
|
| 109 |
+
# Ensure the subfolder exists
|
| 110 |
+
os.makedirs(os.path.dirname(local_path), exist_ok=True)
|
| 111 |
+
|
| 112 |
+
# Write the file
|
| 113 |
+
with open(local_path, "wb") as f:
|
| 114 |
+
f.write(file_content.decoded_content)
|
| 115 |
+
count += 1
|
| 116 |
+
|
| 117 |
+
return True, f"Clean sync complete! Downloaded {count} files."
|
| 118 |
+
except Exception as e:
|
| 119 |
+
return False, str(e)
|
| 120 |
|
| 121 |
|
| 122 |
|
|
|
|
| 168 |
st.sidebar.title(f"Physics Archive ({current_role.title()})")
|
| 169 |
|
| 170 |
# ADMIN ONLY: Sync Button
|
| 171 |
+
if current_role == 'admin' or current_role == 'viewer':
|
| 172 |
if st.sidebar.button("🔄 Pull from GitHub"):
|
| 173 |
with st.spinner("Downloading latest files..."):
|
| 174 |
success, msg = pull_from_github()
|