8000 Slightly imporved pyrepl by WebReflection · Pull Request #1296 · pyscript/pyscript · GitHub
[go: up one dir, main page]

Skip to content

Slightly imporved pyrepl #1296

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 24, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Removed unnecesary label
  • Loading branch information
WebReflection committed Mar 24, 2023
commit 50de4d811a4e0fe7f82eeb98a42138a13a9551f0
31 changes: 4 additions & 27 deletions pyscriptjs/src/components/pyrepl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ export function make_PyRepl(interpreter: InterpreterClient, app: PyScriptApp) {

this <py-repl>
boxDiv <div class='py-repl-box'>
editorLabel <label>...</label>
editorDiv <div class="py-repl-editor"></div>
outDiv <div class="py-repl-output"></div>
</div>
Expand All @@ -35,6 +34,7 @@ export function make_PyRepl(interpreter: InterpreterClient, app: PyScriptApp) {

connectedCallback() {
ensureUniqueId(this);

if (!this.hasAttribute('exec-id')) {
this.setAttribute('exec-id', '0');
}
Expand Down Expand Up @@ -86,10 +86,8 @@ export function make_PyRepl(interpreter: InterpreterClient, app: PyScriptApp) {
boxDiv.className = 'py-repl-box';

const editorDiv = this.makeEditorDiv();
const editorLabel = this.makeLabel('Python Script Area', editorDiv);
this.outDiv = this.makeOutDiv();

boxDiv.append(editorLabel);
boxDiv.appendChild(editorDiv);
boxDiv.appendChild(this.outDiv);

Expand All @@ -98,35 +96,21 @@ export function make_PyRepl(interpreter: InterpreterClient, app: PyScriptApp) {

makeEditorDiv(): HTMLElement {
const editorDiv = document.createElement('div');
editorDiv.id = 'code-editor';
editorDiv.className = 'py-repl-editor';
editorDiv.setAttribute('aria-label', 'Python Script Area');
editorDiv.appendChild(this.editor.dom);

const runButton = this.makeRunButton();
const runLabel = this.makeLabel('Python Script Run Button', runButton);
editorDiv.appendChild(runLabel);
editorDiv.appendChild(runButton);

return editorDiv;
}

makeLabel(text: string, elementFor: HTMLElement): HTMLElement {
ensureUniqueId(elementFor);
const lbl = document.createElement('label');
lbl.innerHTML = text;
lbl.htmlFor = elementFor.id;
// XXX this should be a CSS class
// Styles that we use to hide the labels whilst also keeping it accessible for screen readers
const labelStyle = 'overflow:hidden; display:block; width:1px; height:1px';
lbl 8000 .setAttribute('style', labelStyle);
return lbl;
}

makeRunButton(): HTMLElement {
const runButton = document.createElement('button');
runButton.id = 'runButton';
runButton.className = 'absolute py-repl-run-button';
runButton.innerHTML = RUNBUTTON;
runButton.setAttribute('aria-label', 'Python Script Run Button');
runButton.addEventListener('click', this.execute.bind(this) as (e: MouseEvent) => void);
return runButton;
}
Expand Down Expand Up @@ -170,15 +154,8 @@ export function make_PyRepl(interpreter: InterpreterClient, app: PyScriptApp) {
// should be the default.
autogenerateMaybe(): void {
if (this.hasAttribute('auto-generate')) {
const root = this.getAttribute('root');
const allPyRepls = document.querySelectorAll(`py-repl[root='${root}'][exec-id]`);
const allPyRepls = document.querySelectorAll(`py-repl[root='${this.getAttribute('root')}'][exec-id]`);
const lastRepl = allPyRepls[allPyRepls.length - 1];

// get out if no Repl is found instead of throwing an error
if (lastRepl === null) return;

this.removeAttribute('auto-generate');

const lastExecId = lastRepl.getAttribute('exec-id');
const nextExecId = parseInt(lastExecId) + 1;

Expand Down
5 changes: 2 additions & 3 deletions pyscriptjs/tests/integration/test_py_repl.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@ def test_repl_loads(self):
<py-repl></py-repl>
"""
)
py_repl = self.page.query_selector("py-repl")
py_repl = self.page.query_selector("py-repl .py-repl-box")
assert py_repl
assert "Python" in py_repl.inner_text()

def test_execute_preloaded_source(self):
"""
Expand Down Expand Up @@ -69,7 +68,7 @@ def test_execute_on_shift_enter(self):
</py-repl>
"""
)
self.page.wait_for_selector("#runButton")
self.page.wait_for_selector("py-repl .py-repl-run-button")
self.page.keyboard.press("Shift+Enter")
wait_for_render(self.page, "*", "hello world")

Expand Down
4 changes: 2 additions & 2 deletions pyscriptjs/tests/integration/test_zz_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ def test_repl(self):
wait_for_render(self.page, "*", "<py-repl.*?>")

self.page.locator("py-repl").type("display('Hello, World!')")
self.page.locator("#runButton").click()
self.page.locator("py-repl .py-repl-run-button").click()

assert (
self.page.locator("#my-repl-repl-output").text_content() == "Hello, World!"
Expand All @@ -322,7 +322,7 @@ def test_repl2(self):
wait_for_render(self.page, "*", "<py-repl.*?>")
# confirm we can import utils and run one command
self.page.locator("py-repl").type("import utils\ndisplay(utils.now())")
self.page.wait_for_selector("#runButton").click()
self.page.wait_for_selector("py-repl .py-repl-run-button").click()
# Make sure the output is in the page
self.page.wait_for_selector("#my-repl-1")
# utils.now returns current date time
Expand Down
0