/** * CustomePythonScript * Script to Get the Python source code from textbox, compile it and display output * @author Guru99 * Will get code from each
tag and load it inside the Editor
* On Clicking Run button it will compile the code in the browser
* Will Display the output
* @version 1.1 - 24-Feb-14 -Initial Version
* @version 2.1 - 20-May-14 - Ramji Goyal - Code commented and Made same as PHP Editor
* Used jQuery instead of "$" for removing any jQuery Conflicts
*/
/*******************************************Define Code mirror for each pre tag having class=codepython**************************************************/
jQuery(document).ready(function()
{
// for each pre tag
jQuery('pre.codepython').each(function()
{
var pre = this;
var form = jQuery('form[name=samplepython]').clone(); //make clone of form
jQuery(form).removeAttr('name'); //remove name and hidden style
jQuery(form).removeClass('hidden');
jQuery(jQuery(form).find('textarea')[0]).val(jQuery(pre).text()); //copy pre tag value to form textarea
jQuery(pre).replaceWith(form); //replace pre tag with form
});
var editors = []; //array of editor
// for each textarea tag applying codemirror
jQuery('textarea[name=codepython]').each(function()
{
//set code mirror property look and feel
var editor = CodeMirror.fromTextArea(this,
{
lineNumbers: true,
matchBrackets: true,
mode: "text/x-python",
htmlMode: true,
tabMode: "shift"
});
editors.push(editor); //push this editor in to editors aray
});
/*******************************************************************************************************************************************************/
/*******************************************Compile code and Display Output when Run button clicked************************************************/
// on submit this function will call
jQuery('form[id=frm_compilepython]').submit(function()
{
var form = this;
var myCodeMirror = jQuery(this).find('div textarea[name=codepython]'); // get first textarea of form
var prog = jQuery(myCodeMirror).val(); // get value of codemirror
window.stdout = jQuery(form).find('#output')[0]; //set global variable for output iframe
if(!prog){alert('Enter Python code to Run');return false;} // check if textarea is empty then display alert box
jQuery(stdout).contents().find('body').empty(); // Clear the iframe first if having data in it
jQuery(stdout).removeClass('hidden');
//alert(prog);
Sk.configure({output:function(text){
text = text.replace(/'; // store the value in str variable
console.log(ibody.html());
autoResize(stdout); // resize iframe as per the content of output
ibody.html(str); // print the output into iframe
}});
try {
Sk.importMainWithBody("", false, prog); // run the code of python
} catch (e) {
alert(e);
}
return false;
});
function autoResize(id) {
var newheight;
var newwidth;
if (document.getElementById) {
newheight = id.contentWindow.document.body.scrollHeight;
newwidth = id.contentWindow.document.body.scrollWidth;
}
id.height = (newheight) + "px";
id.width = (newwidth) + "px";
}
});
/*******************************************************************************************************************************************************/