commit | author | age
|
b731b5
|
1 |
.. _qtut_hello_world: |
PE |
2 |
|
b1b922
|
3 |
================================ |
PE |
4 |
01: Single-File Web Applications |
|
5 |
================================ |
|
6 |
|
878d1a
|
7 |
What's the simplest way to get started in Pyramid? A single-file module. No |
SP |
8 |
Python packages, no ``pip install -e .``, no other machinery. |
|
9 |
|
b1b922
|
10 |
|
PE |
11 |
Background |
|
12 |
========== |
|
13 |
|
99d45a
|
14 |
Microframeworks were all the rage, until the next shiny thing came along. "Microframework" is a marketing |
878d1a
|
15 |
term, not a technical one. They have a low mental overhead: they do so little, |
SP |
16 |
the only things you have to worry about are *your things*. |
b1b922
|
17 |
|
878d1a
|
18 |
Pyramid is special because it can act as a single-file module microframework. |
SP |
19 |
You can have a single Python file that can be executed directly by Python. But |
|
20 |
Pyramid also provides facilities to scale to the largest of applications. |
b1b922
|
21 |
|
878d1a
|
22 |
Python has a standard called :term:`WSGI` that defines how Python web |
SP |
23 |
applications plug into standard servers, getting passed incoming requests, and |
|
24 |
returning responses. Most modern Python web frameworks obey an "MVC" |
|
25 |
(model-view-controller) application pattern, where the data in the model has a |
|
26 |
view that mediates interaction with outside systems. |
b1b922
|
27 |
|
878d1a
|
28 |
In this step we'll see a brief glimpse of WSGI servers, WSGI applications, |
SP |
29 |
requests, responses, and views. |
|
30 |
|
b1b922
|
31 |
|
PE |
32 |
Objectives |
|
33 |
========== |
|
34 |
|
878d1a
|
35 |
- Get a running Pyramid web application, as simply as possible. |
b1b922
|
36 |
|
878d1a
|
37 |
- Use that as a well-understood base for adding each unit of complexity. |
b1b922
|
38 |
|
878d1a
|
39 |
- Initial exposure to WSGI apps, requests, views, and responses. |
SP |
40 |
|
b1b922
|
41 |
|
PE |
42 |
Steps |
|
43 |
===== |
|
44 |
|
187104
|
45 |
#. Make sure you have followed the steps in :doc:`requirements`. |
b1b922
|
46 |
|
34e974
|
47 |
#. Starting from your workspace directory |
PE |
48 |
(``~/projects/quick_tutorial``), create a directory for this step: |
b1b922
|
49 |
|
PE |
50 |
.. code-block:: bash |
|
51 |
|
99d45a
|
52 |
$ cd ~/projects/quick_tutorial; mkdir hello_world; cd hello_world |
b1b922
|
53 |
|
PE |
54 |
#. Copy the following into ``hello_world/app.py``: |
|
55 |
|
|
56 |
.. literalinclude:: hello_world/app.py |
|
57 |
:linenos: |
|
58 |
|
|
59 |
#. Run the application: |
|
60 |
|
|
61 |
.. code-block:: bash |
|
62 |
|
187104
|
63 |
$ $VENV/bin/python app.py |
b1b922
|
64 |
|
d749bf
|
65 |
#. Open http://localhost:6543/ in your browser. |
b1b922
|
66 |
|
878d1a
|
67 |
|
b1b922
|
68 |
Analysis |
PE |
69 |
======== |
|
70 |
|
878d1a
|
71 |
New to Python web programming? If so, some lines in the module merit |
b1b922
|
72 |
explanation: |
PE |
73 |
|
878d1a
|
74 |
#. *Line 11*. The ``if __name__ == '__main__':`` is Python's way of saying, |
SP |
75 |
"Start here when running from the command line", rather than when this |
|
76 |
module is imported. |
b1b922
|
77 |
|
d91e5d
|
78 |
#. *Lines 12-14*. Use Pyramid's :term:`configurator` in a :term:`context manager` to connect :term:`view` |
878d1a
|
79 |
code to a particular URL :term:`route`. |
b1b922
|
80 |
|
878d1a
|
81 |
#. *Lines 6-8*. Implement the view code that generates the :term:`response`. |
b1b922
|
82 |
|
878d1a
|
83 |
#. *Lines 15-17*. Publish a :term:`WSGI` app using an HTTP server. |
b1b922
|
84 |
|
878d1a
|
85 |
As shown in this example, the :term:`configurator` plays a central role in |
SP |
86 |
Pyramid development. Building an application from loosely-coupled parts via |
|
87 |
:ref:`configuration_narr` is a central idea in Pyramid, one that we will |
|
88 |
revisit regularly in this *Quick Tutorial*. |
|
89 |
|
b1b922
|
90 |
|
65687f
|
91 |
Extra credit |
b1b922
|
92 |
============ |
PE |
93 |
|
|
94 |
#. Why do we do this: |
|
95 |
|
|
96 |
.. code-block:: python |
|
97 |
|
92b27d
|
98 |
print('Incoming request') |
b1b922
|
99 |
|
PE |
100 |
...instead of: |
|
101 |
|
|
102 |
.. code-block:: python |
|
103 |
|
92b27d
|
104 |
print 'Incoming request' |
b1b922
|
105 |
|
PE |
106 |
#. What happens if you return a string of HTML? A sequence of integers? |
|
107 |
|
878d1a
|
108 |
#. Put something invalid, such as ``print xyz``, in the view function. Kill |
SP |
109 |
your ``python app.py`` with ``ctrl-C`` and restart, then reload your |
|
110 |
browser. See the exception in the console? |
b1b922
|
111 |
|
878d1a
|
112 |
#. The ``GI`` in ``WSGI`` stands for "Gateway Interface". What web standard is |
SP |
113 |
this modelled after? |