Djangocon Eu 2019
Chris Adams
Successful, prolific, well known django developer
Environmentally focussed web generalist
Β
class CoolRedCar(models.Model):
# please don't do this in real life
model = models.CharField(max_length=254)
top_speed = models.FloatField()
def __str__(self):
# should be __repr__()
# if you just want the emoji
return "π"
./manage.py shell
In [1]: cart = ShoppingCart.objects.get(
user="chris"
)
Out[1]: π
In [2]: [item for item in cart.items()]
Out[2]: [π, π, π§, πΎ, π₯]
pip install viz-knowledge
Author of Visualization Analysis and Design
@tamaramunzner
(stuff on a screen or page)
(info we encode in the marks)
(so we can get comfortable with these ideas)
mark:
bar
Β
channel:
x position
length
mark:
point
Β
channel:
x-position
y-position
mark:
point
Β
channel:
x position
y position
colour
mark:
point
Β
channel:
x position
y position
colour
size
import handylibrary as lib
# fetch our data
url = "https://domain.com/data/seattle-weather.csv"
# make our chart using our "marks and channels" grammar
lib.Chart(url).mark_bar().encode(
alt.Y(
'precipitation',
type="quantitative"
),
alt.X(
'date',
type="ordinal",
timeUnit="month"
)
)
Altair
Vega-lite
Vega
D3
SVG / Canvas
π
π
π
π¬
π°
We get to stay up here
(But can get help here if we want it)
ORM
Altair
Browser
π
fetch data our django app
render vega-lite in browser, with template tag or similar
import altair as alt
from django.http import JsonResponse
from django.shortcuts import render
from .models import MyModel
def my_cool_chart_view(req):
query = MyModel.objects.all().values()
data = alt.Data(values=list(query))
chart_obj = alt.Chart(data).mark_bar().encode(
# encode your data using the channels
# and marks grammar we covered before
# you want here
)
return JsonResponse(chart_obj)
def index_page(req):
render(req, "index_page.html")
from django.urls import include, path
from . import views
urlpatterns = [
path('cool_chart/',
views.my_cool_chart_view,
name='my-cool-chart'
),
path('index/',
views.viz_index_page,
name='index-page'
),
]
<!DOCTYPE html>
<html>
<head>
<!-- Load Vega, Vega-Lite & Vega Embed -->
<script src="{% static 'src/assets/vega.min.js' %}"></script>
<script src="{% static 'src/assets/vega-lite.min.js' %}"></script>
<script src="{% static 'src/assets/vega-embed.min.js' %}"></script>
</head>
<body>
<div id="viz">
<!-- viz will replace content here -->
</div>
<script type="text/javascript">
var spec = "{% url 'my-cool-chart' %}";
// transform the vega spec from altair as JSON,
// and render our cool viz on the page
vegaEmbed('#viz', spec)
</script>
</body>
</html>
Β
Β