Python Forum
code not working when executed from flask app
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
code not working when executed from flask app
#1
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)
Reply
#2
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..
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to not open the context menu if a mouse gesture is executed? MicheliBarcello 2 675 Aug-22-2023, 02:47 PM
Last Post: deanhystad
  return next item each time a function is executed User3000 19 2,311 Aug-06-2023, 02:29 PM
Last Post: deanhystad
  New to Python - Not sure why this code isn't working - Any help appreciated TheGreatNinx 4 974 Jul-22-2023, 10:21 PM
Last Post: Pedroski55
  I am new to python and Could someone please explain how this below code is working? kartheekdas 2 1,019 Dec-19-2022, 05:24 PM
Last Post: kartheekdas
Exclamation My code is not working as I expected and I don't know why! Marinho 4 1,093 Oct-13-2022, 08:09 PM
Last Post: deanhystad
  My Code isn't working... End3r 4 1,942 Mar-21-2022, 10:12 AM
Last Post: End3r
  Where is the memory leak in this Flask Code? Morkus 1 5,612 Jun-18-2021, 02:48 PM
Last Post: Oliver
  I don't undestand why my code isn't working. RuyCab 2 1,998 Jun-17-2021, 03:06 PM
Last Post: RuyCab
  code is not working , can anybody help? RandomPerson69 4 2,919 Mar-22-2021, 04:24 PM
Last Post: deanhystad
  Short code for EventGhost not working Patricia 8 3,707 Feb-09-2021, 07:49 PM
Last Post: Patricia

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020