Create multiple (two) source with chartit in Django - django

I'm using chartit in a django project.
I have a models (ReadingSensor) with the following attributes:
id_sensor
date_time
value
I want to create a line chart with several lines for different id_sensors
for example:
ReadingSensor.objects.filter(id_sensor=2)
ReadingSensor.objects.filter(id_sensor=1)
For a single model we have:
ds = DataPool(
series=
[{'options': {
'source': MonthlyWeatherByCity.objects.all()},
'terms': [
'month',
'houston_temp',
'boston_temp']}
])
cht = Chart(
datasource = ds,
series_options =
[{'options':{
'type': 'line',
'stacking': False},
'terms':{
'month': [
'boston_temp',
'houston_temp']
}}],
chart_options =
{'title': {
'text': 'Weather Data of Boston and Houston'},
'xAxis': {
'title': {
'text': 'Month number'}}})
Documentation: http://chartit.shutupandship.com/docs/
I consulted the documentation but found no suggestive example to help me.
Can someone help me?

Actually the example is on the site you provide, please check this link: http://chartit.shutupandship.com/demo/chart/multi-table-same-x/
The idea is just to add more items with options and terms to the series list when constructing DataPool object and adjust the terms in series_options when constructing Chart object accordingly.
Then you may find it's helpful to adjust the field name for the case when two data sources have the fields with the same name, the detailed document regarding to this issue is here: http://chartit.shutupandship.com/docs/apireference.html#datapool

Related

elastic-Search Aggregation [python]

I indexed the post and community models,
post = Index('posts')
post.settings(
number_of_shards=1,
number_of_replicas=0
)
#post.doc_type
class PostDocument(DocType):
community = fields.ObjectField(properties={
'id': fields.IntegerField(),
'description': fields.TextField(),
'name': fields.StringField(),
})
I want to search posts and aggregate the communities
(returns communities of the posts in the result)
I may need to use aggregation, I had difficulties while implementing it, the documentation was not clear for me.
q = Q("multi_match", query=query, fields=['title', 'content'])
document.query(q)
document.aggs.bucket('per_tag', 'terms', field='community')
I think you need change the aggregation to something similar to:
document.aggs.bucket('per_tag', 'terms', field='community__id')
Because community is a complex objects, and elasticsearch only can do aggregation with simple fields. (keyword or integer)

Marshaling items in lists using flask restful

I am writing a Flask-RESTFUL resource. It returns a Model object called DocumentSet with the following structure:
DocumentSet:
id: int
documents: list of Document
Document is another Model object with the following structure:
Document:
id: int
...other fields...
I want to write a #marshal_with decorator that returns the DocumentSet id along with a list of the Document ids like so:
{
id: 5,
document_ids: [1, 2, 3]
}
I've been banging my head against the output marshaling syntax to no avail. Some of the things I've tried:
{'id': fields.Integer, 'document_ids':fields.List(fields.Integer, attribute='documents.id')}
{'id': fields.Integer, 'document_ids':fields.List(fields.Nested({'id':fields.Integer}), attribute='documents')}
What's the magic incantation?
The magic incantation is
{'id': fields.Integer, 'name': fields.String, 'document-ids':{'id': fields.Integer}}
It was right there in the "Complex Structures" paragraph in the documentation.

Adding location to Querying the Graph API with Python

My query currently looks like the one below... I am trying to narrow my search to a specific location (West Midlands). How can I achieve that.
pp(g.request("search", {'q' : 'student mentors', 'type': 'page', 'location': 'West Midlands'}))
There is no location parameter for query type page. You can add "West Midlands" to the query parameter itself because Facebook searches the location/address, too. So you call would be
pp(g.request("search", {'q' : 'student mentors west midlands', 'type': 'page'}))
However that specific query does not return any results, which means there is no page that includes "student mentors" in it's name and "west midlands" in it's address.

Django ORM call to obtain multiple fk values?

