Categories
Python Answers

How to fix secret key not set in a Python Flask session, using the Flask-Session extension?

Sometimes, we want to fix secret key not set in a Python Flask session, using the Flask-Session extension.

In this article, we’ll look at how to fix secret key not set in a Python Flask session, using the Flask-Session extension.

How to fix secret key not set in a Python Flask session, using the Flask-Session extension?

To fix secret key not set in a Python Flask session, using the Flask-Session extension, we set the app.secret_key property to the secret key.

For instance, we write

if __name__ == "__main__":
    app.secret_key = 'super secret key'
    app.config['SESSION_TYPE'] = 'filesystem'

    sess.init_app(app)

    app.debug = True
    app.run()

to set app.secret_key to the secret key when we start our app.

Then the secret key should be picked up by Flask-Session.

Conclusion

To fix secret key not set in a Python Flask session, using the Flask-Session extension, we set the app.secret_key property to the secret key.

Categories
Python Answers

How to fix ImportError: No module named MySQLdb with Python Flask?

Sometimes, we want to fix ImportError: No module named MySQLdb with Python Flask.

In this article, we’ll look at how to fix ImportError: No module named MySQLdb with Python Flask.

How to fix ImportError: No module named MySQLdb with Python Flask?

To fix ImportError: No module named MySQLdb with Python Flask, we install the mysql-python package.

To install it, we run

pip install mysql-python

Then we use

engine = create_engine('mysql+mysqldb://user:password@localhost/foo')

to connect to the database with create_engine.

Conclusion

To fix ImportError: No module named MySQLdb with Python Flask, we install the mysql-python package.

Categories
Python Answers

How to send JSON and status code with a Python Flask response?

Sometimes, we want to send JSON and status code with a Python Flask response.

In this article, we’ll look at how to send JSON and status code with a Python Flask response.

How to send JSON and status code with a Python Flask response?

To send JSON and status code with a Python Flask response, we can return a tuple with the response body and the status code in our view.

For instance, we write

from flask import jsonify

@app.route('/login', methods=['POST'])
def login():
    data = {'name': 'john smith'}
    return jsonify(data), 200

to add the login view with the response returned.

We return the body with jsonify(data) and we return the status code by putting 200 in the same tuple.

Conclusion

To send JSON and status code with a Python Flask response, we can return a tuple with the response body and the status code in our view.

Categories
Python Answers

How to pass variables from Python Flask to JavaScript?

Sometimes, we want to pass variables from Python Flask to JavaScript.

In this article, we’ll look at how to pass variables from Python Flask to JavaScript.

How to pass variables from Python Flask to JavaScript?

To pass variables from Python Flask to JavaScript, we can call render_template with the data argument with the dictionary of data we want to pass to the template.

For instance, in our view, we write

@app.route('/')
def hello():
    data = {'username': 'jane', 'site': 'example.com'}
    return render_template('settings.html', data=data)

to call render_template with the template file name and the data set to the data dict with the data we want to pass to the template.

Then in the settings.html template, we write

<html>
    <head>
         <script type="text/javascript" {{ url_for('static', filename='app.js')}}></script>
         <script type="text/javascript">
            myVar = myFunc({{ data | tojson }})
         </script>
    </head>
</html>

to add a script tag that interpolates the data variable from the render_template argument.

We use the tojson filter to convert it to a JavaScript object.

Conclusion

To pass variables from Python Flask to JavaScript, we can call render_template with the data argument with the dictionary of data we want to pass to the template.

Categories
Python Answers

How to send image generated by PIL to browser with Python Flask?

Sometimes, we want to send image generated by PIL to browser with Python Flask.

In this article, we’ll look at how to send image generated by PIL to browser with Python Flask.

How to send image generated by PIL to browser with Python Flask?

To send image generated by PIL to browser with Python Flask, we can use the StringIO class.

For instance, we write

def serve_pil_image(pil_img):
    img_io = StringIO()
    pil_img.save(img_io, 'JPEG', quality=70)
    img_io.seek(0)
    return send_file(img_io, mimetype='image/jpeg')

@app.route('some/route/')
def serve_img():
    img = Image.new('RGB', ...)
    return serve_pil_image(img)

to create the serve_pil_image that calls send_file with the img_io StringIO object.

We populate it with the image data by writing

pil_img.save(img_io, 'JPEG', quality=70)

Then we use img_io.seek(0) to back to the beginning of the image.

And then we call send_file with img_io and the mimetype to return the image.

Then we call serve_pil_image with the PIL image img to return that as the response in our view.

Conclusion

To send image generated by PIL to browser with Python Flask, we can use the StringIO class.