-
- Summary
- Get ready
- Principle
- Source
- Core
- Full code
- indexphp
- callpyphp
- Temppy
- Demonstrate
- Home
- Prompt information
- Brief test
- Advanced Testing
- Error hints
- Summarize
Summary
yesterday, a whim, made an online php editing tool http://blog.csdn.net/marksinoberg/article/details/53869637, You can easily practice PHP basic syntax, as well as the operation of the Database. At the end of the line, you might make a Python version of the online editing tool. today, I wrote a python version for students and beginners to practice and use.
Get ready
Reading Yesterday's blog should not be difficult to understand, the working principle behind the Tool.
Upload the source code, run the script, and feedback the Results.
In contrast, this time the code is slightly different, after all, yesterday is the pure PHP code between the processing, and today is the coupling between PHP and Python. So you need to add a bit of extra processing.
PHP environment settings
Before coding, you should set up the PHP environment First. specifically, Modify the PHP.ini file.
By removing the disable_functions in the php.ini file ;
, the extension of the calling external language can be turned On.
Principle
The principle here, in a narrow sense, is simply to invoke Python code with PHP.
There are generally two ways of doing this:
- Through the system function
- Through the EXEC function
Each of these two methods have pros and cons, the next will be a simple introduction to the use of these two functions, as to how to choose, according to their own needs to set up.
System mode
The official documentation is explained Below:
The system function itself has the ability to print commands to perform output, meaning that the output in the program can be displayed in a PHP page.
If the program executes successfully, the return value of system is the last line of the program output and false if execution fails.
The second parameter is optional, used to get the status code after the command executes, 0 means that the external program was successfully called, and 1 indicates that the call Failed.
EXEC mode
Official documents are explained Below:
The exec () function, like system (), also executes the given command, but does not output the result , but instead returns the last line of the Result.
Although it returns only the last line of the command result, the second parameter array gives the complete result by appending the result line to the end of the Array.
It is also important to note that the third parameter can be used to obtain the status code of the command execution only if the second parameter is Specified.
Source Code Core
The core ideas are as Follows:
- Get the user to enter the python source code and upload it to the temp.py file on the Server.
- Use PHP to invoke external Python code and execute related scripts.
- The foreground requests the code to run the result in Ajax mode and is displayed on the result page block.
Full Code index.php
<! DOCTYPE html><html><head><meta charset="UTF-8"><title>Guo Pu's online python tool</title><link rel="shortcut icon" href="favicon.ico" type ="image/x-ico" /><style>. Container { width: 1356px; height: 640px; position: absolute; background: #CCC; }. left { width: %; height: %; background: lightgray; position: relative; float: left; }. Header { width: auto; height: px; }input { width: +-px; height: px ; position: relative ; background: lightgreen ; float: right; margin-right: px ; margin-top: 6px ; Border-radius: px ; box-shadow: 1px 1px 1px #6e6e6e ;}. Panel { width: %; height: 540px; align: center; }textarea { font-size: px;} . right { width: %; height: %; background: deepskyblue; position: relative; float: right; }</style></head><body> <div class="container"> <div class="left"> <div class="header"> <label><font size="5">Write down your<a href="#" id="tip">Python code</a>.</font></label> <input id="btn_run" Type="submit" value ="click to run"></input> </div> <hr> <div class="panel"> <textarea id="source" style="width:645px; height:540px; " name="source" Placeholder="echo ' Hello world! ';" > Print "Hello Guo pu"</textarea> <!--<textarea type= "hidden" id= "hidden" hidden></textarea> -- </div> </div> <div class="right"> <h2>The results of your code execution are shown below</H2> <hr> <div class="panel"> <textarea id="result" style="width:645px; height:540px; " > </textarea> </div> </div> </div> <!--write The commit script and get the return result-- <script src="./js/jquery-2.2.4.min.js"></script> <script> //request Run Results function getresult() {document.getElementById ("result"). value ="code is running ..."; $.ajax ({type:"GET"Url:"./callpy.php", success: function(data) {document.getElementById ("result"). value = data; }, error: function(err) {document.getElementById ("result"). value = err; } }); }//upload The source code to the server function uploadsource() { varSource = document.getElementById ("source"). value; $.ajax ({type:"POST"Url:"./index.php", Data: {"source": source}, success: function(){Console.log ("the Code uploads successfully!" "); }, error: function(err){Console.log ("code Upload failed!" "); Alert (err); } }); }//using Ajax to get the results of execution$ (document). Ready ( function() {document.getElementById ("result"). value ="getting Running Results ..."; $("#btn_run"). Click ( function(){ //upload Code FirstUploadsource ();//request The result after the code is runGetResult (); }); });//click "python code" to display the prompt message$ (document). Ready ( function(){$("#tip"). Click ( function(){document.getElementById ("result"). value ="you can use Python2.7.12 and backwards-compatible python syntax. \ n Note the indentation of your Code. In addition, if you need to connect to the database, please contact Guo Pu. qq:1064319632 "; }); });</script> <!--write PHP scripts to get submission information-- <?php$source = $_post [' source ']; $source = "#coding: utf8\n" . $source; File_put_contents ( "./temp.py", $source ); ?> </body></html>
callpy.php
The functionality required to invoke external code here is simple, so the function is chosen system
.
<?php$command = "python ./temp.py";$flag = system($command, $result);if($flag) { echo $result[0];}else{ echo "不好意思,代码运行出错啦。\n\n\n您的语法有问题哟:\n请检查一下标点符号,代码缩进,单词拼写什么的吧!";}
temp.py
temp.py
plainly, It is a temporary file, so every time the code is run it will be updated Again. So it doesn't matter what's inside. The following gives the contents of the file that I tested once temp.py
.
#coding:utf8print"Hello 郭璞"fromimport *print ctime()
Demonstrate
Here is the exciting test interface.
Home
Prompt information
When you click on the "python code" at the top left, you will be given a hint. Such as:
Brief test
This online editing tool makes it easy to write Python Scripts. As long as it is in line with the normal Python syntax, it is possible.
Run wait
When writing a Python script that compares time-consuming, the foreground needs to give a hint and wait. So the best way is to show a "program is running" ... ", This will give users a better experience, but also to reflect a more humane design."
Advanced Testing
Error hints
It's all about the results of the code WORKING. But most of the time we can't write the bug free code at once, so it's a good choice to give a hint in a timely manner. But the advice here is to self-discovery errors, manually check their own code, more able to develop the code coding Habits.
Summarize
In retrospect, the core of this experiment is the two simple ways in which PHP calls external programs. Although there are pros and cons, but to find the right scene to choose, still can achieve good results.
In fact, all the above is a few irrelevant topics. What really works is the integration of php, which has the incomparable advantage of Python in web development (although Python does not have to write a website at all). But Python's flexibility is not an alternative to php.
Through Today's test, It is not difficult to think of it. If the two can be properly integrated, it must be possible to make an elegant and efficient system.
finally, I uploaded the tool to my server. If there is a desire to practice PHP syntax, python syntax, You can Contact Me.
Contact information can be found at the links at the left. (^__^) hehe ...
Online Python Run tool