-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
481 lines (392 loc) · 15.3 KB
/
index.html
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Pyramid - A very general open source Python web framework</title>
<meta name="description" content="Pyramid - A very general open source Python web framework">
<meta name="author" content="ouvigna">
<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, minimal-ui">
<link rel="stylesheet" href="css/reveal.css">
<link rel="stylesheet" href="css/theme/black.css" id="theme">
<!-- Code syntax highlighting -->
<link rel="stylesheet" href="lib/css/zenburn.css">
<!-- Printing and PDF exports -->
<script>
var link = document.createElement( 'link' );
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = window.location.search.match( /print-pdf/gi ) ? 'css/print/pdf.css' : 'css/print/paper.css';
document.getElementsByTagName( 'head' )[0].appendChild( link );
</script>
<!--[if lt IE 9]>
<script src="lib/js/html5shiv.js"></script>
<![endif]-->
</head>
<body>
<div class="reveal">
<!-- Any section element inside of this container is displayed as a slide -->
<div class="slides">
<section>
<h1>Pyramid</h1>
<h3>A very general open source Python web framework</h3>
<p>
<small>By César Pérez for Python Vigo</small>
</p>
</section>
<section>
<section>
<p>Your first Pyramid app</p>
<pre><code>
from wsgiref.simple_server import make_server
from pyramid.config import Configurator
from pyramid.response import Response
def hello_world(request):
return Response('Hello %(name)s!' % request.matchdict)
if __name__ == '__main__':
config = Configurator()
config.add_route('hello', '/hello/{name}')
config.add_view(hello_world, route_name='hello')
app = config.make_wsgi_app()
server = make_server('0.0.0.0', 8080, app)
server.serve_forever()
</code></pre>
</section>
<section>
<p>How do i run it?</p>
<pre><code>
$ virtualenv firstapp
[...]
$ . firstapp/bin/activate
(firstapp)$ pip install pyramid
[...]
(firstapp)$ python helloworld.py
</code></pre>
</section>
<section>
<img src="images/hello_python_vigo.png">
</section>
</section>
<section>
<h2>Come on... Pyramid should help me more</h2>
</section>
<section>
<section>
<h2>Scaffolding!!!</h2>
</section>
<section>
<h3>Create a project...</h3>
<pre><code style="font-size: 18px;">
(myvirtualenv)$ pcreate -l
Available scaffolds:
alchemy: Pyramid SQLAlchemy project using url dispatch
starter: Pyramid starter project
zodb: Pyramid ZODB project using traversal
(myvirtualenv)$ pcreate -s starter projectname
[...]
</code></pre>
</section>
<section>
<h3>...install dependencies...</h3>
<pre><code style="font-size: 18px;">
(myvirtualenv)$ cd projectname
(myvirtualenv)$ python setup.py develop
[...]
</code></pre>
</section>
<section>
<h3>...and serve it</h3>
<pre><code style="font-size: 18px;">
(myvirtualenv)$ pserve development.ini
Starting server in PID 32670.
serving on http://0.0.0.0:6543
</code></pre>
</section>
</section>
<section>
<h2>Configuration</h2>
<p style="font-family: monospace;">$ projectname/__init__.py</p>
<pre><code style="font-size: 18px;">
from pyramid.config import Configurator
def main(global_config, **settings):
""" This function returns a Pyramid WSGI application.
"""
config = Configurator(settings=settings)
config.include('pyramid_chameleon')
config.add_static_view('static', 'static', cache_max_age=3600)
config.add_route('home', '/')
config.scan()
return config.make_wsgi_app()
</code></pre>
</section>
<section>
<section>
<h2>Routes</h2>
<p>In Pyramid, a route is composed by:</p>
<ul>
<li>URL</li>
<li>Route name</li>
<li>Method (or a view)</li>
</ul>
<pre><code class="python">
# '/' is the "URL"
# 'home' is the "route name"
config.add_route('home', '/')
# home_view is the method/view
config.add_view(home_view, route_name='home')
</code></pre>
</section>
<section>
<h3>Why to use Route names?</h3>
<p>A route name is clearer than a url. We can also identify routes by a name</p>
<pre><code class="python">
config.add_route('home', '/')
config.add_route('user_show', '/users/{id}')
</code></pre>
<pre><code class="python">
# This is the Pyramid shell
>>> request.route_url('home')
'http://localhost/'
>>> request.route_url('user_show', id=1)
'http://localhost/users/1'
</code></pre>
</section>
<section>
<h3>Some examples</h3>
<pre><code class="python">
# Simplest route
config.add_route('home', '/')
# Route with parameters
config.add_route('user_show', '/users/{id}')
# Route with parameters 2
config.add_route('user_courses', '/users/{id}/courses')
# Expecific method
config.add_route('user_new', '/users', request_method='POST')
</code></pre>
</section>
</section>
<section>
<section>
<h2>Views</h2>
<p style="font-family: monospace;">$ projectname/views.py</p>
<pre><code>
from pyramid.view import view_config
@view_config(route_name='home', renderer='templates/mytemplate.pt')
def my_view(request):
return {'project': 'projectname'}
</code></pre>
</section>
<section>
<h3>Adding views</h3>
<pre><code style="font-size: 18px;">
def hello_world(request):
return Response("hello world")
if __name__ == '__main__':
# ...
config.add_route('hello', '/helloworld')
config.add_view(hello_world, route_name='hello')
# ...
</code></pre>
<pre><code style="font-size: 18px;">
@view_config(route_name='hello')
def hello_world(request):
return Response("hello world")
if __name__ == '__main__':
# ...
config.add_route('hello', '/helloworld')
config.scan()
# ...
</code></pre>
</section>
<section>
<h3>Creating views</h3>
<p>A view can be any callable</p>
</section>
<section>
<h4>A method</h4>
<pre><code style="font-size: 18px;">
@view_config(route_name='cors_route')
def cors_view(request):
request.response.headerlist.append(
('Access-Control-Allow-Headers', 'Content-Type')
)
return request.response
</code></pre>
</section>
<section>
<h4>A class</h4>
<pre><code style="font-size: 18px;">
class Users:
def __init__(self, request):
self._request = request
@view_config(route_name='users_list')
def list(self):
pass
@view_config(route_name='users_add')
def new(self):
pass
# ...
</code></pre>
</section>
</section>
<section>
<section>
<h2>Renderers</h2>
<pre><code>
from pyramid.view import view_config
@view_config(route_name='home', renderer='json')
def my_view(request):
return {'project': 'projectname'}
</code></pre>
</section>
<section>
<h3>JSON</h3>
<pre><code>
@view_config(route_name='json', renderer='json')
def view_json(request):
return {'project': 'testrenderers'}
</code></pre>
<pre><code class="http">
GET /json HTTP/1.1
Host: localhost:6543
Content-Type: application/json; charset=utf-8
Content-Length: 28
{"project": "testrenderers"}
</code></pre>
</section>
<section>
<h3>TEMPLATES (CHAMELEON)</h3>
<pre><code style="font-size: 18px;">
# views.py
@view_config(route_name='chameleon', renderer='templates/chameleon.pt')
def view_chameleon(request):
return {'project': 'testrenderers'}
</code></pre>
<pre><code style="font-size: 18px;">
<!-- templates/chameleon.pt -->
<html lang="${request.locale_name}">
<head></head>
<body>Project: ${project}</body>
</html>
</code></pre>
<pre><code style="font-size: 18px" class="http">
GET /chameleon HTTP/1.1
Host: localhost:6543
Content-Type: text/html; charset=utf-8
Content-Length: 94
<html lang="en"><head></head><body>Project: testrenderers</body></html>
</code></pre>
</section>
</section>
<section>
<section>
<h2>Models</h2>
<p>Model management by <a target="_blank" href="">SQLAlchemy</a></p>
<pre><code>
$ pcreate -s alchemy modelsproject
</code></pre>
</section>
<section>
<h2>How does a model look?</h2>
<pre><code class="python">
class MyModel(Base):
__tablename__ = 'models'
id = Column(Integer, primary_key=True)
name = Column(Text)
value = Column(Integer)
</code></pre>
</section>
<section>
<h2>Transactions</h2>
<p>Any action that modifies database, has to be controlled by a transaction</p>
<pre><code class="python">
with transaction.manager:
user = User(**self._request.POST)
DBSession.add(user)
</code></pre>
</section>
</section>
<section>
<h2>Structure</h2>
<h4>How do I become tidy with Pyramid?</h4>
<p>Pyramid allows you to keep your own structure, so, try to be tidy by yourself<p>
</section>
<section>
<h2>Custom scaffolding</h2>
<p><a target="_blank" href="http://docs.pylonsproject.org/projects/pyramid/en/latest/narr/scaffolding.html">Creating Pyramid Scaffolds</a></p>
<p>EX.: <a target="_blank" href="https://github.com/ouvigna/restjson_scaffold" style="font-family: monospace">restjson_scaffold</a></p>
</section>
<section>
<h2>Extras</h2>
<ul>
<li><a href="http://docs.pylonsproject.org/projects/pyramid/en/latest/narr/traversal.html" target="_blank">Traversal</a>: Map URL to code</li>
<li><a href="https://alembic.readthedocs.org/en/latest/" target="_blank">Alembic</a>: A database migration tool</li>
<li><a href="https://gevent-socketio.readthedocs.org/en/latest/">Gevent-socketio</a>: Websocket abstraction</li>
</ul>
</section>
<section>
<h3>Pyramid has to have something bad</h3>
</section>
<section>
<h3>The most complete guide: Stackoverflow</h3>
<h4>Questions tagged</h4>
<table>
<tbody>
<tr>
<td><a href="http://stackoverflow.com/questions/tagged/django" target="_blank">Django</a></td>
<td>97,074</td>
</tr>
<tr>
<td><a href="http://stackoverflow.com/questions/tagged/flask" target="_blank">Flask</a></td>
<td>8,087</td>
</tr>
<tr>
<td><a href="http://stackoverflow.com/questions/tagged/pyramid" target="_blank">Pyramid</a></td>
<td>1,541</td>
</tr>
</tbody>
</table>
</section>
<section>
<h2>Links</h2>
<ul>
<li><a href="http://docs.pylonsproject.org/projects/pyramid/en/latest/index.html" target="_blank">The Pyramid Web Framework</a></li>
<li><a href="https://www.airpair.com/python/posts/django-flask-pyramid">Django vs Flask vs Pyramid: Choosing a Python Web Framework</a></li>
</ul>
</section>
<section style="text-align: left;">
<h1>THE END</h1>
<p>
- <a target="_blank" href="https://github.com/ouvigna/python-vigo-pyramid">https://github.com/ouvigna/python-vigo-pyramid</a> <br>
</p>
</section>
<section>
<h1>Questions?</h1>
</section>
</div>
</div>
<script src="lib/js/head.min.js"></script>
<script src="js/reveal.js"></script>
<script>
// Full list of configuration options available at:
// https://github.com/hakimel/reveal.js#configuration
Reveal.initialize({
controls: true,
progress: true,
history: true,
center: true,
transition: 'slide', // none/fade/slide/convex/concave/zoom
// Optional reveal.js plugins
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, condition: function() { return !!document.querySelector( 'pre code' ); }, callback: function() { hljs.initHighlightingOnLoad(); } },
{ src: 'plugin/zoom-js/zoom.js', async: true },
{ src: 'plugin/notes/notes.js', async: true }
]
});
</script>
</body>
</html>