Skip to content

Commit 54dac36

Browse files
author
ax4
committed
#initial setup
an attempt
0 parents  commit 54dac36

File tree

3 files changed

+122
-0
lines changed

3 files changed

+122
-0
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Python Fiddle with node.js implement (python-shell + sockect.io)
2+
3+
A backend interpreted .py shell example.
4+
5+
To run
6+
7+
```
8+
npm install python-shell
9+
npm install sockect.io
10+
```

client.html

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<!DOCTYPE html>
2+
3+
<html>
4+
<head>
5+
<title>PY_shell demo</title>
6+
</head>
7+
<body>
8+
<p><textarea rows="4" cols="50" id="code">
9+
print(100)
10+
print(200)
11+
</textarea></p>
12+
<p><input type='button' id='run' value='Run !' /></p>
13+
<div id='message'></div>
14+
<script src='https://cdn.socket.io/socket.io-1.4.5.js'></script>
15+
<script>
16+
var socket = io(window.location.origin);
17+
var connection = -1;
18+
socket.on('connect', function () { connection += 1; });
19+
20+
// emit the .py code from input box
21+
document.getElementById('run').onclick = function () {
22+
socket.emit('run', {
23+
code: document.getElementById('code').value,
24+
},
25+
function (res) {
26+
document.getElementById('message').innerHTML += (
27+
'<p>Timestamp: ' + res.timestamp + '</p>'+
28+
'<p>Code: ' + res.code + '</p>'+
29+
'<p>StdOut: ' + res.result + '</p>'
30+
)
31+
});
32+
};
33+
</script>
34+
</body>
35+
</html>

server.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
'use strict'
2+
3+
var http = require('http');
4+
var port = process.env.port || 1337;
5+
var fs = require('fs');
6+
var io = require('socket.io');
7+
var PythonShell = require('python-shell');
8+
9+
// web content
10+
var client = fs.readFileSync('client.html');
11+
12+
// http server: send the web content
13+
var server = http.createServer(function (req, res) {
14+
res.writeHead(200, {});
15+
res.end(client);
16+
});
17+
18+
// socket.io server: exchange data dynamically
19+
var ioserver = io(server);
20+
21+
ioserver.on('connection', function (socket) {
22+
// send the std_out back
23+
socket.on('run', function (req, res) {
24+
// write a temp file for .py code
25+
var uid = Date.now();
26+
var source = "./tmp/input/" + uid + ".py";
27+
var dist = "./tmp/output/" + uid;
28+
fs.writeFile(source, req.code, function (err) {
29+
if (err) {
30+
return console.log(err);
31+
}
32+
console.log("The input file was saved!");
33+
});
34+
fs.writeFile(dist, "", function (err) {
35+
if (err) {
36+
return console.log(err);
37+
}
38+
console.log("The output file was saved!");
39+
});
40+
41+
// run .py code and hear from stdout
42+
var pyshell = new PythonShell(source);
43+
pyshell.on('message', function (stdout) {
44+
var temp = stdout;
45+
console.log(temp);
46+
fs.appendFile(dist, temp, function (err) {
47+
if (err) {
48+
return console.log(err);
49+
}
50+
console.log("The output file was appended!");
51+
});
52+
});
53+
54+
pyshell.end(function () {
55+
fs.readFile(dist,"utf-8", function (err, data) {
56+
if (err) {
57+
return console.log(err);
58+
}
59+
console.log("hello read file");
60+
console.log(data);
61+
res({
62+
timestamp: Date.now(),
63+
file: pyshell.script,
64+
code: req.code,
65+
result: data,
66+
});
67+
});
68+
69+
});
70+
});
71+
72+
73+
});
74+
75+
// run the server
76+
77+
server.listen(port);

0 commit comments

Comments
 (0)