@@ -10,7 +10,7 @@ export default class PyScriptActiveCode extends ActiveCode {
1010 }
1111
1212 async runProg ( ) {
13- var userCode = await this . buildProg ( true ) ;
13+ var prog = await this . buildProg ( true ) ;
1414 let saveCode = "True" ;
1515 this . saveCode = await this . manage_scrubber ( saveCode ) ;
1616 $ ( this . output ) . text ( "" ) ;
@@ -21,11 +21,7 @@ export default class PyScriptActiveCode extends ActiveCode {
2121 } ) ;
2222 }
2323 $ ( this . outDiv ) . show ( { duration : 700 , queue : false } ) ;
24-
25- // Properly escape and preserve the user's code indentation
26- userCode = userCode . replace ( / \\ / g, '\\\\' ) . replace ( / ` / g, '\\`' ) . split ( '\n' ) . map ( line => ' ' + line ) . join ( '\n' ) ;
27-
28- const prog = `
24+ prog = `
2925 <html>
3026 <head>
3127 <link rel="stylesheet" href="https://pyscript.net/latest/pyscript.css" />
@@ -36,7 +32,7 @@ export default class PyScriptActiveCode extends ActiveCode {
3632 pre {
3733 position: absolute; font-size: 13px; width: 94%; padding: 9.5px; line-height: 1.42857143; border: 1px ; border-radius: 4px;
3834 }
39- code {
35+ code{
4036 border: 1px solid #ccc; border-radius: 4px;
4137 }
4238 </style>
@@ -49,50 +45,49 @@ export default class PyScriptActiveCode extends ActiveCode {
4945 <pre id="consolePre">
5046 <code id="consoleCode"></code>
5147 </pre>
52- <py-repl >
48+ <py-script >
5349import sys
5450from js import document
5551logger = document.getElementById('consoleCode')
5652preElem = document.getElementById('consolePre')
57-
53+
5854class NewOut:
5955 def write(self, data):
6056 logger.innerHTML += str(data)
61- sys.stderr = sys.stdout = NewOut()
62-
63- def my_exec(code):
64- try:
65- exec(code)
66- preElem.style.visibility = "visible"
67- preElem.style.bottom = "5px"
68- logger.classList.add("plaintext")
69- except Exception as err:
70- error_class = err.__class__.__name__
71- detail = err.args[0]
72- line_number = "" # PyScript does not currently expose line numbers
73- result = f"'{error_class}': {detail} {line_number}"
74- print(result)
75- # Styling the pre element for error
76- preElem.style.visibility = "visible"
77- preElem.style.top = "5px"
78- preElem.style.backgroundColor = "#f2dede"
79- preElem.style.border = "1px solid #ebccd1"
80- logger.classList.add("python")
81-
57+ sys.stderr = sys.stdout = NewOut()
58+
59+ def my_exec(code):
60+ try:
61+ exec(code)
62+ preElem.style.visibility = "visible"
63+ preElem.style.bottom = "5px"
64+ logger.classList.add("plaintext")
65+ except Exception as err:
66+ error_class = err.__class__.__name__
67+ detail = err.args[0]
68+ line_number = "" # PyScript does not currently expose line numbers
69+ result = f"'{error_class}': {detail} {line_number}"
70+ print(result)
71+ # Styling the pre element for error
72+ preElem.style.visibility = "visible"
73+ preElem.style.top = "5px"
74+ preElem.style.backgroundColor = "#f2dede"
75+ preElem.style.border = "1px solid #ebccd1"
76+ logger.classList.add("python")
77+
8278# usage
83- my_exec(\`${ userCode } \`)
84- </py-repl>
79+ my_exec("""${ prog }
80+ """)
81+ </py-script>
8582 <script>
8683 hljs.highlightAll();
8784 </script>
8885 </body>
8986 </html>
9087 ` ;
91-
9288 this . output . srcdoc = prog ;
9389 }
94-
95-
90+
9691 createOutput ( ) {
9792 this . alignVertical = true ;
9893 var outDiv = document . createElement ( "div" ) ;
0 commit comments