The html web page calls the back-end python code method

When we use html code to make web pages, we can use the following methods to call python code:

1. Simple python code, such as outputting 'hello world', can be called by directly writing python code on the web page. At this time, we need to understand Pyscript . The following is the code to run the simple python statement directly on the web page:

 <html>
		<head>  
			<meta charset="utf-8">  
		  </head>  
        <body>
		   <pyscript> print('Hello world') </pyscript>
        </body>
     </html>

2. When the python code is a little more complicated and it is in the early stage of webpage construction, we can consider using the flask framework to make the overall layout of the buttons on the webpage.

method 1)

When the web page code is relatively simple, you can directly replace render_template with html code:

test1.py

def run():
  print('hello world')
run()

test.py (including flask package)

from flask import(
    Flask, render_template, request, redirect, globals
)
import test1

app= Flask(__name__)

@app.route("/",methods=['GET', 'POST'])
def index():
     return '<form action = "http://localhost:5000/b" method = "post"></form><a href="/test"><button onclick="">进入测试</button></a><a href="/test1">
    

@app.route("/test",methods=['GET', 'POST'])
def test():
    test1.run()
    return '<form action = "http://localhost:5000/b" method = "post"></form><a href="/test"><button onclick="">进入测试</button></a>
if __name__ == '__main__':
    app.run(debug=True)

Run test1.py, ctrl+click to open the URL in the terminal as shown below:

 

Click the button to run and the word hello will appear.

Method 2) 

When the web page code is complex and long, you can use render_template for front-end and back-end interaction. At this point we need to create a new template folder under the same folder as the python code containing flask:

The test.py code is the same as above,

b.html

<html>
		<head>  
			<meta charset="utf-8">  
		  </head>  
        <body>   
           <form action = "http://localhost:5000/" method = "post">
           </form>
		   <a href="/test"><button onclick="">进入测试</button></a>
        </body>
     </html>

 test1.py

from flask import(
    Flask, render_template, request, redirect, globals
)
import test1

app= Flask(__name__)

@app.route("/",methods=['GET', 'POST'])
def index():
     return  render_template(
        "b.html"
     )
    

@app.route("/test",methods=['GET', 'POST'])
def test():
    test1.run()
    return render_template(
         "b.html"
     )

if __name__ == '__main__':
    app.run(debug=True)

 The test method is the same as method 1), so I won’t go into details here.

3. In the early stage of web design, the above two methods are sufficient, but bloggers only found out halfway through the design of the web page. After writing the pure Html file in the early stage and then using the flask framework to design the button to respond to the python script, the web page will be unstable. , The blogger's pictures and web page jumps are gone. After research, the blogger found a python script calling method that can be used no matter in the early or mid-term of web design! That is the ActiveX control.

This control is only available in IE browser (at least the blogger did not find this control in other familiar browsers), before we want to use it, we need to check whether our IE browser has enabled the ActiveX control . Manually opening the ActiceX control of IE requires the following steps: Open Settings - Internet Options - Security - Custom Level - click Enable or Prompt for all options related to ActiveX.

 Then we run the code to test it.

a.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="gb2312">
    <title>ceshi</title>
<script language="javascript">   
function exec1 (command) {   
  var ws = new ActiveXObject("WScript.Shell");   
  ws.exec(command);   
}   
</script> 
  </head>
  <body>
    <button class='button1' onclick="exec1('python  D:/xgcs/test1.py')">执行程序</button></p>
  </body>
</html>

Use IE browser to open the URL, click the button to run.

 A pop-up window will appear as shown below before running, just click Yes and Allow. 

 Since it is an output, it is normal for the black box to disappear in a flash. If you want to see the printed hello world, you have to add an input(). Or if your python runs as a ui window, it will stay for a long time, just don't click on the black box.

I hope the experience of the above bloggers can be helpful to everyone, hee hee.

 

Guess you like

Origin blog.csdn.net/yong_gan_niu_niu/article/details/127885904