Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
nitishkumarrao
Explorer

In this tutorial, we'll walk through the process of integrating a Python application with SAP Business Application Studio for an SAP S/4HANA Cloud System. We'll start by demonstrating an example of a URL shortener application, similar to the one showcased in the previous blog. Then, we'll explore the steps to integrate this application with SAP Business Application Studio and deploy it to an SAP S/4HANA Cloud System.

Example: URL Shortener Application

Before we dive into integration, let's briefly recap the URL shortener application:

  • The application is built using Python and Flask.
  • It provides functionality to shorten long URLs into shorter ones.
  • Users can input a long URL, and the application generates a shortened version of it.
  • The application stores the mapping between the long and short URLs.

Now, let's proceed with integrating this application with SAP Business Application Studio.

Integration Steps

Step 1: Setting up the Project
First, let's create a new directory for our project and navigate into it: 

nitishkumarrao_0-1714823384624.png
nitishkumarrao_2-1714823456670.png
Step 2: Writing the Flask Application
Create main.py in your folder and paste the following code:-

 

from flask import Flask, render_template, request, redirect, flash, abort
import hashlib
import validators
import os

app = Flask(__name__)

url_mapping = {}

def generate_short_url(long_url):
    hash_object = hashlib.sha1(long_url.encode())
    hash_hex = hash_object.hexdigest()[:6]
    return hash_hex

@app.route('/', methods=['GET', 'POST'])
def home():
    if request.method == 'POST':
        long_url = request.form['long_url']
        if validators.url(long_url):
            if long_url in url_mapping:
                short_url = url_mapping[long_url]
            else:
                short_url = generate_short_url(long_url)
                url_mapping[long_url] = short_url
            return render_template('index.html', short_url=request.url_root + short_url)
        else:
            flash('Invalid URL. Please enter a valid URL.', 'error')
    return render_template('index.html')

@app.route('/<short_url>')
def redirect_to_long_url(short_url):
    for long_url, mapped_short_url in url_mapping.items():
        if mapped_short_url == short_url:
            return redirect(long_url)
    abort(404)

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=int(os.environ.get('PORT', 5000))

 

Step 3: Creating HTML Templates
Now, let's create an HTML template for our application. Create a new directory named templates inside the url_shortener directory. Inside the templates directory, create a new file named index.html and add the following content:-

 

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>URL Shortener</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            background-color: #f4f4f4;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }
        .container {
            width: 400px;
            padding: 20px;
            background-color: #fff;
            border-radius: 8px;
            box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
        }
        h1 {
            font-size: 24px;
            text-align: center;
            margin-bottom: 20px;
        }
        form {
            display: flex;
            flex-direction: column;
        }
        label {
            font-size: 16px;
            margin-bottom: 8px;
        }
        input[type="text"] {
            padding: 8px;
            margin-bottom: 16px;
            border-radius: 4px;
            border: 1px solid #ccc;
        }
        input[type="submit"] {
            padding: 10px 20px;
            background-color: #007bff;
            color: #fff;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            transition: background-color 0.3s ease;
        }
        input[type="submit"]:hover {
            background-color: #0056b3;
        }
        .short-url {
            font-size: 18px;
            margin-top: 20px;
            word-wrap: break-word;
        }
        .error {
            color: red;
            margin-top: 10px;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>URL Shortener</h1>
        <form action="/" method="post">
            <label for="long_url">Enter a URL:</label>
            <input type="text" id="long_url" name="long_url" placeholder="https://example.com">
            <input type="submit" value="Shorten URL">
        </form>
        {% if short_url %}
            <div class="short-url">
                Shortened URL: <a href="{{ short_url }}">{{ short_url }}</a>
            </div>
        {% endif %}
        {% with messages = get_flashed_messages() %}
            {% if messages %}
                <div class="error">{{ messages[0] }}</div>
            {% endif %}
        {% endwith %}
    </div>
</body>
</html>

 

 

 

Step 4: Running the Application Locally
To run the application locally, open a terminal, navigate to the url_shortener directory, and run the following command:-

nitishkumarrao_3-1714824421991.png

You should see output indicating that the Flask development server is running. Open your web browser and go to http://localhost:5000 to access the application.

Step 5: Deploying to Cloud Foundry
Now that we have our application working locally, let's deploy it to Cloud Foundry. Follow these steps:-

1. Log in to your Cloud Foundry account using the CLI:
nitishkumarrao_4-1714824773725.png

2. Create a Procfile in the project directory with the following content:nitishkumarrao_12-1714826722524.png

3. Create a requirements.txt file listing Flask as dependency:

nitishkumarrao_11-1714826676778.png

 

4. Create a manifest.yml file specifying file configurations:

nitishkumarrao_10-1714826618080.png

5. Create a runtime.txt file specifying the Python runtime version to use when deploying to Cloud Foundry:   nitishkumarrao_9-1714826531408.png

6. Run the following command to deploy your application to Cloud Foundry:
nitishkumarrao_5-1714825474921.png

After the deployment process completes, you will see a URL where your application is hosted. You can access your URL shortener application from any web browser.

nitishkumarrao_7-1714826020682.png

Python SAP BTP, Cloud Foundry runtime and environment 

Labels in this area