Recently, SAP Business Application Studio expanded its support to include Python.
This new inclusion now allows developers to create applications using Python. In this blog post, I will guide you through the process of setting up a Python-based development space in SAP Business Application Studio. We will set up a project and create a small web application that includes a REST API endpoint, that will fetch a list of books available on a website.
For more comprehensive information about setting up SAP Business Technology Platform (SAP BTP) and SAP Business Application Studio, please refer to the E-Bite that I co-authored for SAP PRESS. For those seeking more details about Python, please refer to this page.
Without further ado, let’s dive into the fascinating world of Python development: setting up a dev space and creating an application to interact with RESTful APIs. I am confident that this blog post will provide you with valuable insights and hands-on experience for your future Python projects.
Setting Up the Development Environment
To start, navigate to SAP BTP and create a trial account. Following the steps outlined in this post, set up SAP Business Application Studio and then launch the URL in the browser as shown in the figure below.
Next, select the dev space and assign a unique name to it, for example, "py_dev". From additional SAP extensions, choose Python Tools. (Depending on your needs and the nature of your development activities, you can also choose other extensions.) I selected a few more extensions for my example, but this is optional. Once you've made your selections, click on Create Dev Space. Please note that this may take a few minutes. See the figure below for reference on the steps.
When the status of the dev space creation is displayed as RUNNING, as illustrated in the next figure, you can click on it to open the dev space. Please note that loading may take some time.
After launching the dev space, proceed to the workspace and establish a new basic multitarget application. This can be done by choosing the New Project from Template option, as depicted in the following figures.
The rationale behind creating a basic multitarget template is that it enables you to establish a project with an mta.yml file. This file is essential for developing a full-stack application that can be deployed on SAP BTP, as it is used to construct an MTA-based application.
Developing the Program
First, open a terminal. Create a folder named "myweb." Navigate to the myweb folder by using the appropriate command. After you've navigated to the correct folder, install the specified Python package as demonstrated below.
The below list of commands needs to be executed at the terminal to complete the initial setup of a Python development environment, as well as creating a workspace.
After the installer package has been set up, proceed to open t1.py and apply the get method to retrieve the available book list. However, before you do that, it's crucial to touch on the Python packages necessary for implementing the Python web-based application. There are many Python web frameworks, with some being free and open source. Django, CherryPy, Falcon, Flask, and Tornado are among the most commonly used. For this case, I chose to use Falcon, a lightweight RESTful web framework, to implement the REST API due to its simplicity and minimal technical debt.
In this instance, I will design a REST API to fetch a list of available books, with the response returned as a JSON file. Furthermore, these APIs are asynchronous and are developed using ASGI (Asynchronous Server Gateway Interface). Please find the code snippet in the figure below.
In the content provided, we're utilizing Python's Falcon framework. First, we declare a JSON object which contains a list of books. Following that, we assign the JSON object. Then, we set this JSON object to the response body. Additionally, we specify the response type as JSON to indicate that the response object is of JSON type. Finally, we map the method to the route.
To test this program, navigate to the terminal and execute the following command:
> uvicorn t1:app
It will compile and run the application in the local port; click on the port as highlighted in the figure below.
To view the list of available books pulled in JSON format, click on the link provided. It will open in a new browser tab, and look something like this:
The extensibility of SAP Business Application Studio allows for the development of applications using the Python language, catering to developers who are comfortable with Python. The use of Python opens up limitless possibilities, particularly in the field of data science, as it provides the necessary tools for data transformation and AI-related development. Python's agility and extensive library ecosystem make it the preferred language for developers working on business logic, and with this new update from SAP, developers working with SAP can use Python now too!