简体   繁体   English

在 Flask 中导入 python 脚本使其共享

[英]Import python script in Flask to make it shareabale

I have a python script Flow.py that has a function choked() which takes user input and performs a set of algebraic calculations and prints the results (see sample code below).我有一个 python 脚本Flow.py ,它有一个 function choked choked() ,它接受用户输入并执行一组代数计算并打印结果(参见下面的示例代码)。 I want to make this file shareable with my colleagues.我想让这个文件与我的同事共享。

I tried it two ways: 1 - Making it a.exe file 2 - Using Flask我尝试了两种方法: 1 - 将其制作为 a.exe 文件 2 - 使用 Flask

Problems faced : 1 - For.exe file, I tried cx_Freeze and pyinstaller , but both gives error.面临的问题: 1 - For.exe 文件,我尝试cx_Freezepyinstaller ,但都给出了错误。 The executable file closes immediately upon clicking it.可执行文件在单击后立即关闭。 2 - In Flask, upon running in shell, it doesn't give any error, however on running on localhost, nothing happens 2 - 在 Flask 中,在 shell 中运行时,它没有给出任何错误,但是在 localhost 上运行时,没有任何反应

from flask import Flask 

app = Flask(__name__)

@app.route('/')
def choked():     #this is my Flow.py
    
    e1 = input('Enter value 1: ')  # var type = str
    e2 = input('Enter value 2: ')  # var type = str
    e3 = input('Enter value 3: ')  # var type = str
        
    G = e1*e2*e3

    print ()
    
if __name__ == "__main__":
    app.run(host='0.0.0.0', port='8000', debug=True)

Problem with that is Flask is used for routing and input expects to read from stdin , you need to define it as a POST request, where e1, e2, e3 are specified as URI parameters then do the computation on them and return the response.问题在于 Flask 用于路由, input期望从stdin读取,您需要将其定义为 POST 请求,其中 e1、e2、e3 被指定为 URI 参数,然后对它们进行计算并返回响应。

@app.route('/<string:e1>/<string:e2>/<string:e3>'):
def choked(e1, e2, e3):
    G = e1 * e2* e3
    return G

And when flask is running--而当 flask 运行时——

$ curl localhost:8000/<e1>/<e2>/<e3>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM