Professional Documents
Culture Documents
Flask
Flask
Flask
Flask Tip
flask
Posted on Twitter on Aug. 29, 2022.
Flash messages are used to provide useful information to the user based on their actions
with the app. The flash() method is used to create a flash message to be displayed in the
next request.
return render_template('stocks/add_stock.html')
flask
Posted on Twitter on Aug. 28, 2022.
Flask Redirect
Flask Tip:
return render_template('add_stock.html')
flask
Posted on Twitter on Aug. 26, 2022.
flask
Posted on Twitter on Aug. 25, 2022.
Flask Tip:
<header class="site-header">
<a href="{{ url_for('stocks.index') }}">Flask App</a>
<nav>
<ul>
<li><a href="{{ url_for('users.register') }}">Register</a></li>
<li><a href="{{ url_for('users.login') }}">Login</a></li>
</ul>
</nav>
</header>
flask
Posted on Twitter on Aug. 24, 2022.
Flask Tip:
After configuring the interface to Redis, the session object can be used (but data is stored
on the server!).
import redis
from flask import Flask, session, render_template_string
from flask_session import Session
# Create and initialize the Flask-Session object AFTER `app` has been configured
server_session = Session(app)
@app.route('/get_email')
def get_email():
return render_template_string("""<h1>Welcome {{ session['email'] }}!</h1>""")
For more, review Server-side Sessions in Flask with Redis.
flask
Posted on Twitter on Aug. 23, 2022.
However, if you want to change the life of the session object, define
the PERMANENT_SESSION_LIFETIME configuration variable after creating the Flask
app:
import datetime
app.config['PERMANENT_SESSION_LIFETIME'] = timedelta(minutes=1)
When setting the data in the session, specify that the sessions should be permanent (time
will be based on PERMANENT_SESSION_LIFETIME):
flask
Posted on Twitter on Aug. 20, 2022.
@app.route('/delete_email')
def delete_email():
# Clear the email stored in the session object
session.pop('email', default=None)
return '<h1>Session deleted!</h1>'
For more, review Sessions in Flask.
flask
Posted on Twitter on June 29, 2022.
In Flask, the session object can be read (in the same manner as a dictionary) to retrieve
data unique to the session. It's conveniently available in Jinja templates as well.
@app.route('/get_email')
def get_email():
return render_template_string("""
{% if session['email'] %}
<h1>Welcome {{ session['email'] }}!</h1>
{% else %}
<h1>Welcome! Please enter your email <a
href="{{ url_for('set_email') }}">here.</a></h1>
{% endif %}
""")