Gunicorn reload: don't reload everything - flask

I'm building a webapp using gunicorn, flask, and plotly's dash. I'm using guncorns's --reload option which automatically reloads or resets the workers if any code is modified. I have observed this basically restarts my entire web app. At the start of my webapp I'm initializing a client connection and cursor to documents inside a mongo db. Then the webapp starts graphing stuff. If I modified the HTML of the webapp, I want gunicorn to reload the HTML side of things only, and not reinitialize the mongo db each time. Is there any way I can avoid reloading everything using gunicorn's reload? Or maybe some other alternative?

Gunicorn only reloads the Python code. It will not reload your HTML code.
Your HTML code should be read each time a request is made, unless it is using cached version.
Try disabling cache on the page which you are trying to re-load.
These links should point you towards a solution:
https://pythonhosted.org/Flask-Caching/
https://gist.github.com/arusahni/9434953
Disable cache on a specific page using Flask

Related

The django-geoposition map widget is not showing up in the django admin on my production server, but it is working perfectly fine on my test server

The django geoposition widget shows up in admin when I am testing everything on my laptop with the django test server. However, when I put everything onto my production server, there is no map showing up and no labels showing up for the latitude and longitude. Both setups are using django == 2.1.7 and geoposition == 0.3.0. The production server is being served with nginx and gunicorn.
I opened the browser inspection window and it turned out that the geoposition.js is being served by django before jQuery is being called. So I tried calling jQuery directly in the geoposition.js file, but there is still no widget. I then revert everything back to it's original stage and I am now including js = (setting.STATIC_URL + 'geoposition/geoposition.js") in a media class in admin.py. This is causing two maps to appear in my test server, but still no map in my production server. I added an if statement so that it doesn't run if jQuery is not loaded, so no jQuery errors on the first geoposition.js being served. I added some console.log() statements to the file so I can see where it is at and it is getting into the main function fine, but not getting into the $('.geopostion-widget').each(function() ...
When I look at the html, the map and latitude and longitude values are wrapped in a div of class = "geoposition-widget", but that there is no such div from my production server which makes sense as to why the '.geopostion-widget' function is not doing anything. It seems to be putting in that div automatically on my test server, which it is supposed to do, but not on my production server.
Any clue as to why this is happening? Do I just need to create a custom form for the admin on my production server and wrap the field in a div with class = '.geopostion-widget'? This just seems kind of janky since it is working fine on my test server. Or does it have something to do with the way it's being served with gunicorn and nginx?
Found the answer on github here - https://github.com/philippbosch/django-geoposition/issues/83.
imdario's answer at the bottom solved it. Upgraded geoposition package to imdario's geoposition pakcage with 'pip install django-geoposition-2' and voila - it works.

Serve multiple Django applications from one server

Good morning. I have a dedicated ubuntu server behind my company's firewall. It is using Django, Gunicorn, and Nginx to serve an Intranet application to employees. The original app responds to the URL [server_name]/[original_application_name]. I want to serve additional apps from this server. I have followed this tutorial as I did when setting up the original app.
I can run Gunicorn and serve the app, I have created a second systemd service file that appears steady (copied from the original app with paths changed - service runs), same for a new 'sites-available' file in Nginx (copied from original and modified), new .sock file exists, binding appears successful. However, I have yet to hit on the right configuration combination between settings.py [allowed_hosts], [new_app].service, and nginx etc.
The original app is running and when I try a URL related to the new app it gives an error saying it cannot find the request in the url.py of the original app. The new app would be used by the IT dept. Ideally, the new URL would be something like: it.[server_name]/[new_application_name].
I have looked through other cases with this problem but most use Apache or are on a public hosting site. I have seen this but it requires a "socket file". My original app is not using a socket file. I was hoping to do this without interfering with the original app. Is a "socket file" required? How can I configure this to serve both apps? Never having done this, what will the new URL be? The URL for the admin site in both apps is 'admin/', how can I accommodate this? Thanks!
I combined into one file as you suggested and I am almost there! I have original site responding at [server_name]/inventory and new site responding at [server_name]/assets. Great! My only problem is the admin page. In both apps the admin site is called admin! So, [server_name]/admin brings up the original site. How can I get to the new admin page?

