1+ import { ActiveCode } from "./activecode.js" ;
2+
3+ export default class PyScriptActiveCode extends ActiveCode {
4+ constructor ( opts ) {
5+ super ( opts ) ;
6+ opts . alignVertical = true ;
7+ this . python3_interpreter = $ ( orig ) . data ( "python3_interpreter" ) ;
8+ $ ( this . runButton ) . text ( "Render" ) ;
9+ this . editor . setValue ( this . code ) ;
10+ }
11+
12+ async runProg ( ) {
13+ var prog = await this . buildProg ( true ) ;
14+ let saveCode = "True" ;
15+ this . saveCode = await this . manage_scrubber ( saveCode ) ;
16+ $ ( this . output ) . text ( "" ) ;
17+ if ( ! this . alignVertical ) {
18+ $ ( this . codeDiv ) . switchClass ( "col-md-12" , "col-md-6" , {
19+ duration : 500 ,
20+ queue : false ,
21+ } ) ;
22+ }
23+ $ ( this . outDiv ) . show ( { duration : 700 , queue : false } ) ;
24+ prog = `
25+ <html>
26+ <head>
27+ <link rel="stylesheet" href="https://pyscript.net/latest/pyscript.css" />
28+ <script defer src="https://pyscript.net/latest/pyscript.js"></script>
29+ <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/11.0.1/styles/default.min.css">
30+ <script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/11.0.1/highlight.min.js"></script>
31+ <style>
32+ pre {
33+ position: absolute; font-size: 13px; width: 94%; padding: 9.5px; line-height: 1.42857143; border: 1px ; border-radius: 4px;
34+ }
35+ code{
36+ border: 1px solid #ccc; border-radius: 4px;
37+ }
38+ </style>
39+ </head>
40+ <body>
41+ <py-config>
42+ terminal = false
43+ packages = [ "pandas", "numpy", "matplotlib"]
44+ </py-config>
45+ <pre id="consolePre">
46+ <code id="consoleCode"></code>
47+ </pre>
48+ <py-script>
49+ import sys
50+ from js import document
51+ logger = document.getElementById('consoleCode')
52+ preElem = document.getElementById('consolePre')
53+
54+ class NewOut:
55+ def write(self, data):
56+ logger.innerHTML += str(data)
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+
78+ # usage
79+ my_exec("""${ prog }
80+ """)
81+ </py-script>
82+ <script>
83+ hljs.highlightAll();
84+ </script>
85+ </body>
86+ </html>
87+ ` ;
88+ this . output . srcdoc = prog ;
89+ }
90+
91+ createOutput ( ) {
92+ this . alignVertical = true ;
93+ var outDiv = document . createElement ( "div" ) ;
94+ $ ( outDiv ) . addClass ( "ac_output" ) ;
95+ if ( this . alignVertical ) {
96+ $ ( outDiv ) . addClass ( "col-md-12" ) ;
97+ } else {
98+ $ ( outDiv ) . addClass ( "col-md-5" ) ;
99+ }
100+ this . outDiv = outDiv ;
101+ this . output = document . createElement ( "iframe" ) ;
102+ $ ( this . output ) . css ( "background-color" , "white" ) ;
103+ $ ( this . output ) . css ( "position" , "relative" ) ;
104+ $ ( this . output ) . css ( "height" , "400px" ) ;
105+ $ ( this . output ) . css ( "width" , "100%" ) ;
106+ outDiv . appendChild ( this . output ) ;
107+ this . outerDiv . appendChild ( outDiv ) ;
108+ var clearDiv = document . createElement ( "div" ) ;
109+ $ ( clearDiv ) . css ( "clear" , "both" ) ; // needed to make parent div resize properly
110+ this . outerDiv . appendChild ( clearDiv ) ;
111+ }
112+ enableSaveLoad ( ) {
113+ $ ( this . runButton ) . text ( $ . i18n ( "msg_activecode_render" ) ) ;
114+ }
115+ }
0 commit comments