Using the Python SDK with Flask

To use Weasl on your Python server, you can use the HTTP API that is available to users, or you can install the Weasl SDK Python package.

The current functionality is limited to only helping setup for Flask servers, but if you request another server, our developers will get right to work on it!

Here's how to use it:

from flask import Flask, jsonify, session
from weasl.integrations.flask import login_required
from weasl.exceptions import LoginRequired

app = Flask(__name__)
app.config['WEASL_CLIENT_ID'] = 'YOUR CLIENT ID'


@app.errorhandler(LoginRequired)
def not_logged_in():
    return "you are not logged in"


@app.route('/me')
@login_required
def user():
    return jsonify(session.current_user)

There are a few things happening here. This code:

  • adds the Weasl client ID to the app config
  • catches any "LoginRequired" error with a Flask app error handler
  • decorates a view with "login_required" to show that we first need to authenticate the route before hitting the application code.
  • gets the current_user off of the flask.session, which is automatically added when using the login_required decorator.

In the case that the user is not logged in, or has an expired token, the not_logged_in function will be executed.


Did this help answer your question?

thumbs up
thumbs down

Thanks for the feedback! 🙏🏽


Help by drift