0

I am a complete newbie to API and python. actually, after getting disappointed to find a free host supporting plumber in R I decided to try it by python. The simple problem is that I have a simple function which takes two numeric arguments and using a given CSV file do some calculations and returns a number (I have simply made this in R by the plumber in localhost). now for a test in python have written below code:

from flask import Flask

app = Flask(__name__)

@app.route("/")
def hello():
    return "hello world!"

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

well, this correctly works. but when I try to make a function to take arguments like this:

from flask import Flask

app = Flask(__name__)

@app.route("/")
def hello(a):
    return a + 2

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

I get this page which says I have not passed the arguments.

enter image description here

my main question is that how I can pass the arguments? (in API created by R plumber, for example, I call it like: localhost/5000/?a=2 )

my another question is, could be this kind of API host and request in something like Heroku?

1
  • I could get you an answer, but it is better for you and benefit you for long run to read the documentation about Variable rules. Commented May 25, 2018 at 13:06

3 Answers 3

4

From Flask documentation:

You can add variable sections to a URL by marking sections with <variable_name>. Your function then receives the <variable_name> as a keyword argument. Optionally, you can use a converter to specify the type of the argument like <converter:variable_name>.

So in your case that would be:

@app.route("/<int:a>")
def hello(a):
    return a + 2

Other option would be to use request data.

Sign up to request clarification or add additional context in comments.

Comments

0

You need to include the parameter "a" in the decorator @app.route:

@app.route('/<int:a>')
def hello(a):
   return a + 2

Comments

0

You can also use it like that, pass name as parameter !

@app.route('/helloworld/<Name>')
def helloworld(Name):
    print Name

another implementation would be like that, go through the python-flask documentation !

@app.route("/<int:a>")
def hello(a):
    return a + 2

1 Comment

thank you very much. yes, I should see the documentation. I did the second solution you said but I receive The requested URL was not found on the server.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.