简体   繁体   English

如何将下面的示例代码转换为接受多个输入?

[英]How do I turn the sample code below into accepting multiple inputs?

I would like to create a web service from a simple invoice app I have wrote. 我想从我写的一个简单的发票应用程序创建一个Web服务。 I would like it to return json and hopefully pdf files from apache fop. 我希望它能从jache fop返回json和希望的pdf文件。 I do not want a html web page, I will access the service from python desktop application. 我不想要一个html网页,我将从python桌面应用程序访问该服务。

Can I ignore the template section of the documentation? 我可以忽略文档的模板部分吗?

The most trouble I am having is accepting multiple parameters for a function. 我遇到的最大问题是接受函数的多个参数。

How do I turn the sample code below into accepting multiple inputs? 如何将下面的示例代码转换为接受多个输入?

@app.route('/post/<int:post_id>')
def show_post(post_id):
    # show the post with the given id, the id is an integer
    return 'Post %d' % post_id

I am new to programming and even more so to web services, if I am going about this in the wrong manner please let me know. 我是编程新手,对Web服务更是如此,如果我以错误的方式解决这个问题,请告诉我。

You sure can ignore the html section. 你肯定可以忽略html部分。 Flask is a nice lightweight way to create a web app. Flask是一种很好的轻量级创建Web应用程序的方法。 You can just return json (or other data) as responses to all your urls if you want and completely disregard the html templateing. 如果需要,您可以将json(或其他数据)作为对所有网址的响应返回,并完全忽略html模板。

You can include as many params/regexes as you need in your route definition Each one will create a new param for the function. 您可以在路径定义中包含所需数量的params / regex每个都将为该函数创建一个新参数。

@app.route('/post/<int:post_id>/<int:user_id>/')
def show_post(post_id, user_id):
    # show the post with the given id, the id is an integer
    return 'Post %d' % post_id

Does Flask support regular expressions in its URL routing? Flask是否在其URL路由中支持正则表达式?

Is flask the right tool for the job? 烧瓶是适合这项工作的合适工具吗?

Flask is a micro python web-framework as Bottle or webpy. Flask是一个微型python web框架,如Bottle或webpy。 To my mind, minimalist web framework is nice for your job. 在我看来,简约的Web框架对您的工作很有帮助。

Do not confuse 'variable rules' which are variable parts to a URL and "classic" arguments of your function. 不要将作为可变部分的“变量规则”混淆为URL和函数的“经典”参数。

from flask import Flask
app = Flask(__name__)

@app.route('/post/<int:post_id>', defaults={'action': 1})
def show_post(post_id, action):
    # show the post with the given id, the id is an integer
    # show the defauls argument: action.
    response = 'Post %d\nyour argument: %s' % (post_id, action)
    return response

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

Thanks everyone for all your answers, they have help a lot. 感谢大家的所有答案,他们帮了很多忙。 Below is some working sample code, stepping up from no parameters to parameters and then parameters with json response. 下面是一些工作示例代码,从没有参数升级到参数,然后是json响应的参数。

Hopefully the code below will help someone. 希望下面的代码可以帮助某人。

from flask import Flask
from flask import jsonify
app = Flask(__name__)

@app.route('/')
def helloworld():
    response = 'HelloWorld'
    return response

@app.route('/mathapi/<int:x>/<int:y>/',  methods = ['GET'])
def math(x, y):
    result = x + y # Sum x,t -> result
    response = '%d + %d = %d' %(x, y, result) #Buld response
    return response #Return response

@app.route('/mathapijson/<int:x>/<int:y>/', methods = ['GET'])
def mathjs(x, y):
    result = x + y #Sum x,t -> result
    data = {'x'  : x, 'y' : y, 'result' : result} #Buld arrary
    response = jsonify(data) #Convert to json
    response.status_code = 200 #Set status code to 200=ok
    response.headers['Link'] = 'http://localhost'

    return response #return json response

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

Usage: 用法:

localhost:port/ - Output - HelloWorld localhost:port / - 输出 - HelloWorld

localhost:port/mathapi/3/4/ - output= 3 + 4 = 7 localhost:port / mathapi / 3/4 / - output = 3 + 4 = 7

localhost:port/mathapi/mathapijson/3/4/ - output= {"y": 4, "x": 3, "result": 7} localhost:port / mathapi / mathapijson / 3/4 / - output = {“y”:4,“x”:3,“result”:7}

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

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