flask development single terminal: continue editing Code without quitting flask - flask

Background: I am new to Flask (and fairly new to python, linux terminal, servers). I have gone through the tutorial http://flask.pocoo.org/docs/1.0/tutorial/.
I am developing on raspberry pi 3, via putty from my PC.
I run flask, according to the tutorial:
export FLASK_APP=flaskr
export FLASK_ENV=development
flask run
According to the tutorial I can make changes to the code and the server will automatically reload.
My issue: I don't know how to make changes to my code without stopping Flask (ctrl+c).
How can I leave Flask, return to the Linux Terminal, edit my code, and then return to the Flask Debugger without stopping Flask each time?
I have reviewed the Flask Docs http://flask.pocoo.org/docs/1.0/ to no avail.
Thanks,

You has to enable the debug mode. Default debug is True in flask
app.run(debug=True) or export DEBUG=True
you can go through this link for Flask configuration
http://flask.pocoo.org/docs/1.0/config/#configuring-from-environment-variables
If you are using Flask >=1.0v you need to set environment in development mode.
export FLASK_ENV='development'

Related

Harvard University CS50 Lecture on Flask

I am taking CS50 lecture on Python Flask
https://www.youtube.com/watch?v=oVA0fD13NGI&t=5449s
I am using PyCharm instead of VS Code. Here is my problem.
When I make changes in my PyCharm code and "Save All" I should be able to just "refresh" my browser to see the changes, (That's what the CS50 instructor can do), but I have to Stop and Start my Flask server and then go back to my browser in order to see the change.
Is there something I need to turn on and/or configure in PyCharm so that when I make code changes in a Flask application I can just refresh the browser without having to Stop/Start the Flask server. Thanks in advance for any help on this issue.
if you running your application with flask run in terminal, you can use --debug before run, to run in watch mode like this flask --debug run.
Have another away to run in watch mode.
You can use this code in your main file
if __name__ == "__main__":
app.run(debug=True)
when you run, you have to use python like this python app.py.
You can see more in the flask documents Here

How to run Django function in Pycharm?

I have some internal utilities I want to develop for my Django project. These are utilities that I need to run mostly in my DEV environment, not in production.
I want to be able to call these from my PyDev IDE or Terminal. Is there a way to call functionality in my Django project without creating a view and using a URL to access it?
I've done several Google searches, but nothing is coming up.
If you open the "Python Console" on Pycharm, it will automatically set django up and then you can basically do anything you could do on a Django view. You can write your function anywhere and import and call them. To do the setup manually, you'll need these for lines of code after launching a Python interpreter:
import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'path.to.your.settings')
import django
django.setup()
# Write code using Django models here
you could import them to your PyCharm console starting script. In settings under: Build, Execution, Deployment -> Console -> Python Console -> Starting script

compiled django application fails to run ASGI channels

I'm building a django application using Pyinstaller.
When I run the application i get an error from dapne server.
"daphne\server.py:13: UserWarning: Something has already installed a non-asyncio Twisted reactor. Attempting to uninstall it; you can fix this warning by importing daphne.server early in your codebase or finding the package that imports Twisted and importing it later on."
In addition, when I compare the console log of the EXE application and a regular run of the django application I notice the following difference.
In the regular run which works as expected I see the following print:
"Django version 2.1.5, using settings 'cfehome.settings'
Starting ASGI/Channels version 2.1.5 development server at http://0.0.0.0:8000/"
however, when I run the application from the EXE I see:
"Django version 2.1.5, using settings 'cfehome.settings'
Starting development server at http://0.0.0.0:8000/"
Appreciate any lead to understand this behavior and ways to fix it.
I know this is old but for others seeking answer. There's a simple fix:
Before importing raven in settings.py, import daphne.server So, your settings.py will look like this:
import daphne.server
import raven
For further information you can read: https://github.com/django/channels/issues/793
I hope it will help.

Flask app doesn't open in browser while using flask cli

I wrote flask web which I was testing by running the server like ‘python src/run.py’
Now recently I decided to use flask cli approach.
So I did ‘export FLASK_APP=src/run.py‘
then ‘flask run‘ it work starts web app at 5000 by default but nothing opens in browser.
I do not get any error. Do I have to create FlaskGroup to create flask cli run command explicitly?

How to use Werkzeug interactive traceback debugger in a Django project hosted on Heroku?

With django-extensions installed in your Django project, on a local machine you can use manage.py runserver_plus to have the very useful Werkzeug debugger active, so that the 500 error page lets you poke around with your stack interactively.
How do you activate the Werkzeug-enabled 500 page when running from Heroku?
If you are using gunicorn to serve your Django application on Heroku like on their tutorial https://devcenter.heroku.com/articles/django, you will not be able to see the Werkzeug debbuger because gunicorn does not use Werkzeug.
You have to serve you django application on Heroku using wsgi based server that uses Werkzeug like uWSGI. See -> https://github.com/unbit/uwsgi-docs/blob/master/tutorials/heroku_python.rst
Also DEBUG = True on your heroku settings must be set.
Having DEBUG = True on your production environment(heroku) is greatly discouraged because someone can view your settings(passwords) and even code.