Flask keep running after closing terminal - python

I'm running since yesterday a Flask web application on Windows in dev mode through command line : "env FLASK_APP=theflaskapp.py python -m flask run".
Everything was going well untill today, Flask run the same code even if I change some code lines and it keeps running even if I close the terminal. I don't understand why.
Maybe you can help me, please !

Related

VS Code not identifying any changes made to the code:

Any changes I make to my code aren't doing anything. I am using three things html, flask and python. Any code change I make to my python or html code is not getting implemented on the browser when I am trying to run it.IT is only running older version of the code.
How can I rectify this error of vscode?
Make sure you're refreshing the page if you're opening a HTML file. If you're accessing the python server make sure to restart it every time you make changes to the code. The way to do it is ctrl+c in the terminal and running the command again. There are ways to make this happen automatically, refer here for more information
Did you start your flask server in debug mode?
from Python
app.run(debug=True)
or from terminal
$ export FLASK_DEBUG=1
$ flask run
Try to run in incognito once. Or try after clearing cache.

API not working

I am new to python and trying to create a simple API. below is the code for the same.
from flask import Flask
app = Flask(__name__)
#app.route("/")
def hello():
return "Hello World!"
if __name__ == '__main__':
app.run(host='0.0.0.0',port=5000)
I have saved this as code as "hello-world.py".
When I am trying to run this file in command prompt using python command
python hello-world.py
Command executed in command prompt
Here I have no error but web page is not getting displayed. Below is the error on web page.
http://localhost:5000/
This site can’t be reached
localhost refused to connect.
Search Google for localhost 5000
ERR_CONNECTION_REFUSED
1st Issue - Even after running the code properly, why the web page is not displayed
Now, I enter python command in my command prompt and then I try running file again python hello-world.py. Here I am getting the below error
>>> File "<stdin>", line 1
python hello-world.py
^
SyntaxError: invalid syntax
2nd Issue - Why I am getting an error while trying run the hello-world.py
Please guide me on how to resolve this issue.
From what you're describing, I think you're trying to use the Flask Quickstart. If so, have you done the following command in your command prompt enviroment?
C:\path\to\app>set FLASK_APP=hello.py
At which point if you run your code and it's correctly structured you should see
Running on http://127.0.0.1:5000/
Because you don't see the "Running" on your prompt I would assume this hasn't been done. If this doesn't work, I would just double check that Flask is installed and that Python sees it, and that you're following the steps in the tutorial correctly.
Edit: Also, I would recommend removing the hyphen in hello-world.py. It may help you avoid errors here and elsewhere in python coding. See the responses to this question.
1st issue - firewall or some other services may be blocking your port.
2nd Issue - you are running in interactive terminal. Please execute the command in command prompt.

Django runserver under Jenkins does not work

I have created a script to build my Django project which is called by my Jenkins CI, every time a push is made.
The script runs just fine if I run it manually, but fails to start the web server when it is ran automatically.
No errors are thrown, but the last line of the script:
nohup python manage.py runserver 0:9000 > /dev/null 2>&1 &
has absolutely no effect.
I am 100% sure that the script is ran as jenkins user, under my virtualenv, so that's not the problem. Also, permissions are not a problem, I have checked. Like I said, no error is thrown, so I don't really know what is happening.
Any ideas ?
So, thanks to tomrom95 I found the solution : adding BUILD_ID=dontKillMe in front of the command fixed everything. It's kind of funny.
Here is the link to a more complete answer to why this didn't work and why it works now.

Localhost has stopped updating when various flask/python scripts are run, how do I fix this?

I've been testing out a few .py files with Flask, referring to 127.0.0.1:5000 frequently to see if they're interfacing correctly with the HTML, after running the .py I'm getting the following as normal:
* Running on http://127.0.0.1:5000/
* Restarting with reloader
However, 127.0.0.1:5000 has suddenly stopped updating when scripts are run, remaining as it was after the first time a script was run since my computer has been turned on (restarting the machine is the only way I've found to take a fresh look at my work). To confirm that it's not an issue within .py files or my templates, Flask's hello world example with app.run(debug=True) does not update this localhost page when run. Is there any way to remedy this?
Two things that may or may not be involved with this issue:
(1) I am not using virtualenv but simply running .py files from folder directories on my desktop (following proper format for Flask and the template engine, though). (2) Around the time the problem started I installed SQLAlchemy and its Flash extension, Flask-SQLAlchemy with pip.
After tracking the processes down by running $ netstat -a -o in the command line, it turns out it wasn't a code error, but rather multiple instances of pythonw.exe, which can be taken care of in the Task Manager. I'm not sure why the processes keep running after I close out of all python windows, or why it keeps communicating with 127.0.0.1:5000, however, so thoughts on this would still be appreciated.
Thats right. Just press 'Ctrl+Shift+Esc' to open the task manager.
Scroll down to find the 'python3.exe' files and end the task manually.
The reason is 'ctrl+c' doesnt work for me (it just copies the text on terminal window), so I have to manually kill the python interpreter running in the background. Its hard work, but hey atleast you dont have to restart your computer everytime!!

How to debug Django app running on Heroku using a remote pdb connection?

To debug a bug I'm seeing on Heroku but not on my local machine, I'm trying to do step-through debugging.
The typical import pdb; pdb.set_trace() approach doesn't work with Heroku since you don't have access to a console connected to your app, but apparently you can use rpdb, a "remote" version of pdb.
So I've installed rpdb, added import rpdb; rpdb.set_trace() at the appropriate spot. When I make a request that hits the rpdb line, the app hangs as expected and I see the following in my heroku log:
pdb is running on 3d0c9fdd-c18a-4cc2-8466-da6671a72cbc:4444
Ok, so how to connect to the pdb that is running? I've tried heroku run nc 3d0c9fdd-c18a-4cc2-8466-da6671a72cbc 4444 to try to connect to the named host from within heroku's system, but that just immediately exits with status 1 and no error message.
So my specific question is: how do I now connect to this remote pdb?
The general related question is: is this even the right way for this sort of interactive debugging of an app running on Heroku? Is there a better way?
NOTE RE CELERY: Note, I've now also tried a similar approach with Celery, to no avail. The default host celery's rdb (remote pdb wrapper) uses is localhost, which you can't get to when it's Heroku. I've tried using the CELERY_RDB_HOST environment variable to the domain of the website that is being hosted on Heroku, but that gives a "Cannot assign requested address" error. So it's the same basic issue -- how to connect to the remote pdb instance that's running on Heroku?
In answer to your second question, I do it differently depending on the type of error (browser-side, backend, or view). For backend and view testing (unittests), will something like this work for you?
$ heroku run --app=your-app "python manage.py shell --settings=settings.production"
Then debug-away within ipython:
>>> %run -d script_to_run_unittests.py
Even if you aren't running a django app you could just run the debugger as a command line option to ipython so that any python errors will drop you to the debugger:
$ heroku run --app=your-app "ipython --pdb"
Front-end testing is a whole different ballgame where you should look into tools like selenium. I think there's also a "salad" test suite module that makes front end tests easier to write. Writing a test that breaks is the first step in debugging (or so I'm told ;).
If the bug looks simple, you can always do the old "print and run" with something like
import logging
logger = logging.getLogger(__file__)
logger.warn('here be bugs')`
and review your log files with getsentry.com or an equivalent monitoring tool or just:
heroku logs --tail

Categories

Resources