Categories
Python Answers

How to change the host and port that the Python flask command uses?

Sometimes, we want to change the host and port that the Python flask command uses.

In this article, we’ll look at how to change the host and port that the Python flask command uses.

How to change the host and port that the Python flask command uses?

To change the host and port that the Python flask command uses, we can use the -h flag to change the host and the -p flag to change the port.

For instance, we write

flask run -h localhost -p 3000

to run our flask app with the host set to localhost and the port set to 3000.

Conclusion

To change the host and port that the Python flask command uses, we can use the -h flag to change the host and the -p flag to change the port.

Categories
Python Answers

How to redirect while passing arguments with Python Flask?

Sometimes, we want to redirect while passing arguments with Python Flask.

In this article, we’ll look at how to redirect while passing arguments with Python Flask.

How to redirect while passing arguments with Python Flask?

To redirect while passing arguments with Python Flask, we can store values in a session.

For instance, we write

from flask import session, url_for

def do_baz():
    messages = json.dumps({"main":"Condition failed on page baz"})
    session['messages'] = messages
    return redirect(url_for('.do_foo', messages=messages))

@app.route('/foo')
def do_foo():
    messages = request.args['messages'] 
    messages = session['messages']      
    return render_template("foo.html", messages=json.loads(messages))

to set session['messages'] to messages in the do_baz view with

session['messages'] = messages

and then do the redirect by calling redirect.

Then in the do_foo view, we get the value of

messages = session['messages']

Conclusion

To redirect while passing arguments with Python Flask, we can store values in a session.

Categories
Python Answers

How to reload Python Flask app when template file changes?

Sometimes, we want to reload Python Flask app when template file changes.

In this article, we’ll look at how to reload Python Flask app when template file changes.

How to reload Python Flask app when template file changes?

To reload Python Flask app when template file changes, we set the TEMPLATES_AUTO_RELOAD to True.

For instance, we write

from flask import Flask, render_template, request, url_for, redirect

app = Flask(__name__)
app.config["TEMPLATES_AUTO_RELOAD"] = True

to config our Flask app to auto reload the app when we changed the template code.

Conclusion

To reload Python Flask app when template file changes, we set the TEMPLATES_AUTO_RELOAD to True.

Categories
Python Answers

How to fix Cross Origin Resource Sharing issues with Python Flask?

Sometimes, we want to fix Cross Origin Resource Sharing issues with Python Flask

In this article, we’ll look at how to fix Cross Origin Resource Sharing issues with Python Flask

How to fix Cross Origin Resource Sharing issues with Python Flask?

To fix Cross Origin Resource Sharing issues with Python Flask, we can use the CORS class.

For instance, w write

from flask import Flask
from flask_cors import CORS, cross_origin
app = Flask(__name__)
cors = CORS(app)
app.config['CORS_HEADERS'] = 'Content-Type'

@app.route("/")
@cross_origin()
def helloWorld():
  return "Hello"

to create an app with CORS enabled.

We enable CORS on the app with cors = CORS(app).

And then we enable CORS our view function with the @cross_origin() decorator.

Conclusion

To fix Cross Origin Resource Sharing issues with Python Flask, we can use the CORS class.

Categories
Python Answers

How to get list of all routes defined in the Python Flask app?

Sometimes, we want to get list of all routes defined in the Python Flask app.

In this article, we’ll look at how to get list of all routes defined in the Python Flask app.

How to get list of all routes defined in the Python Flask app?

To get list of all routes defined in the Python Flask app, we can use the app.url_map property.

For instance, we write

from app import app

# ...

print(app.url_map)

to print the list of routes in our app with print(app.url_map).

Conclusion

To get list of all routes defined in the Python Flask app, we can use the app.url_map property.