django flush query caches

I have 2 instances of django application.
One is frontend - a normal wsgi app.
Another is backend - a twisted daemon running with ./manage.py rundaemon.
They share django settigns and models.
Now, when one of them has a query, it got cached.
And when another updates database - the cache will not be flushed.
That's obviously because they have no clue about another instance accessing the same database.
Is there a way to disable caching, or flush it manually and force query to be reexecuted?
(I guess the admin app does flush query caches somehow)
I'm not sure if this is the best solution, but it worked for me when I faced the same problem.
import django
django.db.connection.close()
The connection will automatically get reopened the next time it is needed.

Django HTSQL Web Interface for writing queries is not responding

I am trying to use HTSQL for one of my Django projects. For that I followed the procedure given HERE for furnishing HTSQL/Django requirements. Then I cloned the HTSQL repository for trying the example/demo in it from HERE. I am testing this on Django v1.4. The default db used in the demo example is sqlite3. In Django python shell, the queries are working fine now as per THIS question. But as demonstrated on HTSQL Website, it has a very powerful frontend to communicate with the database and also generates efficient queries (reference). I am trying to use this particular feature for my Django application which is also demonstrated in the demo/example django app from HTSQL. In the demo app, when I started my local django server and tried to access the following url:
localhost:8000/htsql/
The page loads and when I write the following lines:
/polls_poll
to see the data from the polls_poll table, the RUN button does nothing and so does the more drop down menu. No error, no response, no data fetched from the polls_poll table. Then I noticed that the page wasn't loading properly i.e this trace was generated on the Django server terminal. So basically,
codemirror .js and .css files were throwing HTTP 500 error. For that I searched for the links of the codemirror .css and .js files and provided those links in the index.html of the HTSQL resided in the static folder. Following is its path:
>>> /usr/local/lib/python2.7/dist-packages/htsql/tweak/shell/static
Now the terminal trace has changed to THIS
But still the RUN button does nothing and no data is fetched from the table polls_poll
Am I doing something wrong or missing something??
CodeMirror just changed the download URL for their packages, which broke HTSQL shell. You either need to apply the following patch manually:
https://bitbucket.org/prometheus/htsql/changeset/f551f8996610bb68f2f8530fc6c0dbf6b5c34d90
or you can wait for the next bugfix release of HTSQL, which will be out in a day or two.

Why Django blocks simultaneous requests within the same session?

I tried to add sleep(30) at the first line of my view. After that I opened this page in two browser tabs. The first tab loaded the page after 30 seconds, and the second one loaded it in 60 seconds. In the meantime I was able to open pages from another pc just fine. So it looks like Django blocks the concurrent requests from the same client.
This is very well for my app. And I'd like to be sure my site will work this way in the future. However I have not found any documentation or articles describing such Django behaviour. So I'm still not sure if this is a feature or just fortune. Could somebody please explain how and why this works?
What I actually need is to block the session while view is processing. Of course I can use some flags or db transactions. But I'd not like to add a feature that is already implemented in Django.
I use python 2.6.5, django 1.4, ubuntu server, nginx and uwsgi. Tried both postgresql and sqlite.
My uwsgi settings:
<uwsgi>
<pythonpath>/home/admin/app/src</pythonpath>
<app mountpoint="/">
<script>deploy.wsgi</script>
</app>
<workers>4</workers><!-- Not sure this is needed -->
<processes>2</processes>
</uwsgi>
I also got same effect with runserver command.
Actually Django does not block simultaneous requests.
If I run two browsers (for example chrome and firefox) with the same session (by copying the sessionid cookie from the first browser to the second one), blocking does not happen. So, this is a browser feature, and it's not related to Django anyhow. This means I still need to add some blocking feature by myself to make the code safe.