General developer forum

 
 
Picture of philippe juhel
How can I include a Python interpreter written in Javascript in Moodle?
 

Hi,

On a Moodle HTML page, I would like to insert a place to execute some Python code like on this web site (http://interactivepython.org/courselib/static/thinkcspy/SimplePythonData/simpledata.html#input). So, the students won't need to install a Python interpreter on their computer to test their code, they will be able to test it directly inside the Moodle page.

I want to useBrython (http://www.brython.info) which is a Python 3 interpreter written in javascript. I try the HelloWorld example (https://code.google.com/p/brython/wiki/HelloWorld) which code is :

<html>
<head>
<scriptsrc="brython.js"></script>
</head>
<bodyonload="brython()">
<scripttype="text/python">
doc
<="Hello world !"
</script>
</body>
</html>

I need to include the Javascript file (brython.js) and load the javascript function brython(). To do that, I add this two lines on the HEAD part of HTML (see https://moodle.org/mod/forum/discuss.php?d=189403) but when I add this line on the HEAD part, I can't now deploy the setting block.

So, where should I put the javascript code? Or, do you have an idea / solution to have a Python 3 interpreter inside an HTML page?

I use Moodle 2.3.3.

Best regards,

 

Philippe

 
Average of ratings: -
Picture of Gary Sutcliff
I am not sure of your need but....
 

1) Do you want to execute code from within Moodle where the student clicks and sees "Hello World"?  In a different application within Moodle, I have been able to call external information/library/code by using a full URL to let the software see it.  If this is the case, I will see if I can find my file again.

B) If it is a student application and the student is using some kind of editor for this situation, you can use the free Komodo editor you should be able to set it up to automatically include Brython,js code.

 

 

 
Average of ratings: -
Picture of philippe juhel
Re: I am not sure of your need but....
 

Here's an example of what I want :

Inside a Moodle HTML page, I would like to have a textarea. In this text area, the student writes some Python code (1/) then he clicks on the 'Run' button (2/) and the result is printed below (3/).

It's not an external application, it's some Javascript. When the student clicks on the 'Run' button, the content of the textarea is sent to a Python interpreter written in Javascript and the result is printed on a DIV (part /3). So , I need to load a javascript file (brython.js) and call a javascript function (brython()) when the HTML is loaded and that's what I can't do?

 

But if you know an other solution to get an Python interpreter inside a Moodle HTML page, it's OK.

Philippe

 
Average of ratings: -
Picture of Richard Oelmann
Re: I am not sure of your need but....
Group Particularly helpful MoodlersGroup Testers

It may be that you are looking for something like the virtual programming lab plugin

http://vpl.dis.ulpgc.es/

Richard

 
Average of ratings: -
Picture of John Gifford
Re: I am not sure of your need but....
Group Particularly helpful Moodlers

Just had a look at this VPL for Moodle site and it seems to rely on an Ubuntu "Jail" server. Our Moodle runs on a Windows platform. Do you know if it's possible to run VPL either without that Jail server or is there a way of setting up a similar server in Windows?

 
Average of ratings: -
Picture of Richard Oelmann
Re: I am not sure of your need but....
Group Particularly helpful MoodlersGroup Testers

The Ubuntu jail server runs on a virtual machine, so you could host that VM on any server, regardless of its native OS.

 
Average of ratings: -
Picture of Alex Walker
Re: How can I include a Python interpreter written in Javascript in Moodle?
Group Particularly helpful Moodlers

Hi,

I've moved your question from the 'General Help' forum to the 'General Developer' forum. The people who know Moodle and Javascript well hang out in there, so it's more likely they'll see your question in there.

Alex

 
Average of ratings: -
Picture of Adam Morris
Re: How can I include a Python interpreter written in Javascript in Moodle?
 

You put that file somewhere where the web server has access to it. You could just put into the top level wherever Moodle's code lives, but probably a good idea to name it something a bit esoteric, and set the permissions accordingly.

Then in the src= part you put the url of your site, / and then the name you chose. The web server will then serve up that Javascript and you should be good to go.

 
Average of ratings: -
Picture of Andrew Nicols
Re: How can I include a Python interpreter written in Javascript in Moodle?
Group DevelopersGroup Moodle HQGroup Particularly helpful MoodlersGroup Testers

Sorry for joining the game late here.

I've been trying to find any semblance of coherent documentation for this, but without much avail. It seems that whilst you can include the standard file easily, and have it process your python, there isn't any sane way of getting it to re-evaluate your code on a button click (for example).

Since they don't even give any indication as to where you place your code, I can but presume it evals any python in a <script type="text/python"> tag. I assume therefore that the brython.js file needs to be included before any of these script tags, therefore you'll have to use the $PAGE->requires->js('/path/to/your/module/js/brython.js', true); call - note, you need to use true for the second parameter to ensure that the js is included in the header code.

You don't say what it is that you're developing (module, assignment subtype, local plugin, block, etc)... this may give us more context.

Andrew

 
Average of ratings: -
Picture of Hubert Chathi
Re: How can I include a Python interpreter written in Javascript in Moodle?
 

There is actually a console demo page that does that.  If you look at the source, the "run" button executes a Python function (the run function in http://www.brython.info/tests/console.py) that uses "eval" to evaluate the entered program.

My concern would be security: if there isn't a way to sandbox the code, and you run student-submitted code, it could do something bad, since it has access to the entire page.

 
Average of ratings:Useful (1)
Picture of philippe juhel
Re: How can I include a Python interpreter written in Javascript in Moodle?
 

Yes, this is this kind of console that I want to use but to do that, if I look the source code, I see that I must call the javascript function nammed 'brython' when the body is loaded (<body onload="brython({debug:1, cache:'version'})"> and I don't know how I can use the onload event with Moodle. 

This is my problem and if you have a solution...

 

Philippe

 
Average of ratings: -