This assignment is a list of exercises for practicing Flask. You can find multiple features which you can use separately after the Preparation part. Do not forget to commit after you completed a part.
- Send HTTP requests with GET, POST, PUT and DELETE method.
- Use global variables with Flask and display their value on a page.
- Write different behaviors for different HTTP methods on the same endpoint.
-
Initialize an empty Flask project with a new virtualenv and a
.gitignorefile.- A
venvfolder containing a virtual environment with at least python 3.8 is created. - A
requirements.txtwith Flask as one of the requirements is created. - A
templatesfolder is created for your HTML template files. - A
server.pyis created, in which you can handle incoming HTTP requests. - A
.gitignorefile is created to ignore unnecessary venv, pyCharm, Flask, or python files.
- A
-
Create a
/route for the main page from which users can reach the features of the Request counter.- The page has a GET-Request link that sends a GET request to the
/request-counterroute - The page has a POST-Request button (in a form) that sends a POST request to the
/request-counterroute - The page has a Statistics link that navigates to the
/statisticsroute
- The page has a GET-Request link that sends a GET request to the
-
Create a
/request-counterroute that can count incoming HTTP requests.- The numbers of requests are stored for each HTTP method (GET, POST, DELETE, PUT) in separate global variables.
- After increasing the count this route redirects to main page.
-
Create a
/statisticsroute which can show the count results.- The page has a link which leads back to to home page.
- The page has a table which contains 4 rows (one for each HTTP method) and two columns (one for the method name and one for the request count).
-
Store the request count data in a text file instead of global variable.
- A text file named
request_counts.txtis created. - The
request_counts.txt's content is the following. GET: 5 POST: 3 DELETE: 1 PUT: 0
- A text file named
None
- You can send PUT and DELETE requests with Curl. This way you can try your request counter with all HTTP methods to make sure it works.
- Pip and VirtualEnv
- A web-framework for Python: Flask
- Flask documentation (the Quickstart gives a good overview)
- Install Curl
- Example to test DELETE/PUT request
- Ports