Skip to content

Commit 15f0d52

Browse files
committed
Try to run cells the same way for all browsers
1 parent c8d5aa9 commit 15f0d52

File tree

1 file changed

+56
-112
lines changed

1 file changed

+56
-112
lines changed

scripts/automated-notebook-run-script.py

Lines changed: 56 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -103,123 +103,67 @@ def main():
103103
time.sleep(1)
104104

105105
# This will run all the cells of the chosen notebook
106-
if args.driver == "chrome" or args.driver == "firefox":
107-
print("Running Cells")
108-
while True:
109-
focused_cell = driver.find_element(
110-
By.CSS_SELECTOR, ".jp-Notebook-cell.jp-mod-selected"
111-
)
112-
editor_divs = focused_cell.find_elements(
113-
By.CSS_SELECTOR, ".jp-InputArea-editor div"
114-
)
115-
116-
cell_content = "".join(
117-
div.get_attribute("textContent") for div in editor_divs
118-
).strip()
119-
120-
if not cell_content:
121-
print("Empty cell reached")
122-
break
106+
print("Running Cells")
107+
while True:
108+
focused_cell = driver.find_element(
109+
By.CSS_SELECTOR, ".jp-Notebook-cell.jp-mod-selected"
110+
)
111+
editor_divs = focused_cell.find_elements(
112+
By.CSS_SELECTOR, ".jp-InputArea-editor div"
113+
)
114+
115+
cell_content = "".join(
116+
div.get_attribute("textContent") for div in editor_divs
117+
).strip()
118+
119+
if not cell_content:
120+
print("Empty cell reached")
121+
break
123122

124-
if cell_is_waiting_for_input(driver):
125-
print("Cell requesting input")
126-
input_box = WebDriverWait(driver, 5).until(
127-
EC.visibility_of_element_located(
128-
(By.CSS_SELECTOR, ".jp-Stdin-input")
129-
)
130-
)
131-
input_box.click()
132-
input_box.send_keys(f"{args.stdin}")
133-
time.sleep(1)
134-
input_box.send_keys(Keys.CONTROL, Keys.ENTER)
135-
next_cell = focused_cell.find_element(
136-
By.XPATH,
137-
"following-sibling::div[contains(@class,'jp-Notebook-cell')][1]",
138-
)
139-
driver.execute_script(
140-
"arguments[0].scrollIntoView({block:'center'});", next_cell
123+
if cell_is_waiting_for_input(driver):
124+
print("Cell requesting input")
125+
input_box = WebDriverWait(driver, 5).until(
126+
EC.visibility_of_element_located(
127+
(By.CSS_SELECTOR, ".jp-Stdin-input")
141128
)
142-
next_cell.click()
143-
while True:
144-
spans = driver.find_elements(By.CSS_SELECTOR, "span.jp-StatusBar-TextItem")
145-
status_span = spans[2]
146-
text = status_span.text
147-
148-
if "Idle" in text:
149-
break
150-
time.sleep(0.01)
151-
print(focused_cell.text)
152-
focused_cell=next_cell
153-
run_menu = wait.until(
154-
EC.element_to_be_clickable((By.XPATH, "//li[normalize-space()='Run']"))
155129
)
156-
actions.move_to_element(run_menu).pause(0.05).click().perform()
157-
actions.send_keys(Keys.DOWN).send_keys(Keys.ENTER).pause(0.1).perform()
158-
if not cell_is_waiting_for_input(driver):
159-
while True:
160-
spans = driver.find_elements(By.CSS_SELECTOR, "span.jp-StatusBar-TextItem")
161-
status_span = spans[2]
162-
text = status_span.text
163-
164-
if "Idle" in text:
165-
print(focused_cell.text)
166-
break
167-
time.sleep(0.01)
168-
169-
elif args.driver == "safari":
170-
print("Running all cells using Shift+Enter...")
171-
while True:
172-
focused_cell = driver.find_element(
173-
By.CSS_SELECTOR, ".jp-Notebook-cell.jp-mod-selected"
130+
input_box.click()
131+
input_box.send_keys(f"{args.stdin}")
132+
time.sleep(1)
133+
input_box.send_keys(Keys.CONTROL, Keys.ENTER)
134+
next_cell = focused_cell.find_element(
135+
By.XPATH,
136+
"following-sibling::div[contains(@class,'jp-Notebook-cell')][1]",
174137
)
175-
print(focused_cell.text)
176-
177-
editor_divs = focused_cell.find_elements(
178-
By.CSS_SELECTOR, ".jp-InputArea-editor div"
138+
driver.execute_script(
139+
"arguments[0].scrollIntoView({block:'center'});", next_cell
179140
)
180-
cell_content = "".join([div.text for div in editor_divs]).strip()
181-
182-
if not cell_content:
183-
print("Empty cell reached")
184-
break
185-
186-
if cell_is_waiting_for_input(driver):
187-
print("Cell requesting input")
188-
input_box = WebDriverWait(driver, 5).until(
189-
EC.visibility_of_element_located(
190-
(By.CSS_SELECTOR, ".jp-Stdin-input")
191-
)
192-
)
193-
input_box.click()
194-
input_box.send_keys(f"{args.stdin}")
195-
time.sleep(10)
196-
input_box.send_keys(Keys.CONTROL, Keys.ENTER)
197-
next_cell = focused_cell.find_element(
198-
By.XPATH,
199-
"following-sibling::div[contains(@class,'jp-Notebook-cell')][1]",
200-
)
201-
driver.execute_script(
202-
"arguments[0].scrollIntoView({block:'center'});", next_cell
203-
)
204-
print(next_cell.text)
205-
next_cell.click()
206-
driver.execute_script(
207-
"""
208-
const evt = new KeyboardEvent('keydown', {
209-
key: 'Enter',
210-
code: 'Enter',
211-
keyCode: 13,
212-
which: 13,
213-
shiftKey: true,
214-
bubbles: true
215-
});
216-
document.activeElement.dispatchEvent(evt);
217-
"""
218-
)
219-
220-
# Press Shift+Enter to run the cell
221-
notebook_area.send_keys(Keys.SHIFT, Keys.ENTER)
222-
time.sleep(1.0)
141+
next_cell.click()
142+
while True:
143+
spans = driver.find_elements(By.CSS_SELECTOR, "span.jp-StatusBar-TextItem")
144+
status_span = spans[2]
145+
text = status_span.text
146+
147+
if "Idle" in text:
148+
break
149+
time.sleep(0.01)
150+
print(focused_cell.text)
151+
focused_cell=next_cell
152+
run_menu = wait.until(
153+
EC.element_to_be_clickable((By.XPATH, "//li[normalize-space()='Run']"))
154+
)
155+
actions.move_to_element(run_menu).pause(0.05).click().perform()
156+
actions.send_keys(Keys.DOWN).send_keys(Keys.ENTER).pause(0.1).perform()
157+
if not cell_is_waiting_for_input(driver):
158+
while True:
159+
spans = driver.find_elements(By.CSS_SELECTOR, "span.jp-StatusBar-TextItem")
160+
status_span = spans[2]
161+
text = status_span.text
162+
163+
if "Idle" in text:
164+
print(focused_cell.text)
165+
break
166+
time.sleep(0.01)
223167

224168
# In case the notebook stalls during execution
225169
# this makes it so the notebook moves onto the

0 commit comments

Comments
 (0)