How to use win32com with Dash

Hi everyone!
I’m new using dash but I think it awesome and has a lot of potential.
Recently I’ve tried to create an application that get information from a Windows application (SAP) and show this information with graphs to users.
I tried a test but I can’t connect with the program correctly with “win32com.client.GetObject(“SAPGUI”)” as I usually do. Here is my example where I link the function to the callback of the button and performance a simply task. It seems that when I run the server, the function of win32com is blocked.

import dash
import dash_html_components as html
import dash_core_components as dcc
import win32com.client
import sys

# Function to return to the initial page for ARP
def SAP_VolverInicio():
    try:
        
        # Conectamos
        SapGuiAuto = win32com.client.GetObject("SAPGUI")
        if not type(SapGuiAuto) == win32com.client.CDispatch:
          return
        SapGuiAuto = win32com.client.GetObject("SAPGUI")
        application = SapGuiAuto.GetScriptingEngine
        connection = application.Children(0)
        session = connection.Children(0)
        
        cabecera = session.findById("wnd[0]").Text.strip()
        while cabecera[:15]  != "SAP Easy Access":
                session.findById("wnd[0]").sendVKey(15)
                cabecera = session.findById("wnd[0]").Text.strip()
                if session.Children.count > 1:
                    session.findById("wnd[1]").Close
    except:
        print(sys.exc_info())
    
    finally:
        session = None
        connection = None
        application = None
        SapGuiAuto = None

external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']

app = dash.Dash(__name__, external_stylesheets=external_stylesheets)

app.layout = html.Div([
    html.Div(dcc.Input(id='input-box', type='text')),
    html.Button('Submit', id='button',n_clicks=0),
    html.Div(id='container-button-basic',
             children='Enter a value and press submit')
])

@app.callback(
    dash.dependencies.Output('container-button-basic', 'children'),
    [dash.dependencies.Input('button', 'n_clicks')]) 
                                                    
def update_output(n_clicks):
    if n_clicks>0:
        SAP_VolverInicio()
        return 'Test'

if __name__ == '__main__':
    app.run_server(debug=True, threaded=True)

Any help or idea would be very helpful.

Thank you very much.

I know this has been a while but wondering if you managed to figure it out?

I just want to say Thank you to @Victor_90 . I’ve been trying to get my head around how to get data from SAP, but hadn’t stumbled upon anything. This made my life SO MUCH EASIER.

I haven’t tried using SAP from within Dash, but if I do I’ll post my comments.

Thanks a lot!

I am new with GUI scripting and python. I ran into the same exact issue with what @Victor_90 described in his original tread. Is using Dash app as an interface to trigger running of SAP GUI scripting an impossible idea? Thank you.

Hello @Lilly_24,

Welcome to the community!

I think this is possible in these options:
1 - If Dash is run locally on a computer with SAP already open and you have routes mapped to open in the SAP app
2 - Dash is able to connect to the backend data that SAP has, either through APIs or the data and then is able to interact or pull to display
3 - The user takes info from SAP as a csv and loads into Dash to display