models.py (derived from existing db)
class IceCreamComponent(models.Model):
name = models.CharField
#can be 'flavor', 'nut', etc
class IceCream(models.Model):
name = models.CharField
component = models.ForeignKey(IceCreamComponent)
value = models.CharField #will correspond to the component
The context behind this database is that 'IceCream' reports will come in from someone who's only purpose is to report back on a certain component (i.e. my 'extras' reporter will report the name of the ice cream and the extra it contained). It is assumed that all needed reports are in the db when queried so that something like:
IceCreams = IceCream.objects.values('name', 'component__name', 'value')
will return something akin to:
[
{'name': 'Rocky road', 'component__name': 'ice cream flavor', 'value':'chocolate'},
{'name': 'Rocky road', 'component__name': 'nut', 'value':'almond'},
{'name': 'Rocky road', 'component__name': 'extra', 'value':'marshmallow'},
{'name': 'Vanilla Bean', 'component__name': 'ice cream flavor', 'value':'vanilla'},
{'name': 'Vanilla Bean', 'component__name': 'extra', 'value':'ground vanilla bean'},
]
However, as you can imagine something like:
[
{'name': 'Rocky Road', 'ice cream flavor': 'chocolate', 'nut': 'almond', 'extra':'marshmallow' },
{'name': 'Vanilla Bean', 'ice cream flavor': 'vanilla', 'extra':'ground vanilla bean'}
]
is much more usable (especially considering I'd like to use this in a ListView).
Is there a better way to query the data or will I need to loop through the ValuesQuerySet to achieve the desired output?
Can't you reconstruct the list from the original result?
results = []
for row in vqueryset:
converted_row = {}
converted_row[row['component__name']] = row['value']
converted_row['name'] = row['name']
results.append(converted_row)
Of course you would want to paginate the original result before evaluating it (turning it into a list).
oh, you asked if there's a better way. I'm doing it this way because I couldn't find a better way anyway.
Here is the solution I came up with.
processing = None
output = []
base_dict = {}
for item in IceCreams:
# Detect change current site code from ordered list
if item['name'] != processing:
processing = item['name']
# If base_dict is not empty add it to our output (only first one)
# TODO see if there's a better way to do first one
if base_dict:
output.append(base_dict)
base_dict = {}
base_dict['name'] = item['name']
base_dict[item['component__name']] = item['value']

django-admin-tools 3 column layout not working

I'm using django-admin-tools 0.4.
Following the docs here so that I can get a 3 column layout.
I have managed to get the page to space correctly for 3 columns but the Modules can't move over to the 3rd column.
I can only drag from the left column to the middle but not right.
How can I get the modules to move between the 3 columns?
My dashboard.py can be viewed here.
I've attached a screenshot to show what result I have.
The main issue was that admin_tools_dashboard_preferences needs to be truncated(almost for every change being made to the dashboard.)
Also the first snippet of code on the documentation page didn't even work for me. I took snippets from other parts of the docs and they seemed to work no problems.
In the end my example dashboard looks something like this.
Remember to truncate your preferences.
class MyDashboard(Dashboard):
columns = 3
def __init__(self, **kwargs):
Dashboard.__init__(self, **kwargs)
# will only list the django.contrib.auth models
self.children += [
modules.ModelList('Authentication', ['django.contrib.auth.*',])
]
self.children.append(modules.Group(
title="My group",
display="tabs",
children=[
modules.AppList(
title='Administration',
models=('django.contrib.*',)
),
modules.AppList(
title='Applications',
exclude=('django.contrib.*',)
)
]
))
self.children.append(modules.LinkList(
layout='inline',
children=(
{
'title': 'Python website',
'url': 'http://www.python.org',
'external': True,
'description': 'Python programming language rocks !',
},
['Django website', 'http://www.djangoproject.com', True],
['Some internal link', '/some/internal/link/'],
)
))