-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.html
More file actions
366 lines (317 loc) · 17.5 KB
/
Copy pathindex.html
File metadata and controls
366 lines (317 loc) · 17.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Integrating JavaScript with Python Web Frameworks</title>
<meta name="description" content="For today's modern web applications it's inevitable that you're going to need some client side magic to give your application the responsiveness it needs. This talk covers some of the do's and don'ts of integrating JavaScript with various Python web frameworks like Django.">
<meta name="author" content="Jen Zajac">
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<link rel="stylesheet" href="css/reveal.min.css">
<link rel="stylesheet" href="custom/theme.css">
<!-- For syntax highlighting -->
<link rel="stylesheet" href="lib/css/zenburn.css">
<!-- If the query includes 'print-pdf', use the PDF print sheet -->
<!--<script>
document.write( '<link rel="stylesheet" href="css/print/' + ( window.location.search.match( /print-pdf/gi ) ? 'pdf' : 'paper' ) + '.css" type="text/css" media="print">' );
</script>-->
<link media="print" type="text/css" href="css/print/paper.css" rel="stylesheet">
<link media="print" type="text/css" href="custom/print.css" rel="stylesheet">
<!--[if lt IE 9]>
<script src="lib/js/html5shiv.js"></script>
<![endif]-->
</head>
<body>
<div class="reveal">
<div class="slides">
<section>
<h1 class="title">Integrating JavaScript with Python Web Frameworks</h1>
<h3><a href="http://www.littlegreenpixel.com">Jen Zajac</a>, @jenofdoom</h3>
<aside class="notes">
<ul>
<li>who am I?</li>
<li>frontend dev with some pretty rusty backend dev knowledge</li>
<li>have been building a giant django/angularjs web app for a gov ministry for the last year</li>
<li>so that's my particular area of expertise, am trying to keep this talk a bit more general though</li>
</ul>
</aside>
</section>
<section>
<h3>Why do we want to use JS?</h3>
<ul>
<li class="fragment">To offload some work to the client?</li>
<li class="fragment">To make the app or site more responsive to the user?</li>
</ul>
<aside class="notes">
<ul>
<li>making your site or app more responsive is the killer feature</li>
<li>ability to update smaller parts of the page</li>
<li>no need for a reload for e.g. sorting a list of serach results</li>
</ul>
</aside>
</section>
<section>
<h3>How can we use JS to augment our Python app or site?</h3>
<ul>
<li class="fragment">Light interactivity</li>
<ul class="fragment">
<li>e.g. as you type form validation on one page</li>
<li>just use jQuery</li>
</ul>
<li class="fragment">App-like behaviour</li>
<ul class="fragment">
<li>e.g. client side routes (HTML5 push state)</li>
<li>at this point we want a JS framework</li>
</ul>
</ul>
<aside class="notes">
<p>
JS frameworks are a good idea because they provide structure for your code and templating so you don't end up with lots of content strings and markup in your code.
</p>
</aside>
</section>
<section>
<h2>Okay, so I want to use a JS framework with a Python framework, what things do I need to think about?</h2>
</section>
<section>
<h3>Many JS libraries use the {{ somevar }} pattern in their templating</h3>
</section>
<section>
<h3>Many Python libraries <em>also</em> use the {{ somevar }} pattern in their templating</h3>
</section>
<section>
<h3>Some Python templating engines should be fine:</h3>
<ul>
<li>templetor (web.py)</li>
<li>mako (pyramid, bottle)</li>
<li>chameleon (pyramid)</li>
<li>cheetah (bottle)</li>
<li>genshi (cherrypy)</li>
</ul>
<aside class="notes">
<p>On the JS side, backbone should work fine with anything as its template delimiters are configurable and in fact don't use curly braces as default</p>
</aside>
</section>
<section>
<h3>Some, not so fine:</h3>
<ul>
<li>jinja2 (bottle, flask, many others)</li>
<li>SimpleTemplate Engine (bottle)</li>
<li>Django's templating (django)</li>
</ul>
</section>
<section>
<h3>Fortunately, there are some workarounds</h3>
<aside class="notes">
<p>Not for bottle's simpletemplate engine though - you'll have to use an alternative templating engine</p>
</aside>
</section>
<section>
<h3>jinja2</h3>
<p>Use <a href="http://jinja.pocoo.org/docs/templates/#escaping">{% raw %}</a> blocks</p>
<pre>
<code data-trim contenteditable>
{% raw %}
<p>{{ myVar }} is lovely.</p>
{% endraw %}
</code>
</pre>
</section>
<section>
<h3>Django 1.5+</h3>
<p>Use <a href="https://docs.djangoproject.com/en/dev/ref/templates/builtins/#std:templatetag-verbatim">{% verbatim %}</a> blocks</p>
<pre>
<code data-trim contenteditable>
{% verbatim %}
<p>{{ myVar }} is lovely.</p>
{% endverbatim %}
</code>
</pre>
</section>
<section>
<h3>Django 1.4 and below</h3>
<p>Not as straightforwards... use a custom template tag?</p>
<pre>
<code data-trim contenteditable>
@register.filter
def curly(value):
"""Wraps string in {{ }}"""
return '{{ ' + value + ' }}'
</code>
</pre>
<pre>
<code data-trim contenteditable>
{{ 'myVar'|curly }}
</code>
</pre>
<aside class="notes">
<p>This is pretty nasty...</p>
</aside>
</section>
<section>
<h3>Alternatively, sidestep the problem altogether:</h3>
<ul>
<li>Use partial templates that get included by the JS framework</li>
<li>Your Python code never sees them</li>
<li>Or, even more drastic: no Python templates at all</li>
</ul>
</section>
<section>
<h3>Partial JS templates</h3>
<ul>
<li>Keep them with your other static files (like CSS and JS files)</li>
<li>Most JS frameworks let you nest templates in quite a sophisticated manner</li>
<li>Watch out for CORS issues on older flavours of IE - you may need to ensure you serve static from same domain as your site</li>
</ul>
<aside class="notes">
<p>CORS = cross origin resource sharing. See http://caniuse.com/cors</p>
</aside>
</section>
<section>
<h3>Django + Angular partial template example</h3>
<pre>
<code data-trim class="smaller">
<form method="POST" action="{% url my_view %}">
{% csrf_token %}
<div
ng-include
src="staticURL + 'partials/partial.html'"
ng-init="initData={state: '{% myVar %}'}"
></div>
</form>
</code>
</pre>
</section>
<section>
<h3>Okay, so I'm now either serving Python templates that do very little, or I'm not serving them at all... what's supplying data to the JS framework?</h3>
</section>
<section>
<h2>RESTful API</h2>
<aside class="notes">
<ul>
<li>seperation of concerns</li>
<li>easy to cache aggressively</li>
<li>standardised pattern for third parties to interact with</li>
<li>many js frameworks have built in ability to talk to REST services</li>
</ul>
</aside>
</section>
<section>
<h3>Popular API libraries</h3>
<ul>
<li><a href="http://tastypieapi.org/">Tastypie</a> (Django)</li>
<li><a href="http://django-rest-framework.org/">Django REST framework</a> (Django...)</li>
<li><a href="https://bitbucket.org/jespern/django-piston/wiki/Home">Django-piston</a> (Django...)</li>
<li><a href="http://flask-restful.readthedocs.org/en/latest/">Flask-RESTful</a> (Flask)</li>
<li><a href="https://github.com/martinblech/mimerender">mimerender</a> (webapp2, web.py, Flask, Bottle)</li>
<li><a href="http://python-eve.org/">Eve</a> (Flask + MongoDB only)</li>
</ul>
<aside class="notes">
<p>Of the Django options, Django REST framework is possibly the sleekest. Tastypie can be a little curmudegonly but is quite powerful</p>
</aside>
</section>
<section>
<h3>Roll your own</h3>
<p>Some frameworks offer some api functionality out of the box:</p>
<ul>
<li><a href="http://web2py.com/books/default/chapter/29/10#HTML,-XML,-and-JSON">web2py</a></li>
<li><a href="http://docs.cherrypy.org/dev/progguide/REST.html">cherrypy</a></li>
</ul>
<p>Or write your own:</p>
<ul>
<li>build on top of light framework e.g. flash, bottle</li>
<li>or use Django's <a href="http://ccbv.co.uk/">class-based views</a></li>
</ul>
<aside class="notes">
<p>Of the Django options, Django REST framework is possibly the sleekest. Tastypie can be a little curmudegonly but is quite powerful</p>
</aside>
</section>
<section>
<h2>Some other things to consider</h2>
</section>
<section>
<h3>Watch out for “features” of API frameworks burning you</h3>
<aside class="notes">
<p>Check everything is off by default & turn on the things you need (XML and YAML are kinda insecure by default).</p>
</aside>
</section>
<section>
<h3>You need to make sure you are sending <a href="https://docs.djangoproject.com/en/1.5/ref/contrib/csrf/#ajax">CSRF protection tokens</a> along with any POSTs you are doing</h3>
<aside class="notes">
<p>Need to check that the POST originated from the right place.Django allows you to set a X-CSRFToken header. make sure you clean incoming data propely - if using django can use django forms for this, still.</p>
</aside>
</section>
<section>
<h3>Some JS frameworks have opinions about trailing slashes on URLs</h3>
<aside class="notes">
<p>Django has trailing slashes on by default, Angular always strips them. Have to config Django (and Tasyie) to not require them for Angular's RESTful service interation to work.</p>
</aside>
</section>
<section>
<h3>If you know have a bunch of application logic in your JS, that JS <strong>needs</strong> to be unit tested.</h3>
<aside class="notes">
<p>Look at <a href="http://pivotal.github.io/jasmine/">Jasmine</a> as a unit testing framework. This has an HTML based test runner which you can set up to be scraped by e.g. Selenium to integrate with the rest of your tests.</p>
</aside>
</section>
<section>
<h3>Keep your coding conventions nicely segregated - this will help you mentally flip between languages</h3>
<aside class="notes">
<p>E.g. use camelCase for js and underscores for Python. Use hinters and linters to maintain good code quality.</p>
</aside>
</section>
<section>
<h3>If you have small bits of data you want to expose to your JS code without necessitating an API call, use the data attribute on the HTML or body element.</h3>
<aside class="notes">
<p>E.g. use camelCase for js and underscores for Python. Use hinters and linters to maintain good code quality.</p>
</aside>
</section>
<section class="classes-example">
<h3>Using data attributes, example with Django template</h3>
<pre>
<code data-trim contenteditable class="smaller">
<html data-static="{% get_static_prefix %}">
</code>
</pre>
<p>Scrape this with some JS:</p>
<pre>
<code data-trim class="smaller">
var staticURL = document.documentElement.getAttribute('data-static');
</code>
</pre>
</section>
<section data-transition="linear" data-background="#6EA4AF" data-background-transition="slide">
<h2>Thanks for listening</h2>
</section>
</div>
</div>
<script src="lib/js/head.min.js"></script>
<script src="js/reveal.min.js"></script>
<script>
// Full list of configuration options available here:
// https://github.com/hakimel/reveal.js#configuration
Reveal.initialize({
controls: true,
progress: true,
history: true,
center: true,
transition: 'linear',
rollingLinks: false,
controls: false,
//theme: Reveal.getQueryHash().theme, // available themes are in /css/theme
//transition: Reveal.getQueryHash().transition || 'default', // default/cube/page/concave/zoom/linear/fade/none
// Optional libraries used to extend on reveal.js
dependencies: [
{ src: 'lib/js/classList.js', condition: function() { return !document.body.classList; } },
//{ src: 'plugin/markdown/marked.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
//{ src: 'plugin/markdown/markdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'plugin/highlight/highlight.js', async: true, callback: function() { hljs.initHighlightingOnLoad(); } },
//{ src: 'plugin/zoom-js/zoom.js', async: true, condition: function() { return !!document.body.classList; } },
{ src: 'plugin/notes/notes.js', async: true, condition: function() { return !!document.body.classList; } }
// { src: 'plugin/search/search.js', async: true, condition: function() { return !!document.body.classList; } }
// { src: 'plugin/remotes/remotes.js', async: true, condition: function() { return !!document.body.classList; } }
]
});
</script>
</body>
</html>