13

Python is an extremely popular language, especially for beginners, who also happen to be the people who have the hardest time understanding how to post a MCVE as well as a traceback or output. Being able to comment asking posters to include a runnable snippet would improve a great many questions.

Skulpt overcomes the implementation difficulties of snippets in Python since the code does not have to run on a server; it is an entirely in-browser implementation of Python that doesn’t require any preprocessing, plugins, or server-side support; just write Python and reload.

It should simply be a matter of loading one or two javascript libraries when needed.

17
  • 5
    Personally I think that Stack Snippets is best for web languages (PHP, JS, etc.). Python with Skulpt would work, but then it'd open up requests for other languages as well such as C++ and as a result, would probably slow down the network.
    – AStopher
    Commented May 17, 2016 at 11:48
  • 1
    @cybermonkey why would they slow down the network?
    – Alex Hall
    Commented May 17, 2016 at 11:50
  • 1
    More languages equals more resources, would put more pressure on the network. Stack Snippets could potentially have a dedicated team working on it, and dedicated resources (i.e, a server or two dedicated to it). I'm not sure that the benefits would outweigh the security issues here.
    – AStopher
    Commented May 17, 2016 at 11:52
  • @cybermonkey are you still talking about snippets running only in the browser?
    – Alex Hall
    Commented May 17, 2016 at 11:52
  • 1
    I'm talking about snippets. The problem with Stack Snippets is the resources; if you take languages such as C++, compilation and execution would have to be done on the server. As far as I know, no third-party website has ever been integrated into Snippets, and it's the same situation with the SQL Snippets.
    – AStopher
    Commented May 17, 2016 at 11:56
  • 2
    @cybermonkey So only accept requests for languages that can run in the browser.
    – Alex Hall
    Commented May 17, 2016 at 11:57
  • 1
    If a Python runnable snippet feature was to ever be completed, Skulpt wouldn't be used and it would be likely that the Stack Overflow team would develop their own version of Skulpt for Stack Snippets.
    – AStopher
    Commented May 17, 2016 at 11:58
  • @cybermonkey why wouldn't Skulpt be used?
    – Alex Hall
    Commented May 17, 2016 at 11:59
  • 3
    The reasoning is the same used for link-only answers; links rot (aka, Skulpt can go offline at any time, and break the Stack Exchange feature).
    – AStopher
    Commented May 17, 2016 at 11:59
  • Let us continue this discussion in chat.
    – Alex Hall
    Commented May 17, 2016 at 12:00
  • 3
    That is a great userscript idea!
    – Kyll
    Commented May 17, 2016 at 12:07
  • 1
    @cybermonkey - Stack Exchange could host it on their CDN, and integrate it into Stack Snippets, it's just a JS library. There would be no dependency on any external websites this way. Commented May 17, 2016 at 13:22
  • @cybermonkey, please don't encourage people to use Stack Snippets for PHP. HTML, JavaScript and CSS are the targets for Snippets. Commented May 17, 2016 at 15:54
  • 8
    "It should simply be a matter of loading one or two javascript libraries when needed." Let's forget for the moment that these JavaScript libraries will have to be decidedly large. Let's forget for the moment that at least 2/3rds of the reason for Python's popularity is its vast library of modules, which would not be available to a browser implementation. Even with all of that, what assurances are there that Skulpt actually conforms to the proper behavior of Python? At least with web languages, you're actually executing them in their real environment. Commented May 18, 2016 at 4:54
  • 2
    Thanks for informing me about Skulpt, this is very cool! However, it seem like it could too much from the standard CPython environment to be used as a reliable substitution. Commented May 27, 2016 at 2:15

1 Answer 1

9

You can already do this with normal Snippets. (Please add your code twice if you do this, it's too cluttered to find it in the HTML.)

I'm still not convinced this is a fantastic idea, however. One press of the "tidy" button and it might mess up all your precious spaces.

<html> 
<head> 
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js" type="text/javascript"></script> 
<script src="http://skulpt.org/js/skulpt.min.js" type="text/javascript"></script> 
<script src="http://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> 

(Code from the docs)

1
  • 9
    This is a nice idea but my point was to have something beginner friendly, where they can click the 'create a snippet' button and select Python as the language and nothing more. I want something that practically forces them to make a complete verifiable example.
    – Alex Hall
    Commented May 28, 2016 at 13:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .