I am writing this blog to describe the configuration procedures required for Python development in SAP Business Application Studio. This process is divided into the following four key steps:
- Creation of a Dev Space.
- Initial setup and configuration of the Dev Space
- Implementation of a Python application.
- Deployment of the application to BTP Cloud Foundry.
Pre-requisites
As considerable time has elapsed since the setup of my environment, I am unable to provide detailed pre-requisites.
- necessary services are active
- necessary authorizations are assigned
Procedures
1. Creation of Dev Space
On SAP BAS screen, click on "Create Dev Space" button.
With the below input, click on "Create Dev Space" button.
- Fill in the Dev Space name
- select "Basic" for application kind
- select Python Tools in Additional SAP Extensions
2. Initial setup and configuration of the Dev Space
Click on Dev Space name after the instance starts.
2.1. Create new project
select "Creates empty project" from command palette and enter project name after the selection.
The project I'm going to deploy is very simple, so I don't create MTA app.
2.2. Create Python Environment
From command palette, set default python runtime. The default runtime is "/home/user/.asdf-inst/shims/python".
See "Runtime Version Management" for further detail.
* This step might be unnecessary, since I choose the same path when creating a python virtual environment.
After that, create python environment.
select ".venv". I just prefer .venv to .conda for python development.
select "~/.asdf-inst/shims/python3".
2.3. Install Jupyterlab
Though it is not mandatory step, I usually use jupyter for development.
Firstly, create a new jupyter notebook from command palette.
Click on "Select Kernel" and choose "Python Environments"
select ".venv".
When I run jupyter cells, jupyter is automatically installed.
3. Implementation of a Python application.
Implement simple python rest api app based on an article "Create simple Flask REST API using Cloud Foundry".
3.1. install Flask
Install Flask using pip.
Then terminal runs and it automatically activate configured python.
install Flask using pip.
(.venv) user: python-test $ pip install Flask
Collecting Flask
Downloading flask-3.0.3-py3-none-any.whl.metadata (3.2 kB)
Collecting Werkzeug>=3.0.0 (from Flask)
Downloading werkzeug-3.0.3-py3-none-any.whl.metadata (3.7 kB)
Requirement already satisfied: Jinja2>=3.1.2 in ./.venv/lib/python3.11/site-packages (from Flask) (3.1.4)
Collecting itsdangerous>=2.1.2 (from Flask)
Downloading itsdangerous-2.2.0-py3-none-any.whl.metadata (1.9 kB)
Collecting click>=8.1.3 (from Flask)
Downloading click-8.1.7-py3-none-any.whl.metadata (3.0 kB)
Collecting blinker>=1.6.2 (from Flask)
Downloading blinker-1.8.2-py3-none-any.whl.metadata (1.6 kB)
Requirement already satisfied: MarkupSafe>=2.0 in ./.venv/lib/python3.11/site-packages (from Jinja2>=3.1.2->Flask) (2.1.5)
Downloading flask-3.0.3-py3-none-any.whl (101 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 101.7/101.7 kB 7.2 MB/s eta 0:00:00
Downloading blinker-1.8.2-py3-none-any.whl (9.5 kB)
Downloading click-8.1.7-py3-none-any.whl (97 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 97.9/97.9 kB 8.4 MB/s eta 0:00:00
Downloading itsdangerous-2.2.0-py3-none-any.whl (16 kB)
Downloading werkzeug-3.0.3-py3-none-any.whl (227 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 227.3/227.3 kB 13.8 MB/s eta 0:00:00
Installing collected packages: Werkzeug, itsdangerous, click, blinker, Flask
Successfully installed Flask-3.0.3 Werkzeug-3.0.3 blinker-1.8.2 click-8.1.7 itsdangerous-2.2.0
3.2. app.py
create "app.py".
from flask import Flask
import os
app = Flask(__name__)
# Port number is required to fetch from env variable
# http://docs.cloudfoundry.org/devguide/deploy-apps/environment-variable.html#PORT
cf_port = os.getenv("PORT")
# Only get method by default
@app.route('/')
def hello():
return 'Hello World'
if __name__ == '__main__':
if cf_port is None:
app.run(host='0.0.0.0', port=5000, debug=True)
else:
app.run(host='0.0.0.0', port=int(cf_port), debug=True)
3.3. Test the app
In terminal, run "python app.py" and click on "Open in a New Tab" button.
"Hello World" is displayed in a new tab.
4. Deployment of the application to BTP Cloud Foundry
Create some files for deployment and deploy to BTP Cloud Foundry.
4.1. manifest.yml
manifest.yml for BTP Deployment. Probably this is the minimum specs to run the app.
---
applications:
- memory: 128MB
disk_quota: 512MB
random-route: true
4.2 Procfile
Target python application.
web: python app.py
4.3. runtime.txt
Target python version.
python-3.11.X
4.4. requirements.txt
Necessary python packages.
Flask
4.5. .cfignore
".venv" is unnecessary to run cf app, so list up the directory.
.venv
4.6. Deploy
Deploy the app to cloud foundry. My login target is eu10.
cf login -a https://api.cf.eu10.hana.ondemand.com
cf push <app name>
As an alternative way for login, you can use command palette.
Deployed URL is shown on the terminal, so access the URL via browser.
If you don't need the app on cloud foundry, just delete the app.
cf delete <app name>