- Open the
username-python-microservice repository in Visual Studio Code. - Install the latest Flask-RESTPlus by
- Adding
flask-restplusto the bottom ofrequirements.txt. - Adding
flask-restplus = "*"to the bottom ofPipfile.
- Adding
- Open the
server/__init__.pyfile, perform the following modifications and save your work.- Add
from flask_restplus import Apito theimportlist at the top of the file. - Add the following line below the definition of the
appobject 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.pyfile inserver/routes/with the code shown below.from server import api from flask import jsonify from flask_restplus 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
requirements.txt,Pipfile,server/__init__.pyandmath.pyfiles. - 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
requiredfield 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
numpyto the bottom ofrequirements.txt. - Adding
numpy = "*"to the bottom ofPipfile.
- Adding
- Create a new file inside
server/services/and name itmath_services.py. - Start
math_services.pyby importing the Python module you have just added torequirements.txt.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
requirements.txt,Pipfileandmath_services.pyfiles. - 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.txtby clicking the New File button. - Check the latest Python buildpack version available at IBM Cloud.
- Add
python-X.Y.Ztoruntime.txtreflecting the latest available version.- Example:
python-3.6.4
- Example:
- Commit your changes to the
runtime.txtfile. - 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.pyand remove the@api.route('/double/<int:number>')code block. - Add
from server.services import math_services as msto 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.pyfile. - Sync your commits with the GitHub repository.
- Track the deployment progress in the Continuous Delivery Pipeline.