
Intro
Hello everyone! I’m excited to present the first part of a blog series that delves into my latest project – a fun and engaging Hangman game. This project is a culmination of a diverse array of technologies: Python, Flask, MySQL, Docker, AWS, IaC, and CI/CD. By sharing the journey, my aim is to illustrate the synergistic power of these tools when used in tandem. In this inaugural part, we will focus on the very foundations of our application: the Python Flask backend, connecting to a MySQL database, and the pivotal role Docker plays in ensuring the smooth running of our application across varying environments.
Python and Flask: Laying the Groundwork for Our Backend

Our journey begins with Python – a versatile language celebrated for its readability, simplicity, and the breadth of its libraries. Python’s adaptability makes it an excellent choice for a variety of tasks, from data analysis to web development, making it a fitting choice for our Hangman game.
For the web framework, we’ve opted for Flask. Known for its minimalistic and “micro” nature, Flask provides us the flexibility to craft applications without having to navigate around unnecessary extras. It is unopinionated about databases and doesn’t come bundled with form validation or other components. However, Flask’s extension support is vast, and we can add any functionality we need as if Flask itself had provided it.
The backbone of our application is the Flask app, instantiated with the line app = Flask(__name__). This command creates a new Flask web server. Our server responds to various routes, which are the different URLs or pages that the user interacts with. For instance, the route for our application’s homepage is defined with @app.route(‘/’).
In any web application, user authentication is crucial. For this purpose, we’ve defined a blueprint named auth, which is registered with our main application. Blueprints are a feature in Flask that facilitates the modularization of applications, enabling us to split the application into reusable, logical components. This modularization is a lifesaver when applications become complex.
Connecting to MySQL and Managing Data: Storing and Retrieving Information

Next in line is MySQL, a widely favored choice for relational databases. To create a bridge between our Flask application and the MySQL database, we’ve employed the flask-mysqldb module. This nifty extension allows us to interact with MySQL directly from our Python code, making it simpler to manage our data.
Data in our game is stored in two tables: users and stats. The users table is dedicated to storing user data, including usernames and passwords. The stats table, on the other hand, keeps track of the game statistics for each user. These statistics include the number of games won, the number of games lost, the win-loss ratio, the current win streak, and the longest win streak.
Sweating the Details: Implementing the Game Logic
The heart of our Hangman game lies in the Flask routes and an array of helper functions that form the game’s logic. We’ve established different routes for the various stages of the game, such as the home screen, the category selection screen, and the actual gameplay screen.
The crux of the game begins when a new game is initiated. Based on the category selected by the user, a random word is chosen. The game’s status – which comprises the hidden word, incorrect guesses, and the number of remaining lives – is stored in the user’s session data. This session data allows us to maintain the state of the game for each user.
Containerizing with Docker: Ensuring Consistent Behavior Across Environments

To ensure our application consistently behaves the same, regardless of the environment in which it’s run, we’ve chosen to containerize it using Docker. Docker packs our application, along with all its dependencies, into a neat Docker image. This image can be run anywhere Docker is installed, ensuring that the application behaves consistently across a variety of environments.
While the Dockerfile for our application is yet to be created, here’s a glimpse of what it would look like:

This Dockerfile begins with a base Python image and sets the working directory. It then adds our application files to the Docker container and installs the Python dependencies specified in our requirements.txt file. It also exposes the correct port for our application to run. Finally, it specifies that our app.py should be run when the Docker container is launched.
Conclusion
This concludes the inaugural part of our Hangman game project series. We’ve explored the intricacies of developing a Python Flask backend, connecting it to a MySQL database, and implementing the game’s logic. We’ve also delved into the vital role Docker plays in ensuring the consistent behavior of our application across varying environments.
In the second part of this series, we will turn our attention to deploying our Dockerized application on AWS using a variety of services such as ECR, ECS, VPC, RDS, ALB, and Autoscaling. So stay tuned for a deep dive into the world of AWS deployment!
Leave a Reply to Hangman Part II: Deploying to AWS (ECR, VPC, RDS, ECS, ALB, and Autoscaling) – Brett's Cloud & DevOps Blog Cancel reply