Categories
Python Answers

How to escape jinja2 syntax in a jinja2 template with Python Flask?

Sometimes, we want to escape jinja2 syntax in a jinja2 template with Python Flask.

In this article, we’ll look at how to escape jinja2 syntax in a jinja2 template with Python Flask.

How to escape jinja2 syntax in a jinja2 template with Python Flask?

To escape jinja2 syntax in a jinja2 template with Python Flask, we can put render the template code without interpretation by putting the code in the {% raw %} block.

For instance, we write

{% raw %}

Anything in this block is treated as raw text,
including {{ curly braces }} and
{% block %}

{% endraw %}

to treat

Anything in this block is treated as raw text,
including {{ curly braces }} and
{% block %}

as raw text.

Conclusion

To escape jinja2 syntax in a jinja2 template with Python Flask, we can put render the template code without interpretation by putting the code in the {% raw %} block.

Categories
Python Answers

How to add multiple parameters in a Python Flask app route?

Sometimes, we want to add multiple parameters in a Python Flask app route.

In this article, we’ll look at how to add multiple parameters in a Python Flask app route.

How to add multiple parameters in a Python Flask app route?

To add multiple parameters in a Python Flask app route, we can add the URL parameter placeholders into the route string.

For instance, we write

@app.route('/createcm/<summary>/<change>')
def createcm(summary=None, change=None):
    #...

to create the createcm view function by using the app.route decorator.

We call app.route with the URL string with the summary and change placeholders.

And we get the values of the parameters from the parameters in the createcm function.

Conclusion

To add multiple parameters in a Python Flask app route, we can add the URL parameter placeholders into the route string.

Categories
Python Answers

How to fix CSS problems with a Python Flask web app?

Sometimes, we want to fix CSS problems with a Python Flask web app.

In this article, we’ll look at how to fix CSS problems with a Python Flask web app.

How to fix CSS problems with a Python Flask web app?

To fix CSS problems with a Python Flask web app, we should make sure our CSS file in the static files directory.

Then we can use url_for to get the URL to the CSS file.

For instance, we can put our CSS file in project_root/static/stylesheets.

Then we can reference the CSS file with a link tag by writing

<link type="text/css" rel="stylesheet" href="{{ url_for('static', filename='stylesheets/style.css') }}" />

We set the href attribute to the CSS file URL which we get with

{{ url_for('static', filename='stylesheets/style.css') }}

Conclusion

To fix CSS problems with a Python Flask web app, we should make sure our CSS file in the static files directory.

Then we can use url_for to get the URL to the CSS file.

Categories
Python Answers

How to use MySQL in Python Flask?

Sometimes, we want to use MySQL in Python Flask.

In this article, we’ll look at how to use MySQL in Python Flask.

How to use MySQL in Python Flask?

To use MySQL in Python Flask, we use the flask-mysql package.

To install it, we run

pip install flask-mysql

Then in our app’s entry point file, we add

from flask import Flask
from flaskext.mysql import MySQL

app = Flask(__name__)
mysql = MySQL()
app.config['MYSQL_DATABASE_USER'] = 'root'
app.config['MYSQL_DATABASE_PASSWORD'] = 'root'
app.config['MYSQL_DATABASE_DB'] = 'EmpData'
app.config['MYSQL_DATABASE_HOST'] = 'localhost'
mysql.init_app(app)

to set the MySQL creadentials in the config.

We call mysql.init_app with app to incorporate flask-mysql into our Flask app.

Then we can use

conn = mysql.connect()
cursor = conn.cursor()

cursor.execute("SELECT * from User")
data = cursor.fetchone()

to connect to our database with mysql.connect.

And we get the cursor with conn.cursor.

Then we make our query with

cursor.execute("SELECT * from User")
data = cursor.fetchone()

Conclusion

To use MySQL in Python Flask, we use the flask-mysql package.

Categories
Python Answers

How to fix Python Flask throwing ‘working outside of request context’ when starting sub thread?

Sometimes, we want to fix Python Flask throwing ‘working outside of request context’ when starting sub thread.

In this article, we’ll look at how to fix Python Flask throwing ‘working outside of request context’ when starting sub thread.

How to fix Python Flask throwing ‘working outside of request context’ when starting sub thread?

To fix Python Flask throwing ‘working outside of request context’ when starting sub thread, we can wrap our request code in with app.test_request_context().

For instance, we write

@app.route('/my_endpoint', methods=['POST'])
def my_endpoint_handler():
    def handle_sub_view(req):
        with app.test_request_context():
            from flask import request
            request = req
            # ...
    thread.start_new_thread(handle_sub_view, (request))
    return "hello"

to call app.test_request_context() to get the request context in the handle_sub_view function.

Then we import request inside the block with

from flask import request

And then we do what we want with it.

Then we use

thread.start_new_thread(handle_sub_view, (request))

to start a new thread.

Conclusion

To fix Python Flask throwing ‘working outside of request context’ when starting sub thread, we can wrap our request code in with app.test_request_context().