File tree Expand file tree Collapse file tree 6 files changed +74
-22
lines changed Expand file tree Collapse file tree 6 files changed +74
-22
lines changed Original file line number Diff line number Diff line change 8
8
},
9
9
"devDependencies" : {
10
10
"@wasm-tool/wasm-pack-plugin" : " 0.2.0" ,
11
- "webpack" : " ^4.16.3" ,
12
- "webpack-cli" : " ^3.1.0" ,
13
- "webpack-dev-server" : " ^3.1.5" ,
14
11
"copy-webpack-plugin" : " ^4.5.2" ,
15
- "mini- css-extract-plugin " : " ^0.5.0 " ,
12
+ "css-loader " : " ^2.0.1 " ,
16
13
"html-webpack-plugin" : " ^3.2.0" ,
17
- "css-loader" : " ^2.0.1"
14
+ "mini-css-extract-plugin" : " ^0.5.0" ,
15
+ "raw-loader" : " ^1.0.0" ,
16
+ "webpack" : " ^4.16.3" ,
17
+ "webpack-cli" : " ^3.1.0" ,
18
+ "webpack-dev-server" : " ^3.1.5"
18
19
},
19
20
"scripts" : {
20
21
"dev" : " webpack-dev-server -d" ,
Original file line number Diff line number Diff line change @@ -9,23 +9,22 @@ <h1>RustPython Demo</h1>
9
9
<p >
10
10
RustPython is a Python interpreter written in Rust. This demo is
11
11
compiled from Rust to WebAssembly so it runs in the browser.<br >
12
- Please input your Python code below and click < kbd > Run</ kbd > , or you
13
- can open up your browser's devtools and play with
14
- < code > rp.pyEval('print("a")')</ code >
12
+ Please input your Python code below and click <kbd >Run</kbd >
13
+ (or < kbd >Ctrl+Enter</ kbd >), or you can open up your
14
+ browser's devtools and play with <code >rp.pyEval('print("a")')</code >
15
15
</p >
16
- < textarea id ="code ">
17
- n1 = 0
18
- n2 = 1
19
- count = 0
20
- until = 10
21
-
22
- print("These are the first {} numbers in the Fibonacci sequence:".format(until))
23
-
24
- while count < until:
25
- print(n1)
26
- n1, n2 = n2, n1 + n2
27
- count + = 1
16
+ <div id =" code-wrapper" >
17
+ <textarea id =" code" >
18
+ <%= defaultSnippet %>
28
19
</textarea >
20
+ <select id =" snippets" >
21
+ <% for (const name of snippets) { % >
22
+ < option
23
+ < % if (name == defaultSnippetName) % > selected
24
+ >< %= name % >< / option>
25
+ < % } %>
26
+ </select >
27
+ </div >
29
28
<button id =" run-btn" >Run ▷ ; </button >
30
29
<div id =" error" ></div >
31
30
<h3 >Standard Output</h3 >
Original file line number Diff line number Diff line change @@ -46,4 +46,19 @@ document
46
46
. getElementById ( 'run-btn' )
47
47
. addEventListener ( 'click' , runCodeFromTextarea ) ;
48
48
49
+ const snippets = document . getElementById ( 'snippets' ) ;
50
+
51
+ snippets . addEventListener ( 'change' , ( ) => {
52
+ const selected = snippets . value ;
53
+
54
+ // the require here creates a webpack context; it's fine to use it
55
+ // dynamically.
56
+ // https://webpack.js.org/guides/dependency-management/
57
+ const snippet = require ( `raw-loader!./snippets/${ selected } .py` ) ;
58
+
59
+ editor . setValue ( snippet ) ;
60
+
61
+ runCodeFromTextarea ( ) ;
62
+ } ) ;
63
+
49
64
runCodeFromTextarea ( ) ; // Run once for demo
Original file line number Diff line number Diff line change
1
+ n1 = 0
2
+ n2 = 1
3
+ count = 0
4
+ until = 10
5
+
6
+ print (f"These are the first { until } numbers in the Fibonacci sequence:" )
7
+
8
+ while count < until :
9
+ print (n1 )
10
+ n1 , n2 = n2 , n1 + n2
11
+ count += 1
Original file line number Diff line number Diff line change @@ -25,3 +25,14 @@ textarea {
25
25
margin-top : 10px ;
26
26
font-family : monospace;
27
27
}
28
+
29
+ # code-wrapper {
30
+ position : relative;
31
+ }
32
+
33
+ # snippets {
34
+ position : absolute;
35
+ right : 20px ;
36
+ top : 5px ;
37
+ z-index : 25 ;
38
+ }
Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ const HtmlWebpackPlugin = require('html-webpack-plugin');
2
2
const MiniCssExtractPlugin = require ( 'mini-css-extract-plugin' ) ;
3
3
const WasmPackPlugin = require ( '@wasm-tool/wasm-pack-plugin' ) ;
4
4
const path = require ( 'path' ) ;
5
+ const fs = require ( 'fs' ) ;
5
6
6
7
module . exports = {
7
8
entry : './src/index.js' ,
@@ -12,13 +13,27 @@ module.exports = {
12
13
mode : 'development' ,
13
14
module : {
14
15
rules : [
15
- { test : / \. c s s $ / , use : [ MiniCssExtractPlugin . loader , 'css-loader' ] }
16
+ {
17
+ test : / \. c s s $ / ,
18
+ use : [ MiniCssExtractPlugin . loader , 'css-loader' ]
19
+ }
16
20
]
17
21
} ,
18
22
plugins : [
19
23
new HtmlWebpackPlugin ( {
20
24
filename : 'index.html' ,
21
- template : 'src/index.html'
25
+ template : 'src/index.ejs' ,
26
+ templateParameters : {
27
+ snippets : fs
28
+ . readdirSync ( path . join ( __dirname , 'src/snippets' ) )
29
+ . map ( filename =>
30
+ path . basename ( filename , path . extname ( filename ) )
31
+ ) ,
32
+ defaultSnippetName : 'fibonacci' ,
33
+ defaultSnippet : fs . readFileSync (
34
+ path . join ( __dirname , 'src/snippets/fibonacci.py' )
35
+ )
36
+ }
22
37
} ) ,
23
38
new MiniCssExtractPlugin ( {
24
39
filename : 'styles.css'
You can’t perform that action at this time.
0 commit comments