In this section, you will use a new Python library that automatically generates Swagger API documentation from your source code. Later you will add new services and routes to your application, adding new number-crunching features to the cloud-based API.
- Open the
username
-python-microservice repository in Visual Studio Code. - Install the latest Flask-RESTX by
- Adding
flask-restx = "*"
to the bottom ofPipfile
.
- Adding
- Delete the
Pipfile.lock
file to forcepipenv
to read your modifiedPipfile
. - Open the
server/__init__.py
file, perform the following modifications and save your work.- Add
from flask_restx import Api
to theimport
list at the top of the file. - Add the following line below the definition of the
app
object in line 6.api = Api(app, title='My first Python API', version='1.0', doc='/apidocs/', description='A number-crunching API')
- Add
- Create a new
math.py
file inserver/routes/
with the code shown below.from server import api from flask import jsonify from flask_restx import Resource @api.route('/double/<int:number>') @api.doc(params={'number': 'Number to be doubled.'}, description='This method doubles the input.') class DoubleNumber(Resource): def get(self, number): return jsonify(result=2 * number)
- Commit your changes to the
Pipfile
,server/__init__.py
andmath.py
files. - Sync your commits with the GitHub repository.
- Track the deployment progress in the Continuous Delivery Pipeline.
- Go to http://username-python-microservice.mybluemix.net/apidocs/.
- Observe how the header in the top reflects the line recently added to
server/__init__.py
. - Click on the Default namespace and the
/double/{number}
endpoint. - Observe how the blue card reflects the contents of
server/routes/math.py
. - Try to guess from the documentation the purpose and data input format of this method.
- Click Try it out, enter an input in the text box and hit Execute.
- Try several values for the
required
field and observe the response.- A string:
word
- A symbol:
+
- A floating point number:
1.5
- An integer number:
42
- A string:
- Go to http://username-python-microservice.mybluemix.net/double/42 and observe the response.
- Open the
username
-python-microservice repository in Visual Studio Code. - Install the latest NumPy by
- Adding
numpy = "*"
to the bottom ofPipfile
.
- Adding
- Create a new file inside
server/services/
and name itmath_services.py
. - Start
math_services.py
by importing the Python module you have just added toPipfile
.import numpy as np
- Implement a service that converts a string with comma-separated integer values to a Python list of integers.
- Hint: copy
csv_to_list()
fromapp/math_services.py
.
- Hint: copy
- Implement a service that converts a list of integer values to an integer NumPy array.
- Hint: copy
list_to_array()
fromapp/math_services.py
.
- Hint: copy
- Implement a service that calculates the mean of a list of integer values.
- Hint: copy
calculate_mean()
fromapp/math_services.py
.
- Hint: copy
- Commit your changes to the
Pipfile
andmath_services.py
files. - Sync your commits with the GitHub repository.
- Track the deployment progress in the Continuous Delivery Pipeline.
- Open the
username
-python-microservice repository in Visual Studio Code. - Create a new file in the main repository directory named
runtime.txt
by clicking the New File button. - Check the latest Python buildpack version available at IBM Cloud.
- Add
python-X.Y.Z
toruntime.txt
reflecting the latest available version.- Example:
python-3.6.4
- Example:
- Commit your changes to the
runtime.txt
file. - Sync your commits with the GitHub repository.
- Track the deployment progress in the Continuous Delivery Pipeline.
- Open the
username
-python-microservice repository in Visual Studio Code. - Open
math.py
and remove the@api.route('/double/<int:number>')
code block. - Add
from server.services import math_services as ms
to the import list at the top of the file. - Add below
my_list = list()
to create an empty Python list. - Create an API method that prints the content of the list.
- Hint: copy lines 9-13 from
app/custom-math.py
.
- Hint: copy lines 9-13 from
- Create an API method that resets the content of the list.
- Hint: copy lines 16-21 in
app/custom-math.py
.
- Hint: copy lines 16-21 in
- Create an API method that reverses the content of the list.
- Hint: copy lines 24-29 in
app/custom-math.py
.
- Hint: copy lines 24-29 in
- Create an API method that sorts the content of the list.
- Hint: copy lines 32-37 in
app/custom-math.py
.
- Hint: copy lines 32-37 in
- Create an API method that extends the content of the list.
- Hint: copy lines 40-46 in
app/custom-math.py
.
- Hint: copy lines 40-46 in
- Create an API method that replaces the content of the list.
- Hint: copy lines 49-56 in
app/custom-math.py
.
- Hint: copy lines 49-56 in
- Create an API method that calculates the mean of the content of the list.
- Hint: copy lines 59-64 in
app/custom-math.py
.
- Hint: copy lines 59-64 in
- Place your mouse cursor over
csv_to_list()
andcalculate_mean()
to read the documentation. - In the end, your file should look like
app/custom-math.py
. - Commit your changes to the
math.py
file. - Sync your commits with the GitHub repository.
- Track the deployment progress in the Continuous Delivery Pipeline.