Share design data between jinja2 and js - flask

I'm developing an app in flask. I have to tables, one renders on the server side using jinja2 and the other one is a live table that renders dynamically using socketio. They are in different routes but the tables look the same in design.
My problem relies on the rendering, I iterate through the same database in both cases but in the dynamic part i get the json and render it with mustache and the static table do the same but with jinja2. I need to store data related with states and categories that i obtain from the database for every row and use it for rendering in both routes.
basically I want to know where to store this relationship:
{category_id:{icon:x, color:y, name:z}}
I'm almost sure that whatever solution I get I would eventually need this as a jquery object (my current solution but a single change in that data means change multiple places on different templates) so i can then access on rendering to get the dynamic data, but... that doesn't mean i now how to get there nor how to share the same data structure between flask jinja and js. Thanks in advance.

If I understand your question correctly, you can use Flask sessions to store data between views.
from flask import session
Then you can set it:
session['config'] = {category_id:{icon:x, color:y, name:z}}
And get it, and pass it to your view:
default_config = {category_id:{icon:x, color:y, name:z}}
config = session.get('config', default_config)

Related

Automatically populate Django model database tables with unreal data

I'm working on a template inside one of my apps and I need to have a lot of records in a table to see how it looks like (and several other behaviors) inside the template when queried. I don't want to waste my time inserting over 30 records one by one. I'm trying to do a bulk insert but I have no previously dumped data or such to populate using it.
The correctness of data is not important to me. the quantity is important.
Does that have anything to do with mocking?
I'm not trying to unit test anything.
Thanks,
Which type of data do you want...??
i mean.. blog posts.. or any other things...try use Faker for django which provieds fake data.
Faker - Documentation
Faker - github

How to store nested lists as Django model field (or should I keep data as flat files)?

I'm working on a basic django web app (as a novice) that amalgamates several sources into nested lists representing flat 2d tables of data. This are then updated/displayed/referenced accordingly.
My current setup, which works ok, is for these data files to exist as very small csv files which are read/written as needed. They are also picked up using d3.js which has been easier given the d3 templates tend to import from a static data file.
It feels incorrect to not be querying the database directly for all of this data - is there an issue with using flat csv files?
If I should be querying the database, how best to store/access nested list data within Django? I can't find much but have seen some vague references to Serializers?
Since you read/write you should store data in your database. You can create a model which corresponds to your csv. A nested list can be stored as a simple string (or alternatively, if you need to query the list directly you can create another model which will hold the values - but that's rather inefficient). As for importing/outputting csv with models, you can do that with a script of your own or use a library like django-adaptors. Oh, and as far as I am aware of D3.js accepts JSON which is more flexible and better supported in Django.

Display a webpage while rendering

I'm putting together a website, where one of the pages holds an interactive map. The map is implemented as a big table, where each node is a td.
Now this map takes a while to render, and so, I'd love for the site to be displayed as it renders, so that even if the map is not fully rendered, the user can click links or the part of the map that is rendered.
Is there an easy way to do this? AJAX is one option, but since it is a Django website and the map depends on data from the Django template, AJAX becomes a bit unwieldy.
So is there a way to make the page visible while rendering?
(I considered making each node an iframe, so that they would be rendered individually, but that seems a bit silly too)
the django template should only render an empty map (or a map holding the first 10 points) with the javascript code firing on page ready
this javascript script should do this:
request 10 nodes from django (using a different url/view)
render the fetched nodes into the page
if no more nodes: END
goto 1.
Hope this helps
After trying a few different things, it seems that the problem was too many database queries. Each of the nodes made calls to the database while rendering, which caused them to be very slow.
For reference:
Custom filters in Django should not make database queries, if they are used heavily on a page

Django - Static content display based on URL

I'm working on a Django site with a basic three column design. Left column navigation, center column content and right column URL specific content blocks.
My question is about the best method of controlling the URL specific content blocks in the right column.
I am thinking of something along the lines of the Flatpages app that will make the content available to the template context if the URL matches a pre-determined pattern (perhaps regex?).
Does anyone know if such an app already exists?
If not, I am looking for some advice about the best way to implement it. Particularly in relation to the matching of patterns to the current URL. Is there any good way to re-use parts of the Django URL dispatcher for this use?
Django CMS is a good suggestion, it depends on how deep you want to go. If this is just the beginning of different sorts of dynamic content you want then you should go that way for sure.
A simple one-off solution would be something like this:
You would just need to write a view and add some variables on the end of the URL that would define what showed up there. Depending on how fancy you need to get, you could just create a simple models, and just map the view to the model key
www.example.com/content/sidecontent/jokes/
so if "jokes" was your block of variable sidecontent (one of many in your sides model instances) the urls.py entry for that would be
(r'^content/sidecontent/(?P<side>)/$,sides.views.showsides),
and then in your sides app you have a view with a
def showsides(request, side):
Sides.objects.get(pk=side)
etc...
For something like this I personally would use Django CMS. It's like flatpages on steroids.
Django CMS has a concept of Pages, Templates, and Plugins. Each page has an associated template. Templates have placeholders where you can insert different plugins. Plugins are like mini-applications that can have dynamic model-based content.
Although Django-CMS is an interesting suggestion, there are quite a few projects that do specifically what you've requested - render blocks of content based on a URL. The main one that I know about is django-flatblocks.

How do you pass or share variables between django views?

I'm kind of lost as to how to do this:
I have some chained select boxes, with one select box per view. Each choice should be saved so that a query is built up. At the end, the query should be run.
But how do you share state in django? I can pass from view to template, but not template to view and not view to view. Or I'm truly not sure how to do this. Please help!
Put the values to hold into the session.
There are many ways... in the view to template... put the variables in hidden fields in the forms. So when you "submit" in the subsequent forms... the values are then contained in the following request.POST.get().
Of course you can also store the various data elements in a DB table (disk or ram) between views... using the session_id as the key into the datastore. (not recommended for load balanced systems).
And my least favorite is cookies. (see the APIs for how to store in cookies)
UPDATE: Sorry there are no code examples here... the docs are pretty easy to read. There is also a djangosnippets website that you use to look up example code.
You can store such information in session as Ignacio Vazquez-Abrams said or use django-flash - (django-flash usage)