Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the "endpoint" in flask's .add_url_rule()?

Tags:

python

flask

Consider the following code

import flask

class API:
    def hello(self):
        return flask.Response('hello', 200)

api = API()
app = flask.Flask(__name__)
app.add_url_rule('/', 'hello', api.hello)
app.run()

It returns "hello" upon a GET call to /.

The documentation for add_url_rule states that

[add_url_rule] works exactly like the route() decorator.

It requires however at least three parameters. The first and third one are understandable and mimic @route(). What is the second one (hello in my case)?

The documentation further states that this is

endpoint – the endpoint for the registered URL rule. Flask itself assumes the name of the view function as endpoint

What does this mean? Why isn't the URL (/) and the method to call (api.hello) sufficient? What is the role of the "endpoint"? How is it exactly used?

like image 506
WoJ Avatar asked Aug 10 '17 07:08

WoJ


People also ask

What is endpoint in Add_url_rule?

The endpoint name is the registration key for views, a symbolic name by which you can reference the route from other parts of your application. @route() takes the same parameter; the default is the name of the decorated function.

What is an endpoint in flask?

Basically, the "endpoint" is an identifier that is used in determining what logical unit of your code should handle the request. Normally, an endpoint is just the name of a view function. However, you can actually change the endpoint, as is done in the following example.

What is endpoint in Python?

Endpoints Frameworks for Python is integrated with the App Engine standard Python 2.7 runtime environment. Endpoints Frameworks consists of tools, libraries, and capabilities that let you generate APIs and client libraries from an App Engine application.

How do you make a post endpoint in a flask?

So first make sure you have pipenv running, and then say python app.py . I'm not getting any errors so that's a good sign. And then this is going to be a POST request. Our goal is to create a new guide and then we need to pass the data in.


1 Answers

It's the name for the route; the one you'd use in the url_for() function for example. The endpoint name is the registration key for views, a symbolic name by which you can reference the route from other parts of your application.

@route() takes the same parameter; the default is the name of the decorated function. This is documented both in the add_url_rule() documentation as well as the documentation for @route():

  • endpoint – the endpoint for the registered URL rule. Flask itself assumes the name of the view function as endpoint.

(bold italic emphasis mine).

Note that the example in the documentation tried to show the same:

Basically this example:

@app.route('/')
def index():
    pass

Is equivalent to the following:

def index():
    pass
app.add_url_rule('/', 'index', index)

Note that the second argument 'index' matches the function name.

like image 53
Martijn Pieters Avatar answered Oct 18 '22 21:10

Martijn Pieters