Are you looking for a place to run Python and Jupyter notebooks in an SAP environment? SAP Business Application Studio (BAS) might be the answer.
This blog shows how to set up a Python notebook in BAS, specifically for triggering the Machine Learning in SAP HANA Cloud or SAP Datasphere. That setup could be useful for an AI/ML expert who is creating and saving a prediction for business users to benefit from. Those business users could receive the insight for example in SAP Analytics Cloud, or wherever they need that information.
Thanks go to our Developer Advocate @Vitaliy-R who introduced me to these steps in SAP Business Application Studio!
Agenda
The architecture for this example is as straightforward as it can be. Your data is in SAP HANA Cloud, where we also have Machine Learning algorithms (Predictive Analysis Library and Automated Predictive Library). We then use Python in a Jupyter Notebook running in SAP Business Application Studio to trigger the Machine Learning algorithms in SAP HANA Cloud. This is possible with SAP's hana_ml Python package.
This blog focuses mostly on a stand-alone SAP HANA Cloud. However, you can also trigger the same Machine Learning in SAP Datasphere's embedded SAP HANA Cloud. This blog has more details.
More components are typically needed for a productive environment. To automate the creation of predictions the Python code can be schedules for instance with Cloud Foundry or SAP AI Core.
This blog assumes that:
- You have SAP Business Application Studio up and running
- You have access to a SAP HANA Cloud instance, which has the Script Server activated
Go into the SAP Business Application Studio, for instance via the SAP Build Lobby. Create a new Dev space, name it “hana_ml”. Chose the application kind “Basic” and in “Additional SAP Extensions” tick “Python Tools”. Click “Create Dev Space”.
After a short while the space will switch to “RUNNING”. Click on the space to open it. You are now in the development environment of the SAP Business Application Studio. In the menu on the left open the “Extensions” section and “OK” an initial message that might pop up. You can then see that the extensions for both “Python” as well as “Jupyter” are already enabled / available. There is no “install” button available for those for instance, which means that they are already installed.
Now create a permanent Python environment into which you can install additional Python packages that will remain in place after restarting the Dev Space. Without these steps, any package installations would need to be reinstalled after a restart.
- Open the Terminal with “View” → “Terminal”.
- You can check which Python version is being used: python –version
- Create a folder for the environment with this command: mkdir ~/projects/hanaml
- Go into the newly created folder: cd ~/projects/hanaml
- Create a new Python environment called hanaml_env: python3 -m venv hanaml_env –upgrade-deps
- Activate this new environment: source hanaml_env/bin/activate
- Install SAP's Python package hana_ml: pip install hana_ml
With the following steps you open a Jupyter Notebook that uses the new Python environment:
- IMPORTANT: Before opening a Jupyter Notebook you need to select the folder, in which your custom Python environment is installed. Click on “Explorer” in the menu in the left. Click “Open Folder”, then enter the path to the folder, which contains the custom environment, ie “home/user/projects/hanaml”.
- To create and open a Jupyter Notebook go to the search box at the top of the page and select “Show and Run Commands”.
- Type Jupyter into the search box and click “Create: New Jupyter Notebook”. A Jupyter Notebook opens up.
- It is not yet using the Python environment that you created earlier. To switch to that new environment, click on “Select Kernel” on the top right hand side. Choose “Python environments…”. Then select the hanaml_env environment, which includes the hana_ml Package. Now the top right hand side of the Notebook shows the name of that kernel.
To verify that the hana_ml package is indeed available, run these two lines of code, to output the version of that package.
import hana_ml
print(hana_ml.__version__)
You should see a version like “2.28.26042901“, whatever the current version is.
To save the Notebook, just use the menu on the left: File → Save.
Now let's look at SAP HANA Cloud so that we can connect to it from the Jupyter Notebook in the SAP Business Application Studio. You can use an existing SAP HANA Cloud instance. It just needs to be configured and sized appropriately (see below). When creating a new instance on HANA Cloud Central, select these options:
- For full control over the specifications select the option “Configure Manually”:
- Select a Memory of at least 48 GB. That is the minimum for the Script Server.
- Select “Specific IP addresses and IP ranges (in addition to Cloud Foundry in SAP BTP)”. In most cases this will allow the SAP Business Application Studio to connect. If you would like to add BAS's external IP addresses on the allow list, you find these IP addresses in the documentation.
- Under Additional Features select “Script Server”.
Once SAP HANA Cloud is running, create a database user for Python to connect from BAS to SAP HANA Cloud. Open the SAP HANA Database Explorer (or your preferred SQL interface) and logon with the DBADMIN user. Create the user with these statements, which assign the required roles.
CREATE USER AIUSER PASSWORD YOURSECRETPASSWORD NO FORCE_FIRST_PASSWORD_CHANGE;
ALTER USER AIUSER DISABLE PASSWORD LIFETIME;
GRANT AFL__SYS_AFL_AFLPAL_EXECUTE TO AIUSER;
GRANT AFLPM_CREATOR_ERASER_EXECUTE to AIUSER;
One more thing, we need the endpoint of the SAP HANA Cloud instance to create a connection. Copy this from HANA Cloud Central.
Everything is in place to establish a connection from the Jupyter Notebook in the SAP Business Application Studio to SAP HANA Cloud. Enter your logon credentials in this code. The SQL endpoint needs to be passed as address parameter, but you have to remove the port from the string (“:443”). The port is passed as separate parameter, but for SAP HANA Cloud this is always 443.
import hana_ml.dataframe as dataframe
conn = dataframe.ConnectionContext(address="YOURENDPOINT",
port=443,
user="AIUSER",
password='YOURSECRETPASSWORD'
)
conn.connection.isconnected()
The isconnected() method should return “True”.
This blog showed how to set up Python in SAP Business Application Studio to trigger HANA Cloud Machine Learning. Now you may want to actually trigger some Machine Learning in SAP HANA Cloud.
A separate blog for the Machine Learning is on the way. Most likely it is going to be an updated version of the “Hands-on Tutorial: Machine Learning with SAP HANA Cloud”. If you don't want to wait, you can already get started with that example.



