{"id":21672407,"url":"https://github.com/redis-developer/gitpod-flask-redis","last_synced_at":"2025-04-12T03:53:01.000Z","repository":{"id":43180188,"uuid":"469879309","full_name":"redis-developer/gitpod-flask-redis","owner":"redis-developer","description":"A quick start to get up and running with Flask and Redis application development using GitPod","archived":false,"fork":false,"pushed_at":"2022-08-17T13:28:50.000Z","size":50,"stargazers_count":2,"open_issues_count":0,"forks_count":5,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-12T03:52:53.947Z","etag":null,"topics":["flask","flask-application","gitpod","gitpod-workspace","redis"],"latest_commit_sha":null,"homepage":"https://developer.redis.com","language":"HTML","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/redis-developer.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-03-14T19:37:33.000Z","updated_at":"2023-04-22T22:12:28.000Z","dependencies_parsed_at":"2022-09-11T10:51:30.787Z","dependency_job_id":null,"html_url":"https://github.com/redis-developer/gitpod-flask-redis","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":"gitpod-samples/template-python-flask","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/redis-developer%2Fgitpod-flask-redis","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/redis-developer%2Fgitpod-flask-redis/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/redis-developer%2Fgitpod-flask-redis/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/redis-developer%2Fgitpod-flask-redis/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/redis-developer","download_url":"https://codeload.github.com/redis-developer/gitpod-flask-redis/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248514209,"owners_count":21116899,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["flask","flask-application","gitpod","gitpod-workspace","redis"],"created_at":"2024-11-25T13:29:13.654Z","updated_at":"2025-04-12T03:53:00.974Z","avatar_url":"https://github.com/redis-developer.png","language":"HTML","readme":"# A Python Flask / Redis template on Gitpod\n\nThis is a [Python](https://www.python.org/) [Flask](https://flask.palletsprojects.com/) template configured for ephemeral development environments on [Gitpod](https://www.gitpod.io/) with [Redis Stack](https://redis.io/docs/stack/) using the [redis-py](https://pypi.org/project/redis/) client and [RedisInsight](https://github.com/RedisInsight/RedisInsight) visualization tool.  You can also run this application locally if you prefer.\n\nThe application is a basic counter that stores a value in Redis in the `mycounter` key.  It's intended as a start point for building your own Flask applications that use Redis.\n\nIf you prefer to work with Node.js, we've also built this same application using Node.js and the Express framework.  [Check it out here](https://github.com/redis-developer/gitpod-flask-redis).\n\n## Quick Start\n\nThere are two ways of running this application - entirely in the cloud with Gitpod, or locally on your own machine either with your own install of Redis Stack or using Docker.\n\n### Using Gitpod\n\nWhen using Gitpod, the only things you need are:\n\n* A modern browser (we have tested with [Google Chrome](https://www.google.com/chrome/)).\n* A [GitHub](https://github.com) account.\n\nClick the button below to start a new cloud development environment using Gitpod:\n\n[![Open in Gitpod](https://gitpod.io/button/open-in-gitpod.svg)](https://gitpod.io/#https://github.com/redis-developer/gitpod-flask-redis)\n\nIf you're using Gitpod for the first time, you'll need to authorize it to work with your GitHub account.\n\nGitpod will then open a workspace in the browser for you.  This contains:\n\n* VS Code for editing the code.\n* An embedded browser window with the application running in it.\n* Two terminal sessions: the left hand terminal can be used for entering commands, the right hand one starts the application for you.  Flask monitors for changes in the application code and will restarts the application for you whenever you save code changes in VS Code.\n\nTake a note of your workspace URL which looks something like this:\n\n```\nhttps://yourgithubusername-gitpodexpr-eamwc2bab7h.ws-eu38.gitpod.io/\n```\n\nIf you want to open the application in a separate browser tab outside of the Gitpod workspace tab, append `5000-` to the workspace URL, for example:\n\n```\nhttps://5000-yourgithubusername-gitpodexpr-eamwc2bab7h.ws-eu38.gitpod.io/\n```\n\nTo start RedisInsight, append `8001-` to the workspace URL and open that in another new tab.  For example:\n\n```\nhttps://8001-yourgithubusername-gitpodexpr-eamwc2bab7h.ws-eu38.gitpod.io/\n```\n\nYou'll need to agree to the terms and conditions the first time that you start RedisInsight.\n\nThe application should show that the current value of the counter is 0, and RedisInsight should show you an empty database with no keys yet.\n\n### Running Locally\n\nTo run the application locally you'll need to install the following:\n\n* The [git command line tools](https://git-scm.com/downloads).\n* A recent version of [Python 3](https://www.python.org/downloads/) (the application has been tested using Python 3.9.5).\n* [Docker Desktop](https://www.docker.com/products/docker-desktop/) (to run a Redis Stack container) or a local install of Redis Stack ([installation options here](https://redis.io/docs/stack/get-started/install/)).\n* Your favorite IDE if you want to edit / read the code (we like [VS Code](https://code.visualstudio.com/)).\n\nFirst, clone the repo, create a Python virtual environment and install dependencies:\n\n```bash\n$ git clone https://github.com/redis-developer/gitpod-flask-redis.git\n$ cd gitpod-flask-redis\n$ python3 -m venv venv\n$ . ./venv/bin/activate\n$ pip install -r requirements.txt\n```\n\nAlso, set Flask to development mode so that it will restart the server automatically for you when you make code changes:\n\n```bash\n$ export FLASK_ENV=development\n```\n\nIf you're using Docker to run Redis Stack, start the container:\n\n```bash\n$ docker-compose up -d\n```\n\nThis starts a Redis Stack container with Redis listening on port 6379 and RedisInsight listening on port 8001.\n\nIf you're not using Docker, install a copy of Redis Stack using your platform's package manager ([see options here](https://redis.io/docs/stack/get-started/install/)).  Make sure it's up and running with Redis on port 6379 and RedisInsight on port 8001 (these are the default port values).\n\nOnce Redis Stack is up and running, you can go ahead and start the application like this:\n\n```bash\n$ flask run\n```\n\nNow, open tabs in your browser for each of the following URLs:\n\n* Application: `http://127.0.0.1:5000`\n* RedisInsight: `http://127.0.0.1:8001` (agree to the terms and conditions the first time you visit this URL)\n\nThe application should show that the current value of the counter is 0, and RedisInsight should show you an empty database with no keys yet.\n\nDon't stop the application now, but when you're ready to, press Ctrl-C in the terminal window that Flask is running in.\n\nIf you're using Docker, you can stop the Redis Stack container like this when you're finished with it:\n\n```bash\n$ cd gitpod-express-redis\n$ docker-compose down\n```\n\n## How the Application Works\n\nBefore diving into code, let's first try out the application and see what data is stored in Redis.\n\n* Initially, there's nothing in Redis and the application shows the counter's value to be 0.\n* Pressing the \"Increment\" button adds one to the counter's current value, which is stored in a key named \"mycounter\" in Redis.\n* Press \"Increment\" a few times, then refresh your RedisInsight tab's view of the database to see that a key named \"mycounter\" has been added, and that it's value matches that shown in the application front end.\n* Press the \"Reset\" button, then refresh your RedisInsight tab's view of the database.  Note that the \"mycounter\" key has now been deleted.\n\n### Front End\n\nThe application's front end isn't our focus here.  It's a simple web application built with Bulma and vanilla JavaScript.  The JavaScript that handles button presses is contained in `static/app.js` and the HTML can be found in `templates/homepage.html` - it's a simple [Flask template](https://flask.palletsprojects.com/en/2.1.x/tutorial/templates/).  There are no CSS files in this repo, the CSS and Font Awesome JS files that Bulma uses are served from a CDN.\n\n### Back End\n\nNow let's look at the code in `app.py` to see how to use Node Redis to connect to Redis Stack.\n\n#### Initializing Flask and Redis\n\nThis is a regular Flask application - so we initialize Flask and annotate the Python functions for each route using `@app.route`.  We also create a Redis client using redis-py and connect it to Redis Stack running on port 6379 of our local machine:\n\n```python\nfrom flask import Flask, render_template\nimport redis\n\napp = Flask(__name__)\n\n# Connect to Redis\nr = redis.Redis(\n    host='localhost', \n    port=6379, \n    db=0, \n    decode_responses=True\n)\n```\n\n#### Home Page\n\nThe home page is also the application's only page, and it's rendered like this:\n\n```python\n@app.route(\"/\")\ndef home():\n    # Get the current counter value.\n    count = r.get(COUNTER_KEY_NAME)\n    if count is None:\n        count = 0\n\n    # Render the home page with the current counter value.\n    return render_template('homepage.html', count = count)\n```\n\nHere, we use the Redis `GET` command to get the value stored at our counter's key, if any.  If the key doesn't exist yet (Redis returns `None`), we set `count` to an initial value of `0`.  Note that we don't write this to Redis as there's no need (our increment button code will deal with that).\n\nFinally, we render out the `homepage.html` template (in `templates/homepage.ejs`), passing it the value of `count` - this makes sure that when the homepage is rendered, the current value of the counter is there.\n\n#### Pressing the Increment Button\n\nWhen the Increment button is pressed in the front end, a request is sent to `/incr`, which is handled by the following code:\n\n```python\n@app.route(\"/incr\")\ndef incr():\n    # Atomically add one to the counter in Redis.\n    # If the key doesn't exist, Redis will create it with\n    # an initial value of 1.\n    count = r.incrby(COUNTER_KEY_NAME, 1)\n    return { \"count\": count }\n```\n\nThe [Redis `INCRBY`](https://redis.io/commands/incrby/) command atomically increments the numeric value stored at a given key by a specified amount.  If the key doesn't exist, Redis creates it for us (this is why we don't need to store an initial value of 0 in Redis in the home page route).  `INCRBY` returns the new value stored at the key, and that's what we send back to the front end.\n\n#### Pressing the Reset Button\n\nWhen the Reset button is pressed in the front end, a request is sent to `/reset`, which is handled by the following code:\n\n```python\n@app.route(\"/reset\")\ndef reset():\n    # Reset by just deleting the key from Redis.\n    r.delete(COUNTER_KEY_NAME)\n    return { \"count\": 0 }\n```\n\nTo reset the counter, we delete its key from Redis, then return 0 to the front end.  The front end JavaScript then updates the displayed value for the counter.\n\n## Making Changes to the Application\n\nIf you change the application code, Flask will restart the server and pick up your changes immediately.  For example, let's make the Increment button add 10 to the value of the counter rather than 1...\n\nThe redis-py `incrby` function takes two parameters:\n\n* The key name holding the value to increment.\n* A number to increment the current value by.\n\nIn `app.py`, find the line:\n\n```python\ncount = r.incrby(COUNTER_KEY_NAME, 1)\n```\n\nand change it to read:\n\n```python\ncount = r.incrby(COUNTER_KEY_NAME, 10)\n```\n\nSave your changes and try hitting the Increment button again... what happens to the value of the counter now?\n\n## What Capabilities does Redis Stack have?\n\n[Redis Stack](https://redis.io/docs/stack/) includes the following:\n\n* An open source [Redis 6.2](https://redis.io/docs/getting-started/) server instance.\n* The [RediSearch](https://redis.io/docs/stack/search/) module, adding full text search and secondary indexing capabilities.\n* The [RedisJSON](https://redis.io/docs/stack/json/) module, which enables JSON as a native data type in Redis.\n* The [Redis Graph](https://redis.io/docs/stack/graph/) module, allowing you to store data in Redis as a graph and retrieve it with Cthe Cypher query language.\n* The [RedisBloom](https://redis.io/docs/stack/bloom/) module which implements additional probabilistic data structures such as Bloom and Cuckoo filters.\n* The [RedisTimeSeries](https://redis.io/docs/stack/timeseries/) module, which adds a time series data type and aggregations to Redis.\n* [RedisInsight](https://redis.io/docs/stack/insight/), a data visualization and management tool for Redis.\n\n## Additional Resources\n\nLooking to learn more about Redis? Here's some useful resources:\n\n* Chat with us and get your questions answered on the [Redis Discord server](https://discord.gg/redis).\n* Subscribe to our [YouTube channel](https://www.youtube.com/c/Redisinc).\n* [redis.io](https://redis.io/) - Docmentation and reference materials.\n* [developer.redis.com](https://developer.redis.com) - the official Redis Developer site.\n* [Redis University](https://university.redis.com) - free online Redis courses.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fredis-developer%2Fgitpod-flask-redis","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fredis-developer%2Fgitpod-flask-redis","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fredis-developer%2Fgitpod-flask-redis/lists"}