thinhvv commited on
Commit
1a005d3
·
verified ·
1 Parent(s): 092d6d1

Update init.py

Browse files
Files changed (1) hide show
  1. init.py +38 -25
init.py CHANGED
@@ -15,9 +15,9 @@ st.set_page_config(
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 repository where your app resides
21
  APP_SUBDIR = "private_repo"
22
  git_user = st.secrets["GIT_USR"]
23
  git_token = st.secrets["GIT_TK"]
@@ -36,20 +36,29 @@ def clone_repository():
36
 
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):
44
  print("Installing custom packages...")
45
- for package in os.listdir(packages_dir):
46
- package_path = os.path.join(packages_dir, package)
47
- if os.path.isdir(package_path):
48
- print(f"Installing package: {package}")
49
  subprocess.run(
50
- [sys.executable, "-m", "pip", "install", "-e", package_path],
51
  check=True
52
  )
 
 
 
 
 
 
 
 
53
  else:
54
  print("No 'packages' directory found. Skipping custom packages installation.")
55
 
@@ -70,26 +79,30 @@ def install_requirements():
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
- os.chdir(APP_SUBDIR)
83
- # Install custom packages and requirements at the repository root.
 
 
 
 
 
84
  install_custom_packages()
85
  install_requirements()
86
- # Create the marker file to note that setup is complete.
87
  with open(marker, "w") as f:
88
  f.write("done")
89
  else:
90
- # Setup already complete: navigate to the app subdirectory.
91
- os.chdir(REPO_NAME)
92
- os.chdir(APP_SUBDIR)
93
 
94
  # --------------------------------------------------
95
  # Main Loading Process
@@ -101,11 +114,11 @@ try:
101
  subprocess.run(["ls", "-l"], shell=True)
102
 
103
  # Import and run the main app.
104
- # Since your repository contains 'st_app.py' at the root of the subdirectory,
105
  # import main_app from st_app.py.
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)
 
15
  # --------------------------------------------------
16
  # Secrets: GitHub Credentials & Repository Name
17
  # --------------------------------------------------
18
+ # PRV_REPO: Name of the GitHub repository to clone
19
+ REPO_NAME = st.secrets["PRV_REPO"] # e.g. "internal_demo_signal_portfolio"
20
+ # APP_SUBDIR: The subdirectory inside the cloned repository where your app resides.
21
  APP_SUBDIR = "private_repo"
22
  git_user = st.secrets["GIT_USR"]
23
  git_token = st.secrets["GIT_TK"]
 
36
 
37
  def install_custom_packages():
38
  """
39
+ Install custom Python packages from the 'packages' directory.
40
+ - If an item is a directory, install it in editable mode.
41
+ - If an item is a tar.gz file, install it normally.
42
  """
43
  packages_dir = os.path.join(os.getcwd(), "packages")
44
  if os.path.exists(packages_dir):
45
  print("Installing custom packages...")
46
+ for item in os.listdir(packages_dir):
47
+ item_path = os.path.join(packages_dir, item)
48
+ if os.path.isdir(item_path):
49
+ print(f"Installing package (editable): {item}")
50
  subprocess.run(
51
+ [sys.executable, "-m", "pip", "install", "-e", item_path],
52
  check=True
53
  )
54
+ elif os.path.isfile(item_path) and item.endswith(".tar.gz"):
55
+ print(f"Installing package from tar.gz: {item}")
56
+ subprocess.run(
57
+ [sys.executable, "-m", "pip", "install", item_path],
58
+ check=True
59
+ )
60
+ else:
61
+ print(f"Skipping unknown package format: {item}")
62
  else:
63
  print("No 'packages' directory found. Skipping custom packages installation.")
64
 
 
79
  def setup_environment():
80
  """
81
  Clone the repository and install dependencies if not already done.
82
+ A marker file (.setup_complete) is stored in the app folder to avoid repeating setup.
83
  """
84
+ # Compute the absolute path to the app folder inside the cloned repository.
85
+ repo_app_path = os.path.join(os.getcwd(), REPO_NAME, APP_SUBDIR)
86
+ marker = os.path.join(repo_app_path, ".setup_complete")
87
+
88
+ # Clone repository if needed.
89
+ if not os.path.exists(REPO_NAME):
90
  clone_repository()
91
+
92
+ # Change directory into the repository's app folder.
93
+ if not os.path.exists(repo_app_path):
94
+ raise FileNotFoundError(f"App folder not found: {repo_app_path}")
95
+ os.chdir(repo_app_path)
96
+
97
+ # If the marker file does not exist, run installation steps.
98
+ if not os.path.exists(marker):
99
+ print("Running environment setup in:", repo_app_path)
100
  install_custom_packages()
101
  install_requirements()
 
102
  with open(marker, "w") as f:
103
  f.write("done")
104
  else:
105
+ print("Setup already completed. Skipping installation steps.")
 
 
106
 
107
  # --------------------------------------------------
108
  # Main Loading Process
 
114
  subprocess.run(["ls", "-l"], shell=True)
115
 
116
  # Import and run the main app.
117
+ # Since your repository contains 'st_app.py' in the app folder,
118
  # import main_app from st_app.py.
119
  from st_app import main_app
120
  main_app()
121
 
122
  except Exception as e:
123
  # Log errors to the console (these messages will not display in the UI)
124
+ print("Something went wrong! Error details:", e)