-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhello_world_with_pythonandflask.txt
More file actions
34 lines (21 loc) · 1.37 KB
/
hello_world_with_pythonandflask.txt
File metadata and controls
34 lines (21 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// Creating a Hello World REST API with Python and Flask
In this lab you will create your first REST API Hello World application using the Flask.
1. Open a new Terminal in the lab environment.
2. Now you will write a Python code that will act as a web application serving REST API endpoint that returns the string Hello World.
Run the following command to create a file named server.py and the click the button below to edit the content of the file..
touch /home/project/server.py
3. Paste the following code in server.py and save the file.
from flask import Flask
app = Flask("My Hello World Application")
@app.route("/")
def hello():
return "Hello World! :)"
if __name__=="__main__":
app.run(debug=True)
# When no port is specified, starts at default port 5000
4. Before you run this web application, you need to install the packages that are required for the application to run.
On the terminal, run the following command to install the packages.
python3 -m pip install flask
5. Once the flask package is installed, run server.py to start a server which is listening at port 5000 and serving a REST API endpoint at the root / that returns the string Hello World!.
python3 server.py
6. To access the REST API endpoint through the browser, click the following button, which will open a new browser page with the string that is transferred back by the server application.