What Is an 'Endpoint' in Flask

What is an 'endpoint' in Flask?

How Flask Routing Works

The entire idea of Flask (and the underlying Werkzeug library) is to map URL paths to some logic that you will run (typically, the "view function"). Your basic view is defined like this:

@app.route('/greeting/<name>')
def give_greeting(name):
return 'Hello, {0}!'.format(name)

Note that the function you referred to (add_url_rule) achieves the same goal, just without using the decorator notation. Therefore, the following is the same:

# No "route" decorator here. We will add routing using a different method below.
def give_greeting(name):
return 'Hello, {0}!'.format(name)

app.add_url_rule('/greeting/<name>', 'give_greeting', give_greeting)

Let's say your website is located at 'www.example.org' and uses the above view. The user enters the following URL into their browser:

http://www.example.org/greeting/Mark

The job of Flask is to take this URL, figure out what the user wants to do, and pass it on to one of your many python functions for handling. It takes the path:

/greeting/Mark

...and matches it to the list of routes. In our case, we defined this path to go to the give_greeting function.

However, while this is the typical way that you might go about creating a view, it actually abstracts some extra info from you. Behind the scenes, Flask did not make the leap directly from URL to the view function that should handle this request. It does not simply say...

URL (http://www.example.org/greeting/Mark) should be handled by View Function (the function "give_greeting")

Actually, it there is another step, where it maps the URL to an endpoint:

URL (http://www.example.org/greeting/Mark) should be handled by Endpoint "give_greeting".
Requests to Endpoint "give_greeting" should be handled by View Function "give_greeting"

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.

@app.route('/greeting/<name>', endpoint='say_hello')
def give_greeting(name):
return 'Hello, {0}!'.format(name)

Now, when Flask routes the request, the logic looks like this:

URL (http://www.example.org/greeting/Mark) should be handled by Endpoint "say_hello".
Endpoint "say_hello" should be handled by View Function "give_greeting"

How You Use the Endpoint

The endpoint is commonly used for the "reverse lookup". For example, in one view of your Flask application, you want to reference another view (perhaps when you are linking from one area of the site to another). Rather than hard-code the URL, you can use url_for(). Assume the following

@app.route('/')
def index():
print url_for('give_greeting', name='Mark') # This will print '/greeting/Mark'

@app.route('/greeting/<name>')
def give_greeting(name):
return 'Hello, {0}!'.format(name)

This is advantageous, as now we can change the URLs of our application without needing to change the line where we reference that resource.

Why not just always use the name of the view function?

One question that might come up is the following: "Why do we need this extra layer?" Why map a path to an endpoint, then an endpoint to a view function? Why not just skip that middle step?

The reason is because it is more powerful this way. For example, Flask Blueprints allow you to split your application into various parts. I might have all of my admin-side resources in a blueprint called "admin", and all of my user-level resources in an endpoint called "user".

Blueprints allow you to separate these into namespaces. For example...

main.py:

from flask import Flask, Blueprint
from admin import admin
from user import user

app = Flask(__name__)
app.register_blueprint(admin, url_prefix='admin')
app.register_blueprint(user, url_prefix='user')

admin.py:

admin = Blueprint('admin', __name__)

@admin.route('/greeting')
def greeting():
return 'Hello, administrative user!'

user.py:

user = Blueprint('user', __name__)
@user.route('/greeting')
def greeting():
return 'Hello, lowly normal user!'

Note that in both blueprints, the '/greeting' route is a function called "greeting". If I wanted to refer to the admin "greeting" function, I couldn't just say "greeting" because there is also a user "greeting" function. Endpoints allow for a sort of namespacing by having you specify the name of the blueprint as part of the endpoint. So, I could do the following...

print url_for('admin.greeting') # Prints '/admin/greeting'
print url_for('user.greeting') # Prints '/user/greeting'

what does endpoint mean in flask-restful

The thing is that the add_resource function registers the routes with the framework using the given endpoint. If an endpoint isn't given then Flask-RESTful generates one for you from the class name.

Your case is WorkerAPI, the endpoint will beworkerapi for these two methods, better make endpoint explicit and avoid to have conflicting endpoint names registered.

For what's endpoint, you can refer to this answer for more details.

Create Flask endpoint using a Loop

You can use

app.add_url_rule(rule, endpoint=None, view_func=None, provide_automatic_options=None, **options)

to achieve this.
For example:

for endpoint in endpoint_list:
app.add_url_rule(endpoint['route'], view_func=endpoint['view_func'])

Check out the docs.

Note that the endpoint_list contains records of endpoints. It should be a list of dictionaries, for example:

endpoint_list = [
{
"route": "/",
"view_func": index
},
.....
]

Avoid using "list" as the variable name. It would override Python's default list function.

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

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.

How to properly write a Patch endpoint in flask and Pymongo

@app.route('/update-instructor/<id>', methods=['PATCH'])
def update_one_instructor(id):
request_params = request.get_json()
print(request_params)
# {'first': 'me', 'email': 'email'} only gives you the things from postman

updateObject = request_params
# updateObject = {
# "course": course,
# "email": email,
# "first": first,
# "last": last,
# "password": password,
# "role": role
# }

# Continue with your logic

return "Update Successful"

I put this in postman:

  {
"first": "me",
"email": "email"
}

Generic endpoint in Flask

If you want an endpoint to literally capture everything after a particular slash, you can use a path placeholder in your route definition.

@app.route('/<path:path>')

A more detailed example in this answer:

Capture Arbitrary Path in Flask Route



Related Topics



Leave a reply



Submit