PrerequisitesSetting up a virtual environment (venv)MacWindowsCloning the repository Installing dependenciesInitializing the Database before running the applicationSetting the Flask environment variablePopulating the DatabaseUsing a SQL Client (The application need not be running for this)Using a Python Script (The application should be running for this)Running the ApplicationExplanation of the routes
Prerequisites
- Python 3.x installed on your system
- Git installed on your system (optional, if you want to clone the repository)
Setting up a virtual environment (venv)
Mac
- Open Terminal.
- Navigate to the directory where you want to create the virtual environment.
- Run the following command to create a virtual environment named "venv":
python3 -m venv venv
- Activate the virtual environment:
source venv/bin/activate
Windows
- Open Command Prompt.
- Navigate to the directory where you want to create the virtual environment.
- Run the following command to create a virtual environment named "venv":
python -m venv venv
- Activate the virtual environment:
venv\Scripts\activate
Cloning the repository
If you haven't already, clone the Git repository to your local machine:
git clone https://github.com/manasa99/demo_pomodoro.git
Installing dependencies
- Navigate to the project directory (
cd demo
).
- Install the required dependencies using pip:
pip install -r requirements.txt
Initializing the Database before running the application
If you are running your Flask app using
python run.py
, setting FLASK_APP=app.py
is not required. The FLASK_APP
environment variable is used to specify the entry point of your Flask application when you run the flask
command directly, such as with flask run
. Since you are running your app with python run.py
, you are explicitly specifying the entry point in your run.py
file, so the FLASK_APP
environment variable is not needed in this case.You typically initialize the database once before starting your application or whenever you need to reset the database. This can be done in the
init_db()
function or similar, and you can call this function from your run.py
file or any other initialization script you have.But, as we will be using
flask
to initialize the db, we will not be required to create a method for that but rather set the environmental variable to use Flask
.Setting the Flask environment variable
For Mac
export FLASK_APP=app.py
For Windows
set FLASK_APP=app.py
Incase of powershell
$env:FLASK_APP = "app.py"
Now for the DB
flask db init # Initialize the database flask db migrate # Create the initial migration flask db upgrade # Apply the migration to create the tables
Populating the Database
When using the database for the first time, it will be empty. You can populate the database in two ways:
You can choose either way or test both.
Using a SQL Client (The application need not be running for this)
- Before Running the Application:
- Use a SQL client to connect to the database.
- Execute the SQL script
initialise_db.sql
to populate the database with initial data.
Example (for both Mac and Windows):
sqlite3 instance/pomodoro.db < initialise_db.sql
Note: If you don't have the path set for
sqlite3
, you can use the full path to the sqlite3.exe
executable.C:\path\to\sqlite3.exe instance\pomodoro.db < initialise_db.sql
You might have observed that the model name we have is TimerData, but when querying through sql we use timer_data. Try to find out why!?
Using a Python Script (The application should be running for this)
- While the Flask Application is Running:
- Run the Python script
initialize_db.py
in another terminal. - This script interacts with the Flask application to populate the database.
Example:
python initialize_db.py
Note: Ensure that the Flask application is running when you execute the Python script.
Running the Application
Start the Flask application:
You can run the application in 2 ways:
- Using flask (This might not work if you have not set up the flask environment variable)
flask run
- Or using python
python run.py
Access the application:
Open your web browser and navigate to
http://localhost:5000
By default the flask application runs on port
5000
until or unless specified otherwise in the app.run(debug=True)
Explanation of the routes
Here's an explanation for each route in the
routes.py
:/
and/<name>
(GET):- Description: Returns a rendered template
hello.html
with a greeting message. If a name is provided in the URL, it uses that name in the greeting. - Example:
/
returns "Hello, world!",/Alice
returns "Hello, Alice!".
/meth
(GET, POST, PUT, DELETE):- Description: Returns a string indicating the HTTP method used in the request.
- Example: A GET request to
/meth
returns "This is a GET request", and so on for other methods.
/timerdata
(GET):- Description: Returns all timer data if no
timer_id
is provided, or returns data for a specific timer iftimer_id
is provided as a query parameter. - Example:
/timerdata?timer_id=123
returns data for timer withid
123.
/timerdata
(POST):- Description: Creates a new timer with the provided data in the request body.
- Example: POST request to
/timerdata
with JSON data like{"name": "TimerName", "time": 60, "user_id": "user123"}
creates a new timer.
/timerdata/<timer_id>
(PUT):- Description: Updates an existing timer with the provided
timer_id
using the data in the request body. - Example: PUT request to
/timerdata/123
with JSON data like{"name": "UpdatedTimerName", "time": 120}
updates the timer withid
123.
/timerdata/<timer_id>
(DELETE):- Description: Deletes the timer with the provided
timer_id
. - Example: DELETE request to
/timerdata/123
deletes the timer withid
123.
Find out what Query Params are
These routes will give you an idea of how we can use different HTTP methods, how we can use the render template and covers the CRUD for the timer_data.
Testing HTTP methods through CLI
We can directly test the
GET
methods through our browser. For the other methods we will be needing tools like Postman or we can also send the requests through terminal/cli.Here is how we can do these with terminal or cli:
POST:
curl -X POST http://localhost:5000/timerdata -H "Content-Type: application/json" -d "{\"name\":\"<timer_name>\",\"time\":<time>,\"user_id\":\"<user_id>\"}"
PUT:
curl -X PUT http://localhost:5000/timerdata/<timer_id> -H "Content-Type: application/json" -d "{\"name\":\"<updated_name>\",\"time\":<updated_time>,\"user_id\":\"<updated_user_id>\"}"
Delete:
curl -X DELETE http://localhost:5000/timerdata/<timer_id>
Replace
<timer_id>
, <timer_name>
, <time>
, <user_id>
, <updated_name>
, <updated_time>
, and <updated_user_id>
with appropriate values.