thinhvv commited on
Commit
d6dd170
·
verified ·
1 Parent(s): 855b960

Update init.py

Browse files
Files changed (1) hide show
  1. init.py +43 -29
init.py CHANGED
@@ -13,20 +13,22 @@ st.set_page_config(
13
  )
14
 
15
  # --------------------------------------------------
16
- # Secrets: Repository and GitHub Credentials
17
  # --------------------------------------------------
 
 
 
 
 
 
18
 
19
- private_repo = st.secrets["PRV_REPO"] # e.g. "private_app"
20
- git_user = st.secrets["GIT_USR"]
21
- git_token = st.secrets["GIT_TK"]
22
-
23
- def clone_private_repo():
24
  """
25
  Clone the GitHub repository if it hasn't been cloned already.
26
- The repository will be cloned into a folder named after `private_repo`.
27
  """
28
- repo_url = f"https://{git_user}:{git_token}@github.com/{git_user}/{private_repo}.git"
29
- if not os.path.exists(private_repo):
30
  print("Cloning repository...")
31
  subprocess.run(["git", "clone", repo_url], check=True)
32
  else:
@@ -35,7 +37,7 @@ def clone_private_repo():
35
  def install_custom_packages():
36
  """
37
  Install custom Python packages from the 'packages' directory
38
- in the current directory (if it exists) using pip in editable mode.
39
  """
40
  packages_dir = os.path.join(os.getcwd(), "packages")
41
  if os.path.exists(packages_dir):
@@ -53,7 +55,7 @@ def install_custom_packages():
53
 
54
  def install_requirements():
55
  """
56
- Install dependencies from the requirements.txt file in the current directory.
57
  """
58
  requirements_path = os.path.join(os.getcwd(), "requirements.txt")
59
  if os.path.exists(requirements_path):
@@ -65,33 +67,45 @@ def install_requirements():
65
  else:
66
  print("requirements.txt not found.")
67
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68
  # --------------------------------------------------
69
  # Main Loading Process
70
  # --------------------------------------------------
71
  try:
72
- # Step 1: Clone the repository if necessary
73
- clone_private_repo()
74
-
75
- # Step 2: Change directory into the cloned repository folder,
76
- # then into the subdirectory that contains your app.
77
- os.chdir(private_repo)
78
- os.chdir(private_repo)
79
 
80
- # (Optional) List files to verify the directory structure (console output only)
81
  subprocess.run(["ls", "-l"], shell=True)
82
 
83
- # Step 3: Install custom packages (if any)
84
- install_custom_packages()
85
-
86
- # Step 4: Install other dependencies from requirements.txt
87
- install_requirements()
88
-
89
- # Step 5: Import and run the main app.
90
- # The main app is assumed to be in a module named st.app (e.g. st/app.py)
91
- # with a callable main_app() function.
92
  from st.app import main_app
93
  main_app()
94
 
95
  except Exception as e:
96
- # Log the error to the console.
97
  print("Something went wrong! Error details:", e)
 
13
  )
14
 
15
  # --------------------------------------------------
16
+ # Secrets: GitHub Credentials & Repository Name
17
  # --------------------------------------------------
18
+ # PRV_REPO: Name of the GitHub repository to clone (e.g. "internal_demo_signal_portfolio")
19
+ REPO_NAME = st.secrets["PRV_REPO"]
20
+ # The subdirectory inside the cloned repo that contains the main app files
21
+ APP_SUBDIR = "private_repo"
22
+ git_user = st.secrets["GIT_USR"]
23
+ git_token = st.secrets["GIT_TK"]
24
 
25
+ def clone_repository():
 
 
 
 
26
  """
27
  Clone the GitHub repository if it hasn't been cloned already.
28
+ The repository is cloned into a folder named REPO_NAME.
29
  """
30
+ repo_url = f"https://{git_user}:{git_token}@github.com/{git_user}/{REPO_NAME}.git"
31
+ if not os.path.exists(REPO_NAME):
32
  print("Cloning repository...")
33
  subprocess.run(["git", "clone", repo_url], check=True)
34
  else:
 
37
  def install_custom_packages():
38
  """
39
  Install custom Python packages from the 'packages' directory
40
+ (if it exists in the current working directory) using pip in editable mode.
41
  """
42
  packages_dir = os.path.join(os.getcwd(), "packages")
43
  if os.path.exists(packages_dir):
 
55
 
56
  def install_requirements():
57
  """
58
+ Install dependencies from the requirements.txt file in the current working directory.
59
  """
60
  requirements_path = os.path.join(os.getcwd(), "requirements.txt")
61
  if os.path.exists(requirements_path):
 
67
  else:
68
  print("requirements.txt not found.")
69
 
70
+ def setup_environment():
71
+ """
72
+ Clone the repository and install dependencies if not already done.
73
+ A marker file (.setup_complete) is used to avoid repeating setup on every visit.
74
+ """
75
+ marker = ".setup_complete"
76
+
77
+ # If the marker file does not exist, perform the setup.
78
+ if not os.path.exists(marker):
79
+ clone_repository()
80
+ # Change directory into the cloned repository folder.
81
+ os.chdir(REPO_NAME)
82
+ # Install custom packages and requirements at the repository root.
83
+ install_custom_packages()
84
+ install_requirements()
85
+ # Create the marker file to note that setup is complete.
86
+ with open(marker, "w") as f:
87
+ f.write("done")
88
+ # Change directory into the subdirectory that contains your app.
89
+ os.chdir(APP_SUBDIR)
90
+ else:
91
+ # Setup already complete: navigate to the app subdirectory.
92
+ os.chdir(REPO_NAME)
93
+ os.chdir(APP_SUBDIR)
94
+
95
  # --------------------------------------------------
96
  # Main Loading Process
97
  # --------------------------------------------------
98
  try:
99
+ setup_environment()
 
 
 
 
 
 
100
 
101
+ # (Optional) List files in the current directory for debugging (logged to console)
102
  subprocess.run(["ls", "-l"], shell=True)
103
 
104
+ # Import and run the main app.
105
+ # The main app is assumed to be in st/app.py with a callable main_app() function.
 
 
 
 
 
 
 
106
  from st.app import main_app
107
  main_app()
108
 
109
  except Exception as e:
110
+ # Log errors to the console (these messages will not display in the UI)
111
  print("Something went wrong! Error details:", e)