3

I would like to create a web service from a simple invoice app I have wrote. I would like it to return json and hopefully pdf files from apache fop. I do not want a html web page, I will access the service from python desktop application.

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.

ЯegDwight
  • 24,821
  • 10
  • 45
  • 52
Martyn Jones
  • 118
  • 5
  • not sure why someone just gave you -1 ... Question seems fine... maybe dont ask if its the right tool but rather how to do it with this tool ... it is a tool and it certainly can accomplish what you want – Joran Beasley Sep 10 '12 at 16:28
  • Noone will come and thump you on the head if you ignore the template section, in any case. If the rest of Flask works for you, by all means use Flask! :-) – Martijn Pieters Sep 10 '12 at 16:30
  • 1
    I gave the -1. AFAIC, "Is ___ the right tool for this job" is a subjective question, and would encourage debate. On the other hand, the "How do I accept multiple inputs in flask" IS a good question for StackOverflow. I'd like to see that be the actual question. – Mark Hildreth Sep 10 '12 at 16:31
  • Edited the question title as Mark Hildreth suggestion, thanks for -1 on my first ever post. I agree that the right tool for the job was subjective (removed). – Martyn Jones Sep 10 '12 at 16:36
  • 1
    @MartynJones: I do believe that your questions regarding "Can flask be used to host a web service" can be a good question, but with some different wording. You already gave one reason why you believe it might not (multiple inputs), and now have an answer as to how to correctly do it. If you believe that flask is too tightly bound to HTML rendering, perhaps another question asking "How can I use Flask to return JSON/XML/ in a response rather than HTML" would be helpful. – Mark Hildreth Sep 10 '12 at 16:44

3 Answers3

4

You sure can ignore the html section. Flask is a nice lightweight way to create a web app. You can just return json (or other data) as responses to all your urls if you want and completely disregard the html templateing.

You can include as many params/regexes as you need in your route definition Each one will create a new param for the function.

@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?

Community
  • 1
  • 1
dm03514
  • 54,664
  • 18
  • 108
  • 145
2

Is flask the right tool for the job?

Flask is a micro python web-framework as Bottle or webpy. To my mind, minimalist web framework is nice for your job.

Do not confuse 'variable rules' which are variable parts to a URL and "classic" arguments of your function.

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()
Lujeni
  • 356
  • 2
  • 10
0

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.

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/mathapi/3/4/ - output= 3 + 4 = 7

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

Martyn Jones
  • 118
  • 5