python 如何在pyscript块中运行Flask?

nvbavucw  于 2023-01-19  发布在  Python
关注(0)|答案(2)|浏览(162)

如何使下面的 flask 端点功能与现有的网站一起工作?
带 flask 的Python脚本(运行良好)

from flask import Flask, request

# create the Flask app
app = Flask(__name__)

@app.route('/data')
def query_example():
    # if key doesn't exist, returns None
    P_ID = request.args.get('P_ID')

    # if key doesn't exist, returns None
    P_TITLE = request.args.get('P_TITLE')

    return '''
            <h1>The P_ID is: {}</h1>
            <h1>The P_TITLE value is: {}'''.format(P_ID,  P_TITLE)
            
if __name__ == '__main__':
    # run app in debug mode on port 5000
    app.run(debug=True, port=5000)

下面是我创建的本地网站的截图。(https://i.stack.imgur.com/h54ZG.png
我更新了上面的代码,将.py转换为.html,但是遇到了这个错误:
操作错误:[Errno 50]协议不可用

<script type=module src=main_joy_01_12.js></script><my-header></my-header>

<head>
    <link rel="stylesheet" href="https://pyscript.net/latest/pyscript.css" />
    <script defer src="https://pyscript.net/latest/pyscript.js"></script>
</head>

<html>
    <p>fix title/contaxt</p>
    <h1>this is the data extract from mysql: Nordic85678</h1>
    </body>
</html>

<py-config>
    packages = ["flask"]
</py-config>

<py-script>

    # with pyscript that can run python inside html 
    # following with normal python code, execute independently is working fine

    from flask import Flask, request
    
    # create the Flask app
    app = Flask(__name__)
    
    
    @app.route('/data')
    def query_example():
        # if key doesn't exist, returns None
        P_ID = request.args.get('P_ID')
    
        # if key doesn't exist, returns None
        P_TITLE = request.args.get('P_TITLE')
    
        return ''' 
    
                <script type=module src=main_joy_01_12.js></script><my-header></my-header>
    
    <head>
        <link rel="stylesheet" href="https://pyscript.net/latest/pyscript.css" />
        <script defer src="https://pyscript.net/latest/pyscript.js"></script>
    </head>
    
    <html>
        <p>fix title/contaxt</p>
        <h1>this is the data extract from mysql: {}</h1>
    
        </body>
    </html>
    
    
    '''.format( P_TITLE)
    
                
    if __name__ == '__main__':
        # run app in debug mode on port 5000
        app.run(debug=True, port=5000)
    
</py-script>

<my-footer></my-footer>

下面是完整的错误输出:

Traceback (most recent call last):
  File "/lib/python3.10/site-packages/_pyodide/_base.py", line 435, in eval_code
    .run(globals, locals)
  File "/lib/python3.10/site-packages/_pyodide/_base.py", line 304, in run
    coroutine = eval(self.code, globals, locals)
  File "<exec>", line 235, in <module>
  File "/lib/python3.10/site-packages/flask/app.py", line 1188, in run
    run_simple(t.cast(str, host), port, self, **options)
  File "/lib/python3.10/site-packages/werkzeug/serving.py", line 1062, in run_simple
    s = prepare_socket(hostname, port)
  File "/lib/python3.10/site-packages/werkzeug/serving.py", line 898, in prepare_socket
    s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
OSError: [Errno 50] Protocol not available

这个想法来自于一个原始网站(https://www.happy.com),在那里你输入https://www.happy.com/data?P-ID=en_1-01这样的http,它可以显示输入的数据,比如“你在寻找en_1-01吗?”

hec6srdp

hec6srdp1#

不可能在浏览器内部运行Flask。如果可以的话,这将是一个安全问题。浏览器虚拟机不提供API来打开应用程序的套接字。所有网络I/O必须通过浏览器提供的API。

r7knjye2

r7knjye22#

一个极简主义的例子:

from flask import Flask, request

app = Flask(__name__)

@app.route('/data')
def display():
    value = request.args.get('P-ID')
    html_content = """
<!DOCTYPE html>
<html>
  <head>
    <title>Example</title>
  </head>
  <body>
    <h1>Are you looking for {}</h1>
  </body>
</html>
""".format(value)
    return html_content

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

相关问题