awacke1 commited on
Commit
253eb12
Β·
verified Β·
1 Parent(s): 2a5f3e9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -29
app.py CHANGED
@@ -9,8 +9,15 @@ from git import Repo
9
  import shutil
10
  from datetime import datetime
11
  import base64
12
- from playwright.sync_api import sync_playwright
13
- import time
 
 
 
 
 
 
 
14
 
15
  # Azure configuration
16
  ENDPOINT = "https://acae-afd.documents.azure.com:443/"
@@ -19,31 +26,46 @@ CONTAINER_NAME = os.environ.get("COSMOS_CONTAINER_NAME")
19
 
20
  # Playwright function for Azure login
21
  def azure_login_with_playwright(username, password):
22
- with sync_playwright() as p:
23
- browser = p.chromium.launch(headless=False) # Set to True for headless mode
24
- page = browser.new_page()
25
- page.goto("https://portal.azure.com")
26
-
27
- # Wait for and fill in the email field
28
- page.wait_for_selector('input[type="email"]')
29
- page.fill('input[type="email"]', username)
30
- page.click('input[type="submit"]')
31
-
32
- # Wait for and fill in the password field
33
- page.wait_for_selector('input[type="password"]')
34
- page.fill('input[type="password"]', password)
35
- page.click('input[type="submit"]')
36
-
37
- # Wait for login to complete (adjust as needed)
38
- page.wait_for_selector('button[id="nav_menu"]', timeout=60000)
39
-
40
- # At this point, the login should be complete
41
- # You may want to extract any necessary tokens or cookies here
 
 
 
 
42
 
43
- browser.close()
44
-
45
- # For now, we'll just return True to indicate successful login
46
- return True
 
 
 
 
 
 
 
 
 
 
 
47
 
48
  # GitHub configuration
49
  def download_github_repo(url, local_path):
@@ -114,8 +136,14 @@ if not st.session_state.logged_in:
114
  login_method = st.radio("Choose login method", ["Automatic", "Manual (Browser Simulation)"])
115
 
116
  if login_method == "Manual (Browser Simulation)":
117
- username = st.text_input("Azure Username/Email")
118
- password = st.text_input("Azure Password", type="password")
 
 
 
 
 
 
119
 
120
  if st.button("πŸš€ Login"):
121
  if login_method == "Automatic":
@@ -132,6 +160,9 @@ if not st.session_state.logged_in:
132
  st.error(f"Failed to authenticate automatically. Please try the Manual (Browser Simulation) method. Error: {str(e)}")
133
  st.stop()
134
  else:
 
 
 
135
  if not username or not password:
136
  st.error("Please enter both username and password for manual login.")
137
  st.stop()
@@ -152,7 +183,7 @@ if not st.session_state.logged_in:
152
  st.success("Successfully logged in!")
153
  st.rerun()
154
  else:
155
- # Main app content (unchanged)
156
  col1, col2 = st.columns([1, 3])
157
 
158
  with col1:
 
9
  import shutil
10
  from datetime import datetime
11
  import base64
12
+ import subprocess
13
+ import sys
14
+
15
+ # Check if playwright is installed
16
+ try:
17
+ from playwright.sync_api import sync_playwright
18
+ PLAYWRIGHT_INSTALLED = True
19
+ except ImportError:
20
+ PLAYWRIGHT_INSTALLED = False
21
 
22
  # Azure configuration
23
  ENDPOINT = "https://acae-afd.documents.azure.com:443/"
 
26
 
27
  # Playwright function for Azure login
28
  def azure_login_with_playwright(username, password):
29
+ if not PLAYWRIGHT_INSTALLED:
30
+ st.error("Playwright is not installed. Please install it using 'pip install playwright'")
31
+ return False
32
+
33
+ try:
34
+ with sync_playwright() as p:
35
+ browser = p.chromium.launch(headless=False) # Set to True for headless mode
36
+ page = browser.new_page()
37
+ page.goto("https://portal.azure.com")
38
+
39
+ # Wait for and fill in the email field
40
+ page.wait_for_selector('input[type="email"]')
41
+ page.fill('input[type="email"]', username)
42
+ page.click('input[type="submit"]')
43
+
44
+ # Wait for and fill in the password field
45
+ page.wait_for_selector('input[type="password"]')
46
+ page.fill('input[type="password"]', password)
47
+ page.click('input[type="submit"]')
48
+
49
+ # Wait for login to complete (adjust as needed)
50
+ page.wait_for_selector('button[id="nav_menu"]', timeout=60000)
51
+
52
+ browser.close()
53
 
54
+ return True
55
+ except Exception as e:
56
+ st.error(f"An error occurred during browser simulation: {str(e)}")
57
+ return False
58
+
59
+ # Function to install Playwright browsers
60
+ def install_playwright_browsers():
61
+ try:
62
+ result = subprocess.run([sys.executable, "-m", "playwright", "install"],
63
+ capture_output=True, text=True, check=True)
64
+ st.success("Playwright browsers installed successfully!")
65
+ return True
66
+ except subprocess.CalledProcessError as e:
67
+ st.error(f"Failed to install Playwright browsers. Error: {e.stderr}")
68
+ return False
69
 
70
  # GitHub configuration
71
  def download_github_repo(url, local_path):
 
136
  login_method = st.radio("Choose login method", ["Automatic", "Manual (Browser Simulation)"])
137
 
138
  if login_method == "Manual (Browser Simulation)":
139
+ if not PLAYWRIGHT_INSTALLED:
140
+ st.warning("Playwright is not installed. Please install it to use browser simulation.")
141
+ if st.button("Install Playwright"):
142
+ if install_playwright_browsers():
143
+ st.rerun()
144
+ else:
145
+ username = st.text_input("Azure Username/Email")
146
+ password = st.text_input("Azure Password", type="password")
147
 
148
  if st.button("πŸš€ Login"):
149
  if login_method == "Automatic":
 
160
  st.error(f"Failed to authenticate automatically. Please try the Manual (Browser Simulation) method. Error: {str(e)}")
161
  st.stop()
162
  else:
163
+ if not PLAYWRIGHT_INSTALLED:
164
+ st.error("Playwright is not installed. Please install it to use browser simulation.")
165
+ st.stop()
166
  if not username or not password:
167
  st.error("Please enter both username and password for manual login.")
168
  st.stop()
 
183
  st.success("Successfully logged in!")
184
  st.rerun()
185
  else:
186
+ # Main app content
187
  col1, col2 = st.columns([1, 3])
188
 
189
  with col1: