Python Forum

Full Version: code not working when executed from flask app
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi all!

To start I have to say i am new to Python...
But i mannaged to do nice things allready. But now have a problem that i just dont know how to fix:

when i execute this code it works:

debug.py:
import main_debug


main_debug.omnic_ready()
print("test successful")
but when i execute the 'main_debug.omnic_ready() from app.py it does not work:

from flask import Flask, render_template, request, Response, url_for, flash, get_flashed_messages, redirect

import main_debug
app = Flask(__name__)
app.secret_key = "Secret_key"


@app.route('/FTIR_status', methods=['POST', 'GET'])
def FTIR_status():
    main_debug.omnic_ready()
    return render_template("index_wbase.html", graph=False)
    # Get the batch name from the form data


if __name__ == "__main__":
    app.run(debug=True, port=5005)
I have been able to execute functions from the app.py flask application, but never this one.. I will show the rest of the code so you see what is hapening.
but i guess it has something to do with the permissions of flask and the DDE server that is used:

main_debug.py:
from Omnic_adrem_debug import Omnic


def omnic_ready():
    global omnic
    omnic = Omnic()
    try:
        omnic.initialize_omnic()
        print("Omnic ready for analyzing")
    except Exception as e:
        print("Something went wrong:", e)
and here is the Omnic_adrem_debug.py:

from OmnicDDE import OmnicDDE

MyOmnic = OmnicDDE()

class Omnic:
    def __init__(self):  # , gantry
        try:
            MyOmnic.GetExperimentalSettings()
        except NameError:
            print("OmnicDDE object not instantiated")

    def initialize_omnic(self):
        self.OMNIC_ready = False
        if not self.OMNIC_ready:

            print("Get Omnic ready")
            MyOmnic.GetExperimentalSettings()
            print("Set scan numbers to 10")
            MyOmnic.SetNumScans(10)
            print("Clear Window")
            MyOmnic.ClearWindow()
            print("collect background")
            MyOmnic.CollectBackground("Background")
            self.OMNIC_ready = True
            print("Omnic Is ready for sampling")
and finally here is the OmnicDDE.py code:
# For Python 3.XX, use pywin32 (pip install --upgrade pywin32)
import win32ui
import dde
import time
from OmnicExperimentalSettings import ExperimentalSettings


class OmnicDDE():
    Application = "Omnic"
    Thread = "Spectra"

    def __init__(self):
        self.server = dde.CreateServer()
        self.server.Create("MyOmnic_server")
        self.MyOmnicConversation = dde.CreateConversation(self.server)
        self.MyOmnicConversation.ConnectTo(self.Application, self.Thread)
        print(time.asctime() + "\t" + "Connection With Omnic Established")

        # Minimize the Omnic window
        self.MyOmnicConversation.Exec('[MinimizeWindow]')

    def CollectBackground(self, title=""):
        # title = kwargs.get('title','')
        cmd = '[Invoke CollectBackground """' + title + '""" Auto Polling]'
        self.MyOmnicConversation.Exec(cmd)
        print(time.asctime() + "\t" + "Collecting Background ...")
        CurrentWindow = self.MyOmnicConversation.Request('Window Title')
        self.MyOmnicConversation.Exec('[MaximizeWindow """' + CurrentWindow + '"""]')
        self.MyOmnicConversation.Exec('[MinimizeWindow]')
        MS = self.MyOmnicConversation.Request('MenuStatus CollectBackground')
        while MS == "Disabled":
            time.sleep(1)
            MS = self.MyOmnicConversation.Request('MenuStatus CollectBackground')
        self.MyOmnicConversation.Exec('[MinimizeWindow]')
        print(time.asctime() + "\t" + "Background Collection Finished")
        # If the user does not specify the title, he probably doesn't want to save it
        if title == "":
            time.sleep(0.5)
            self.MyOmnicConversation.Exec("[DeleteSelectedSpectra]")
So I only have problems when the code gets executed from the app.py (flask web interface) and not from the debug.py.
The error that i get is:
dde.error: Request failed
Thanks a lot for the help
(what I allready tried, is opening powershell as admin and starting the app.py from powershell but the problem stays the same)
hmm, no response yet..
Is there some more information that I could provide to help me find the sollution?
like I said, I think, it has something to do with the flask app and DDE server rights..