Python Intermediate - Flask
An Understanding of Python Frameworks
Understanding Flask
Flask, a micro-framework for Python, is renowned for its simplicity and flexibility. It empowers developers to create web applications with ease, making it an excellent choice for projects of varying scales.
In this blog post, we’ll understand Flask, explore its file structure, and build our first Flask app, complete with HTML, CSS, Bootstrap, and a touch of JavaScript for user experience. The dynamic landing page showcases “Mr.Cloudexplorer” and “Blog Shower App,” redirecting users to a randomly chosen blog post upon clicking the logo, sourced from a predefined list of URLs.
Flask File Structure
Before coding, let’s take a moment to understand Flask’s file structure.
A typical Flask project consists of folders like templates
for HTML templates, static
for static files (CSS, JS, images), and the main application file, usually named app.py
.
Flask's lightweight nature means you have the flexibility to structure your project according to your preferences.
Building Our First Flask App
Let’s start by creating a simple Flask app. First, ensure you have Flask installed
pip install Flask
Now, let’s structure our project
Run in localhost
Now, let’s test our project locally
follow link http://127.0.0.1:5000
everything works well as expected.
GitHub code
Deployment with Render
Now that our Flask app is ready, let’s deploy it using Render, a cloud application hosting platform for developers. Follow these steps:
- Sign up on Render.
2. Create a new web service.
3. Choose GitHub as your repository and connect it.
4. Enter the following details
- Give unique name
- Environment: Python
- Build Command:
pip install -r requirements.txt
- Start Command:
gunicorn app:app
- Choose free instance
- Click “Create Web Service”.
- Wait till for the build, deployment, and running process yet to complete
Auto Save and Deploy.
Render will automatically detect your Flask app, install dependencies, and deploy it. Once deployed, you’ll receive a live URL where you can view your Flask app in action.
Congratulations! You’ve successfully built and deployed your first Flask app.
In the next blog post, we’ll explore another framework and create a simple app, continuing our journey into the exciting world of Python development. Stay tuned!
Happy coding!
Reach me 👨💻