I have just started learning how to build small Web apps with Python and Flask but am having some issues with the workflow. The way I am currently developing is:
Make a change to the app (HTML, CSS, JS, Python Flask code etc)
Stop the server
Clear the browser cache to remove the old static assets
Start the server
Reload my app in the browser
This is becoming a complete chore. I have come from developing with Node/Express and React where I have been using nodemon (https://www.npmjs.com/package/nodemon) to monitor any application changes. The server restarts automatically as soon as you save your app, with any changes detected immediately being reflected in the browser.
Is there something like this for Flask?
From the answers given in the comments above, the solution is to run the Flask development server using the Flask CLI like so:
export FLASK_ENV=development
flask run
This runs the Flask reloader so changes to the Flask application and any HTML are updated when the page reloads.
To fix the problem of the browser caching css files which have to be cleared to see any changes, I referred to this Flask css not updating SO question and the comment by #Aylen.
Simply reload the Web page for your app using
Ctrl+shift+R
This reloads the page and clears the cache at the same time.
Related
I have just deployed my first flask app, however, I have found out that it is really easy to download all the code I spent so long writing. For example, if my app was app.com, users can simply go to the website /app.py and my app.py would get automatically downloaded. Same for the passenger_wsgi.py and every other folder short of a db.
How can I disable that without compromising the integrity of my flask app?
I'm developing a web application with flask for an acquaintance of mine, who will then host the website on a VPS. Since I don't want to give my source code as well, but only the service, is there a way to run it on the VPS without making the source code visible?
I am just about ready to deploy my Flask based website but before I do, I would like to know if the Flask framework is loaded for each session or if it is loaded just once on the server. The reason I ask is because I have a lot of python libraries to load and I want to know if I should load them all at once (if Flask is only loaded once) or load them on a page by page basis (if Flask is loaded for each session). It is really a question about getting the best performance for the end user.
Flask and all of your dependencies are loaded once when you start your web server, so no need to worry about startup time.
I am working in a dev environment using the built in Django web server. One of the inconvenience that I have is everytime I make changes in HTML or Static files it does not apply when I reload the browser until I kill the dev server and run again.
python manage.py runserver localhost:8000
Is there a way so Django will reflect the changes instantly? Thanks in advance
Django reload the server only on changes on .py files. I read different ways to trigger reloading (such as installing a third party app, tweaking caching depending on whether DEBUG = True, etc etc).
The easiest and dumbest way is to make an insignificant slight change in the view you're working on (say, adding a #, removing it) after you edited and saved your template. It is dumb but it works.
I wonder is there some optional configuration to the dev server to autorefresh page when files changed. I know that django dev server autoreload project when some changes appear but what i am looking for is refreshing the webpage like it is in for example meteor. I was googling a little and find some apps and plugins to ff and chrome.
Django is designed to web development so i suspect that such feature should be in the core of dev server. Is it?
No, dev server is just a simple server that accepts request, passes it to the django app and returns a response from the app. It is something different than you can find in some JavaScript libraries or frameworks, where data are held in browser and you only hot reload the source code and library regenerates the page using the same data.