Categories
Python Answers

How to disable caching in Python Flask?

Sometimes, we want to disable caching in Python Flask.

In this article, we’ll look at how to disable caching in Python Flask.

How to disable caching in Python Flask?

To disable caching in Python Flask, we can set the response headers to disable cache.

For instance, we write

@app.after_request
def add_header(r):
    r.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
    r.headers["Pragma"] = "no-cache"
    r.headers["Expires"] = "0"
    r.headers['Cache-Control'] = 'public, max-age=0'
    return r

to create the add_header function that adds a few headers to the response after each request is done.

We make it run after each request with the @app.after_request decorator.

And then we add the Expires and Cache-Control headers and set their values all to 0 to disable caching.

Conclusion

To disable caching in Python Flask, we can set the response headers to disable cache.

Categories
Python Answers

How to disable console messages in a Python Flask server?

Sometimes, we want to disable console messages in a Python Flask server.

In this article, we’ll look at how to disable console messages in a Python Flask server.

How to disable console messages in a Python Flask server?

To disable console messages in a Python Flask server, we can call setLevel on the logger object.

For instance, we write

from flask import Flask
app = Flask(__name__)

import logging
log = logging.getLogger('werkzeug')
log.setLevel(logging.ERROR)

@app.route("/")
def hello():
    return "Hello World!"

if __name__ == "__main__":
    app.run()

to get the logger with

log = logging.getLogger('werkzeug')

Then we set the logger to only log errors with

log.setLevel(logging.ERROR)

Conclusion

To disable console messages in a Python Flask server, we can call setLevel on the logger object.

Categories
Python Answers

How to make sessions timeout in Python Flask?

Sometimes, we want to make sessions timeout in Python Flask.

In this article, we’ll look at how to make sessions timeout in Python Flask.

How to make sessions timeout in Python Flask?

To make sessions timeout in Python Flask, we set the session.permanent and app.permanent_session_lifetime properties.

For instance, we write

from datetime import timedelta
from flask import session, app

@app.before_request
def make_session_permanent():
    session.permanent = True
    app.permanent_session_lifetime = timedelta(minutes=5)

We set make a session permanent with

session.permanent = True

And then we set to session lifetime to 5 minutes with

app.permanent_session_lifetime = timedelta(minutes=5)

We use the @app.before_request decorator to run make_session_permanent before every request.

Conclusion

To make sessions timeout in Python Flask, we set the session.permanent and app.permanent_session_lifetime properties.

Categories
Python Answers

How to select iframe using Python Selenium?

Sometimes, we want to select iframe using Python Selenium.

In this article, we’ll look at how to select iframe using Python Selenium.

How to select iframe using Python Selenium?

To select iframe using Python Selenium, we can call switch_to.frame.

For instance, we write

self.driver = webdriver.Firefox()

time.sleep(3)
driver.switch_to.frame(driver.find_element_by_tag_name("iframe"))

elem = driver.find_element_by_xpath("/html/body/p")
elem.send_keys("Lorem Ipsum")
driver.switch_to.default_content()

to write

driver.switch_to.frame(driver.find_element_by_tag_name("iframe"))

to switch to the first iframe found by find_element_by_tag_name.

Then we do some manipulation in the iframe with

elem = driver.find_element_by_xpath("/html/body/p")
elem.send_keys("Lorem Ipsum")

then we switch back to the original page with

driver.switch_to.default_content()

Conclusion

To select iframe using Python Selenium, we can call switch_to.frame.

Categories
Python Answers

How to make Python Selenium interact with an existing browser session?

Sometimes, we want to make Python Selenium interact with an existing browser session.

In this article, we’ll look at how to make Python Selenium interact with an existing browser session.

How to make Python Selenium interact with an existing browser session?

To make Python Selenium interact with an existing browser session, we open the driver and the connect to the session with the given session ID.

For instance, we write

driver = webdriver.Firefox()

url = driver.command_executor._url 
session_id = driver.session_id  

driver = webdriver.Remote(command_executor=url,desired_capabilities={})
driver.close()   
driver.session_id = session_id

driver.get("http://www.example.com")

to open the driver with

driver = webdriver.Firefox()

We get the ID of the existing session with

url = driver.command_executor._url 
session_id = driver.session_id  

Then we close the driver and connect to it with the session with

driver = webdriver.Remote(command_executor=url,desired_capabilities={})
driver.close()   
driver.session_id = session_id

Conclusion

To make Python Selenium interact with an existing browser session, we open the driver and the connect to the session with the given session ID.