Is there any option to get ipython command line while flask is running

Let me now explain my problem with an example.

Here is some example GUI code with Tkinter

from Tkinter import *
root = Tk()
w = Label(root, text="Hello, world!")
w.pack()
root.mainloop()

      

If I run this code in Ipython, I won't get the command line when the GUI is visible. Now if I comment out the "root.mainloop ()" line, the code is still running in Ipython and I have command line access so I can validate the data when I run the code.

Now, coming to the Flask body,

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello World!'

app.run()

      

When I run this application in Ipython, I don't get the command line. To access the variables while the code is running, I need to stop the flack server.

Is there any option for starting the flack server and accessing the command line?

thank

+3


source to share


2 answers


run the flask app in a separate thread. try this example: -

from flask import Flask                                                         
import thread
data = 'foo'
app = Flask(__name__)
@app.route("/")
def main():
    return data
def flaskThread():
    app.run()
if __name__ == "__main__":
    thread.start_new_thread(flaskThread,())

      



run this file in ipython: - "run -i filename.py" then you can access the ipython terminal to check.

+1


source


I am second to @NewWorld and recommend the debugger. You can test the program in the IPython shell using the IPython debugger. Installation, for example. from:

pip install ipdb

      

Then load the debugger with: ipdb.set_trace()

like;



@app.route('/')
def hello_world():
    import ipdb; ipdb.set_trace()
    return 'Hello World!'

      

This will open the IPython command line so you can check the "data while the code is running".

More information:
Look here to get started with ipdb.
 This site gives a short introduction to the available commands once inside the debugger.

+1


source







All Articles