Categories
Python Answers

How to check if row exists in table with Python Flask-SQLAlchemy?

Sometimes, we want to check if row exists in table with Python Flask-SQLAlchemy

In this article, we’ll look at how to check if row exists in table with Python Flask-SQLAlchemy.

How to check if row exists in table with Python Flask-SQLAlchemy?

To check if row exists in table with Python Flask-SQLAlchemy, we can make a query top the table.

For instance, we write

exists = db.session.query(User.id).filter_by(name='davidism').first() is not None

to call query to query the table by User.id.

And we call filter_by to add additional filters by the name field value.

Then we call first to return the first value.

Conclusion

To check if row exists in table with Python Flask-SQLAlchemy, we can make a query top the table.

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.