Skip to Content

Create a Tiny World Python Application using the Flask framework to connect to SAP HANA, Express Edition.

By Daniel Wroblewski

Create a sample Python application using the Flask Web framework, which connects to SAP HANA, Express Edition


You will learn

This tutorial will guide you through the process of creating a sample Python/Flask application to connect to SAP HANA, Express Edition.

Step 1: Create a sample python application on your local machine

The tutorial assumes you have a UNIX machine. Please use equivalent commands if running a Windows environment.

  1. Open terminal and create a directory.
    ~$ mkdir TinyWorldFlaskApp
    ~$ cd TinyWorldFlaskApp/
  2. Run the following commands.
    ~TinyWorldFlaskApp$ touch
    ~TinyWorldFlaskApp$ touch requirements.txt
    ~TinyWorldFlaskApp$ mkdir templates
    ~TinyWorldFlaskApp$ cd templates
    ~TinyWorldFlaskApp/templates$ touch hello.html
    ~$ cd ..
  3. Copy the below code to the file in this directory:

    # [START application]
    import logging
    from flask import Flask
    from flask import render_template
    import pyhdb
    application = Flask(__name__)
    def connHXE():
        # define connection to the HXE database you have access to
        # replace the IP address and password below
          connection = pyhdb.connect(
          if not connection.isconnected():
              return 'HXE Server is not accessible'
          cursor.execute("SELECT * from M_DATABASE")
          allrows = ''
          row = cursor.fetchone()
          while row is not None:
            print (row)
            allrows = allrows + str(row)
            row = cursor.fetchone()
          if row is None:
              return render_template (
    	    host = user_provided_host,
    	    port = user_provided_port,
    	    user = user_provided_user,
    	    name= allrows )
              return 'Select failed'
    def server_error(e):
        #log the error and stacktrace
        logging.exception ('An error occurred during a request.')
        return 'An internal error occurred.', 500
    #run the app.
    if __name__ == "__main__":'', port =8080, debug=True)
    #[END application]
  4. Edit the file to specify your HXE IP address and the system user password:

    # replace the IP address and password below
  5. Copy the below lines to the requirements.txt file in this directory.

  6. Copy the following code to the file hello.html under the templates directory.

    <!doctype html>
    <title>HXE Select from HXE</title>
    {% if host %}
    <h1>IP Address: {{ host }}</h1>
    {% else %}
    <h1>IP Address missing</h1>
    {% endif %}
    {% if port %}
    <h1>HXE Port Number: {{ port }}</h1>
    {% else %}
    <h1>HXE Port Number missing</h1>
    {% endif %}
    {% if user %}
    <h1>HXE User Name: {{ user }}</h1>
    {% else %}
    <h1>HXE User Name missing</h1>
    {% endif %}
    {% if name %}
    <h1>M_DATABASE contents:</h1>
      {% for n in name %}</h1>
      {% endfor %}
    {% else %}
    <h1>M_DATABASE contents missing</h1>
    {% endif %}
Step 2: Run the application locally to test.
  1. Run the following commands at the root of the project directory.

    ~TinyWorldFlaskApp$ pip install virtualenv
    ~TinyWorldFlaskApp$ virtualenv env
    ~TinyWorldFlaskApp$ source env/bin/activate
    ~TinyWorldFlaskApp$ pip install -r requirements.txt
    ~TinyWorldFlaskApp$ python
    Sample Output of Deployment
  2. Browse the application at
    The results would look like below.

    Screenshot of browser output

You now have a working Python/Flask application connecting to SAP HANA.

Next Steps

Updated 09/03/2018

Time to Complete

15 Min.



  • Setup: HANA, express edition must be running and accessible from your client platform. For instructions on how to setup a HANA, express edition see the HANA Express database deploy tutorial.
  • This tutorial assumes that you have a database login that can access the M_DATABASE view in the HANA, express edition SystemDB.

Next Steps

Back to top