A sample Flask web application that demonstrates both web development and API integration in Python. This application showcases how to build web pages, handle user interactions, and make calls to external APIs.
- Flask Web Framework: Modern Python web development
- External API Integration: Makes calls to weather, jokes, and cat facts APIs
- Responsive Design: Bootstrap-based responsive UI
- Interactive Frontend: JavaScript-powered user interactions
- Error Handling: Graceful error handling with fallback responses
- RESTful API Endpoints: JSON API endpoints for data access
- Template System: Jinja2 templating for dynamic content
python-web-app/
├── app/
│ ├── __init__.py # Package initialization
│ ├── main.py # Main Flask application
│ ├── api_client.py # API client modules
│ ├── templates/ # HTML templates
│ │ ├── base.html # Base template
│ │ ├── index.html # Home page
│ │ ├── weather.html # Weather page
│ │ ├── jokes.html # Jokes page
│ │ ├── cat_facts.html # Cat facts page
│ │ ├── 404.html # 404 error page
│ │ └── 500.html # 500 error page
│ └── static/
│ └── css/
│ └── style.css # Custom styles
├── requirements.txt # Python dependencies
├── run.py # Application entry point
└── README.md # This file
- Python 3.7 or higher
- pip (Python package installer)
-
Clone or navigate to the project directory:
cd python-web-app
-
Create a virtual environment (recommended):
python3 -m venv venv
-
Activate the virtual environment:
On macOS/Linux:
source venv/bin/activate
On Windows:
venv\Scripts\activate
-
Install dependencies:
pip install -r requirements.txt
-
Start the development server:
python3 run.py
Or alternatively:
python3 -m flask --app app.main run
-
Open your browser and navigate to:
http://localhost:5000
You can set the following environment variables to customize the application:
SECRET_KEY
: Flask secret key (default: 'dev-secret-key-change-in-production')PORT
: Port to run the application on (default: 5000)DEBUG
: Enable debug mode (default: True)
For production use with real weather data, you'll need to:
- Sign up for a free API key at OpenWeatherMap
- Update the
WeatherAPI
class inapp/api_client.py
to use your real API key - Uncomment the real API call code in the
get_weather
method
The application provides the following API endpoints:
GET /api/status
- Check application statusGET /weather/api/<city>
- Get weather data for a cityGET /api/joke
- Get a random jokeGET /api/cat-fact
- Get a random cat fact
- Flask Routes: Multiple routes for different pages and API endpoints
- Template Rendering: Using Jinja2 templates for dynamic HTML generation
- Static Files: Serving CSS and JavaScript files
- Form Handling: Processing user input from web forms
- Error Pages: Custom 404 and 500 error pages
- HTTP Requests: Making GET requests to external APIs
- Error Handling: Graceful handling of API failures with fallback responses
- JSON Processing: Parsing and processing JSON responses
- Timeout Handling: Request timeout configuration
- Multiple APIs: Integration with weather, jokes, and cat facts APIs
- Responsive Design: Bootstrap-based mobile-friendly design
- Interactive UI: JavaScript-powered dynamic content updates
- Loading States: User feedback during API calls
- Error Display: User-friendly error messages
- History Tracking: Client-side history for jokes and facts
- Home Page: Visit the home page and check all navigation links
- Weather Feature: Try searching for different cities
- Jokes Feature: Click the joke button multiple times
- Cat Facts Feature: Test the cat facts functionality
- API Status: Check the API status from the home page
You can test the API endpoints directly using curl:
# Check application status
curl http://localhost:5000/api/status
# Get weather data
curl http://localhost:5000/weather/api/London
# Get a random joke
curl http://localhost:5000/api/joke
# Get a cat fact
curl http://localhost:5000/api/cat-fact
- New API Client: Add new API client classes to
api_client.py
- New Routes: Add new routes to
main.py
- New Templates: Create HTML templates in the
templates/
directory - New Styles: Add CSS to
static/css/style.css
- Use environment variables for configuration
- Implement proper error handling
- Add logging for debugging
- Use virtual environments
- Keep API keys secure
- Test thoroughly
- Import Errors: Make sure you're in the virtual environment and have installed dependencies
- Port Already in Use: Change the port using the
PORT
environment variable - API Timeouts: Check your internet connection; the app will fall back to mock data
- Template Not Found: Ensure all templates are in the
app/templates/
directory
The application uses Python's logging module. Check the console output for error messages and debugging information.
This project demonstrates several key concepts in Python web development:
Feel free to fork this project and add your own features! Some ideas for enhancements:
- Add user authentication
- Implement a database for storing favorites
- Add more API integrations
- Create a mobile app version
- Add unit tests
- Implement caching for API responses
This project is for educational purposes. Feel free to use and modify as needed.
Happy Coding! 🐍✨