Technology Blogs by SAP
Learn how to extend and personalize SAP applications. Follow the SAP technology blog for insights into SAP BTP, ABAP, SAP Analytics Cloud, SAP HANA, and more.
cancel
Showing results for 
Search instead for 
Did you mean: 
Fukuhara
Product and Topic Expert
Product and Topic Expert
3,609

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:

  1. Creation of a Dev Space.
  2. Initial setup and configuration of the Dev Space
  3. Implementation of a Python application.
  4. 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.

  1. necessary services are active
  2. necessary authorizations are assigned

Procedures

1. Creation of Dev Space

On SAP BAS screen, click on "Create Dev Space" button.

01.create_dev_space.png

With the below input, click on "Create Dev Space" button.

  1. Fill in the Dev Space name
  2. select "Basic" for application kind
  3. select Python Tools in Additional SAP Extensions

02.dev_space_initial.jpeg

2. Initial setup and configuration of the Dev Space

Click on Dev Space name after the instance starts.

03.DevSpaceName.jpeg

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.

01.create_empty_project.jpeg

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.

02.Runtime.jpeg

After that, create python environment.

07.CreatePythonEnvironment.jpeg

select ".venv".  I just prefer .venv to .conda for python development.

Fukuhara_0-1718790223483.png

select "~/.asdf-inst/shims/python3".

Fukuhara_1-1718790349470.png

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.

Fukuhara_2-1718790573982.png

Click on "Select Kernel" and choose "Python Environments"

Fukuhara_3-1718790683528.png

select ".venv".

Fukuhara_4-1718790757845.png

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.

09.CreateTerminal.jpeg

Then terminal runs and it automatically activate configured python.

10.terminal.jpeg

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.

12.RunFlask.jpeg

"Hello World" is displayed in a new tab.

14.FlaskScreen.jpeg

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.

Fukuhara_5-1718791022532.png

 

Deployed URL is shown on the terminal, so access the URL via browser.

14.cf_web.jpeg

If you don't need the app on cloud foundry, just delete the app.

 

cf delete <app name>

 

 

1 Comment
ptharso
Participant
0 Kudos

Como fazer uma instalação de Django Python Sqlite?