In this quickstart tutorial, we will explore how to use Flask with Upstash Redis to build a simple web application that increments a counter each time a user accesses the homepage.
First, install Flask and the Upstash Redis client for Python.
Create a Redis database using Upstash Console or Upstash CLI and export the UPSTASH_REDIS_REST_URL
and UPSTASH_REDIS_REST_TOKEN
to your environment.
You can also use python-dotenv
to load environment variables from your .env
file.
Create app.py
:
Run the Flask app locally:
Visit http://127.0.0.1:5000/
in your browser, and you will see the counter
increment with each refresh.
Code Breakdown
Redis.from_env()
, we initialize the connection to our Redis database using the environment variables exported earlier./
) is accessed, Redis increments the counter
key. This key-value pair is automatically created in Redis if it does not exist, and its value is incremented on each request.In this quickstart tutorial, we will explore how to use Flask with Upstash Redis to build a simple web application that increments a counter each time a user accesses the homepage.
First, install Flask and the Upstash Redis client for Python.
Create a Redis database using Upstash Console or Upstash CLI and export the UPSTASH_REDIS_REST_URL
and UPSTASH_REDIS_REST_TOKEN
to your environment.
You can also use python-dotenv
to load environment variables from your .env
file.
Create app.py
:
Run the Flask app locally:
Visit http://127.0.0.1:5000/
in your browser, and you will see the counter
increment with each refresh.
Code Breakdown
Redis.from_env()
, we initialize the connection to our Redis database using the environment variables exported earlier./
) is accessed, Redis increments the counter
key. This key-value pair is automatically created in Redis if it does not exist, and its value is incremented on each request.