Skip to content

Instantly share code, notes, and snippets.

@bnmnetp
Last active October 15, 2023 09:11
Show Gist options
  • Star 24 You must be signed in to star a gist
  • Fork 20 You must be signed in to fork a gist
  • Save bnmnetp/4650616 to your computer and use it in GitHub Desktop.
Save bnmnetp/4650616 to your computer and use it in GitHub Desktop.
Here is about the simplest example I can think of that gets you a working skulpt environment. This little sample shows regular python as well as importing a module.
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js" type="text/javascript"></script>
<script src="http://www.skulpt.org/js/skulpt.min.js" type="text/javascript"></script>
<script src="http://www.skulpt.org/js/skulpt-stdlib.js" type="text/javascript"></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)
throw "File not found: '" + x + "'";
return Sk.builtinFiles["files"][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});
(Sk.TurtleGraphics || (Sk.TurtleGraphics = {})).target = 'mycanvas';
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());
});
}
</script>
<h3>Try This</h3>
<form>
<textarea id="yourcode" cols="40" rows="10">import turtle
t = turtle.Turtle()
t.forward(100)
print "Hello World"
</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>
@NatiThai
Copy link

I have a big problem, I would like to create a function check(Javascript) text form output but I don't know how to create that
I need a solution please help.

Best regards

@mr-douglas
Copy link

mr-douglas commented Jul 11, 2020

This seems to run Python 2, is there a Python 3 version?
(I tried print(6/5) and print 6 / 5 and both seemed to behave like Python 2)

@justinwride
Copy link

This seems to run Python 2, is there a Python 3 version?
(I tried print(6/5) and print 6 / 5 and both seemed to behave like Python 2)

Check out this issue skulpt/skulpt#777

@gracekim-14
Copy link

The example worked well for me but I cannot seem to prompt the user for an input using turtle.textinput().

This is the beginning of my code:

import turtle

screen = turtle.Screen()
screen.setup(width=500, height=400)
user_bet = screen.textinput("Place your bet","Which turtle do you think will win? Enter a colour: ")

And it returns the error:
AttributeError: 'Screen' object has no attribute 'textinput' on line 7

@gracekim-14
Copy link

I have python projects that need to import/modify other files. How can I do this using skulpt?

@SamHastings1066
Copy link

"Uncaught ReferenceError: Sk is not defined skulpt"

To avoid the error above I had to remove the www. from the script URLs. See this stackoverflow answer.

@jhlchan
Copy link

jhlchan commented Oct 15, 2023

Please fix this by replacing the lines:

<script src="http://www.skulpt.org/js/skulpt.min.js" type="text/javascript"></script> 
<script src="http://www.skulpt.org/js/skulpt-stdlib.js" type="text/javascript"></script> 

with these two lines:

<script src="https://skulpt.org/js/skulpt.min.js" type="text/javascript"></script>
<script src="https://skulpt.org/js/skulpt-stdlib.js" type="text/javascript"></script>

This will help people reading Get Started with Skulpt, especially the section Using Skulpt with HTML with an example that works immediately.

Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment