forked from trinketapp/pygal.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo-skulpt.html
159 lines (137 loc) · 5.36 KB
/
demo-skulpt.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<html>
<head>
<script src="bower_components/skulpt/skulpt.js" type="text/javascript"></script>
<script src="bower_components/skulpt/skulpt-stdlib.js" type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
</head>
<body>
<script type="text/javascript">
// output functions are configurable. This one just appends some text
// to a pre element.
function outf(text) {
var mypre = document.getElementById("output");
mypre.innerHTML = mypre.innerHTML + text;
}
function builtinRead(x) {
if (Sk.builtinFiles !== undefined && Sk.builtinFiles["files"][x] !== undefined) {
return Sk.builtinFiles["files"][x];
}
if (Sk.externalLibraries[x]) {
var externalLibraryInfo = Sk.externalLibraries[x];
return Sk.misceval.promiseToSuspension(
new Promise(function(resolve, reject) {
// get the main skulpt extenstion
var request = new XMLHttpRequest();
request.open("GET", externalLibraryInfo.path);
request.onload = function() {
if (request.status === 200) {
resolve(request.responseText);
} else {
reject("File not found: '" + x + "'");
}
};
request.onerror = function() {
reject("File not found: '" + x + "'");
}
request.send();
}).then(function (code) {
if (!code) {
throw new Sk.builtin.ImportError("Failed to load remote module '" + name + "'");
}
var promise;
function mapUrlToPromise(path) {
return new Promise(function(resolve, reject) {
scriptElement = document.createElement("script");
scriptElement.type = "text/javascript";
scriptElement.src = path;
scriptElement.async = true
scriptElement.onload = function() {
resolve(true);
}
document.body.appendChild(scriptElement);
});
}
if (externalLibraryInfo.loadDepsSynchronously) {
promise = (externalLibraryInfo.dependencies || []).reduce((p, url) => {
return p.then(() => mapUrlToPromise(url));
}, Promise.resolve()); // initial
} else {
promise = Promise.all((externalLibraryInfo.dependencies || []).map(mapUrlToPromise));
}
return promise.then(function() {
return code;
}).catch(function() {
throw new Sk.builtin.ImportError("Failed to load dependencies required for " + name);
});
})
);
}
throw "File not found '" + x + "'";
}
// Here's everything you need to run a python program in skulpt
// grab the code from your textarea
// get a reference to your pre element for output
// configure the output function
// call Sk.importMainWithBody()
function runit() {
var prog = document.getElementById("yourcode").value;
var mypre = document.getElementById("output");
mypre.innerHTML = '';
Sk.pre = "output";
Sk.configure({output:outf, read:builtinRead});
var myPromise = Sk.misceval.asyncToPromise(function() {
return Sk.importMainWithBody("<stdin>", false, prog, true);
});
myPromise.then(function(mod) {
console.log('success');
},
function(err) {
console.log(err.toString());
});
}
Sk.externalLibraries = {
'./pygal/__init__.js' : {
path : '__init__.js',
dependencies : [
'https://cdnjs.cloudflare.com/ajax/libs/highcharts/6.0.2/highcharts.js',
'https://cdnjs.cloudflare.com/ajax/libs/highcharts/6.0.2/js/highcharts-more.js'
],
loadDepsSynchronously: true
}
};
Sk.domOutput = function(html) {
console.log(html);//-->here get the div tag with no content
//return $('body').append(html).children().last();
return $('#mycanvas').append(html).children().last();
};
$(function (){
runit()
})
</script>
<h3>Try This</h3>
<form>
<textarea id="yourcode" cols="80" rows="30">import pygal
# Our data is a list of series.
# Each series is a list with a label and a list of data points
data = [["Firefox", [None, None, 0, 16.6, 25, 31, 36.4, 45.5, 46.3, 42.8, 37.1]],
["Chrome", [None, None, None, None, None, None, 0, 3.9, 10.8, 23.8, 35.3]],
["IE", [85.8, 84.6, 84.7, 74.5, 66, 58.6, 54.7, 44.8, 36.2, 26.6, 20.1]],
["Others", [14.2, 15.4, 15.3, 8.9, 9, 10.4, 8.9, 5.8, 6.7, 6.8, 7.5]]]
# Make a Pygal chart
stackedline_chart = pygal.StackedLine(fill=True)
stackedline_chart.title = "Browser usage evolution (in %)"
stackedline_chart.x_labels = map(str, range(2002, 2013))
# For each series in our data, add label and data points
for label, data_points in data:
stackedline_chart.add(label, data_points)
# Render the chart
stackedline_chart.render()
# Example modified from the pygal.org documentation
</textarea><br />
<button type="button" onclick="runit()">Run</button>
</form>
<pre id="output" ></pre>
<!-- If you want turtle graphics include a canvas -->
<div id="mycanvas"></div>
</body>
</html>