We'll be working with the data from my band's website, which uses the Django admin as a basic CMS. The source is on GitHub.
Requirements:
$ brew install graphviz
(venv)$ pip install pydot graphviz
Let's use some of IPython's magic to find out.
Thanks to manage.py shell_plus
, all of our models have already been imported.
View the source code for a model:
In [1]:
Gig??
View the whole file:
In [2]:
from inspect import getfile
In [3]:
gig_file = getfile(Gig)
gig_file
Out[3]:
In [4]:
%pycat $gig_file
View the contents of the app directory:
In [5]:
from os import path
In [6]:
!ls -l {path.dirname(gig_file)}
View the output of the graph_models
command from Django Extensions:
In [7]:
from graphviz import Source
from IPython.display import Image
In [8]:
!manage.py graph_models music news shows -o models.png 2>/dev/null
Image('models.png')
Out[8]:
Alternatively, capture the output, and render it as SVG:
In [9]:
dot = !manage.py graph_models shows 2>/dev/null
Source(dot.n)
Out[9]:
Learn more about IPython's magic functions:
In [10]:
%quickref
In [11]:
gigs = Gig.objects.published().past()
gigs.count()
Out[11]:
In [12]:
[gig for gig in gigs.filter(date__year='2016')]
Out[12]:
In [13]:
for date in gigs.dates('date', 'year'):
gig_count = gigs.filter(date__year=date.year).count()
print('{}: {}'.format(date.year, gig_count))
In [14]:
gigs.values('venue').distinct().aggregate(count=Count('*'))
Out[14]:
Render a Django template in the notebook:
In [15]:
from django.template import Context, Template
from IPython.display import HTML
In [16]:
top_venues = (
gigs.values('venue__name', 'venue__city')
.annotate(gig__count=Count('*'))
.order_by('-gig__count')
[:10]
)
template = Template("""
<table>
<tr>
<th>Venue</th>
<th>City</th>
<th>Gigs</th>
</tr>
{% for v in venues %}
<tr>
<td>{{v.venue__name}}</td>
<td>{{v.venue__city}}</td>
<td>{{v.gig__count}}</td>
</tr>
{% endfor %}
</table>
""")
context = Context(
{'venues': top_venues}
)
HTML(template.render(context))
Out[16]:
In [ ]: