diff options
| author | Paul Everitt <paul@agendaless.com> | 2013-08-03 11:23:20 -0400 |
|---|---|---|
| committer | Paul Everitt <paul@agendaless.com> | 2013-08-03 11:23:20 -0400 |
| commit | 83fefbf3f92183d1d899c8449a03546dd3c022a3 (patch) | |
| tree | 84a7e584d06e8ca042e91ff71ad847acd34f270d /docs/getting_started | |
| parent | d44d0e044555cd1287680fc8368632a64ebe979b (diff) | |
| download | pyramid-83fefbf3f92183d1d899c8449a03546dd3c022a3.tar.gz pyramid-83fefbf3f92183d1d899c8449a03546dd3c022a3.tar.bz2 pyramid-83fefbf3f92183d1d899c8449a03546dd3c022a3.zip | |
"Web Application Development Framework" -> "Web Framework". Yay.
Diffstat (limited to 'docs/getting_started')
| -rw-r--r-- | docs/getting_started/quick_glance.rst | 219 | ||||
| -rw-r--r-- | docs/getting_started/quick_glance/hello_world/hello_world/templates/mytemplate.jinja2 | 6 | ||||
| -rw-r--r-- | docs/getting_started/top_ten.rst | 2 |
3 files changed, 213 insertions, 14 deletions
diff --git a/docs/getting_started/quick_glance.rst b/docs/getting_started/quick_glance.rst index 13ae22ba5..da65f2e51 100644 --- a/docs/getting_started/quick_glance.rst +++ b/docs/getting_started/quick_glance.rst @@ -2,20 +2,48 @@ Quick Glance ============ -Pyramid lets you start small and finish big. The -:doc:`index` guide +Pyramid lets you start small and finish big. This :doc:`index` guide walks you through many of the key features. Let's put the emphasis on *start* by doing a quick tour through Pyramid. -This *Quick Glance* is shorthand, snippet-oriented. It is not intended -as full example. Instead, the other chapters will provide complete -examples. +This *Quick Glance* is provides snippets instead of full examples. For +working code, see the *Getting Started* chapters on each topic. .. note:: Like the rest of Getting Started, we're using Python 3 in our samples. You can too, or you can use Python 2.7. +Setup +===== + +This is just a "quick glance", so we won't kill ourselves showing +installation details. The guides fully cover each topic, +including setup. In a nutshell: + +.. code-block:: bash + + $ pyvenv-3.3 env33 + $ source env33/bin/activate + $ curl -O http://python-distribute.org/distribute_setup.py + $ python3.3 ./distribute_setup.py + $ rm distribute* + $ easy_install-3.3 pip + $ pip-3.3 install pyramid + +We use Python 3.3's builtin virtual environment tool ``pyvenv`` to make +an isolated Python. It is so isolated, we don't have package +installation tools! After setting those up and cleaning up, +we install Pyramid into our virtual environment. + +.. note:: + + Note the use of ``3.3`` on many of the commands, as a way to emphasize + in this document which versions of the commands we are using. This is + optional. + + + The Smallest ============ @@ -506,14 +534,183 @@ Pyramid supplies helpers for test writing, which we use in the test setup and teardown. Our one test imports the view, makes a dummy request, and sees if the view returns what we expected. +Logging +======= + +It's important to know what is going on inside our web application. +In development we might need to collect some output. In production, +we might need to detect situations when other people use the site. We +need *logging*. + +Fortunately Pyramid uses the normal Python approach to logging. The +scaffold generated, in your ``development.ini``, a number of lines that +configure the logging for you to some reasonable defaults. You then see +messages sent by Pyramid (for example, when a new request comes in.) + +Maybe you would like to log messages in your code? In your Python +module, import and setup the logging: + +.. code-block:: python + + import logging + log = logging.getLogger(__name__) + +You can now, in your code, log messages: + +.. code-block:: python + + log.debug('Some Message') +This will log ``Some Message`` at a ``debug`` log level, +to the application-configured logger in your ``development.ini``. What +controls that? These sections in the configuration file: + +.. code-block:: ini + + [loggers] + keys = root, hello_world + + [logger_hello_world] + level = DEBUG + handlers = + qualname = hello_world + +Our application, a package named ``hello_world``, is setup as a logger +and configured to log messages at a ``DEBUG`` or higher level. + +Sessions +======== + +When people use your web application, they frequently perform a task +that requires semi-permanent data to be saved. For example,a shopping +cart. These are frequently called *sessions*. + +Pyramid has basic built-in support for sessions, with add-ons such as +*Beaker* (or your own custom sessioning engine) that provide richer +session support. For the built-in session support, you first import +the "factory" which provides the sessioning: + +.. code-block:: python + + from pyramid.session import UnencryptedCookieSessionFactoryConfig + my_session_factory = UnencryptedCookieSessionFactoryConfig('itsaseekreet') + +We tell the configuration system that this is the source of our +sessioning support when setting up the ``Configurator``: + +.. code-block:: python + + config = Configurator(session_factory = my_session_factory) + +This now lets us use the session in our application code: + +.. code-block:: python + + session = request.session + if 'abc' in session: + session['fred'] = 'yes' + +Databases +========= - - logging +Web applications mean data. Data means databases. Frequently SQL +databases. SQL Databases frequently mean an "ORM" +(object-relational mapper.) In Python, ORM usually leads to the +mega-quality *SQLAlchemy*, a Python package that greatly eases working +with databases. - - resources, asset specs, tests, +Pyramid and SQLAlchemy are great friends. That friendship includes a +scaffold! + +.. code-block:: bash + + $ pcreate --scaffold alchemy hello_sqlalchemy + $ cd hello_sqlalchemy + $ python3.3 setup.py develop + +We now have a working sample SQLAlchemy application with all +dependencies installed. The sample project provides a console script to +initialize a SQLite database with tables. Let's run it and then start +the application: + +.. code-block:: bash + + $ initialize_hello_sqlalchemy_db development.ini + $ pserve development.ini + +We can now visit our sample at +`http://localhost:6543/ <http://localhost:6543/>`_. Some choices that +the scaffold helped us with: + +- A ``setup.py`` with appropriate dependencies + +- Connection strings and integration in our ``development.ini`` file + +- A console script which we ran above to initialize the database + +- The SQLAlchemy engine integrated into the ``Configurator`` on + application startup + +- Python modules for the SQLAlchemy models and the Pyramid views that + go with them + +- Some unit tests...yummy! + +As mentioned above, an ORM is software that eases the mapping of +database structures into a programming language. SQLAlchemy uses models +for this, and its scaffold generated a sample model: + +.. code-block:: python + + class MyModel(Base): + __tablename__ = 'models' + id = Column(Integer, primary_key=True) + name = Column(Text, unique=True) + value = Column(Integer) + + def __init__(self, name, value): + self.name = name + self.value = value + +The Python module also includes this: + +.. code-block:: python + + from zope.sqlalchemy import ZopeTransactionExtension + +The generated application includes the optional support for +``pyramid_tm``, a unique transaction monitor that integrates your +database transactions with your code for transparent rollback and commit. + +View code, which mediates the logic between web requests and the rest +of the system, can then easily get at the data: + +.. code-block:: python + + one = DBSession.query(MyModel).filter(MyModel.name == 'one').first() + + +Forms +===== + +Developers have lots of opinions about forms, and thus there are many +form libraries for Python. Pyramid doesn't directly bundle a form +library, but *Deform* is a popular choice. Let's see it in action. +First we install it: + +.. code-block:: bash + + $ pip-3.3 install deform + + + +Authentication +============== + + +Authorization +============= -sessions, logging, special views -databases, forms, security Notes @@ -534,6 +731,6 @@ Notes - Debugging -- Template reloading +- Explain and link to WSGI, Python Packages -- Explain and link to WSGI, Python Packages
\ No newline at end of file +- Richer routing
\ No newline at end of file diff --git a/docs/getting_started/quick_glance/hello_world/hello_world/templates/mytemplate.jinja2 b/docs/getting_started/quick_glance/hello_world/hello_world/templates/mytemplate.jinja2 index a796c7273..998edfe12 100644 --- a/docs/getting_started/quick_glance/hello_world/hello_world/templates/mytemplate.jinja2 +++ b/docs/getting_started/quick_glance/hello_world/hello_world/templates/mytemplate.jinja2 @@ -1,7 +1,7 @@ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> - <title>The Pyramid Web Application Development Framework</title> + <title>The Pyramid Web Framework</title> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/> <meta name="keywords" content="python web application" /> <meta name="description" content="pyramid web application" /> @@ -21,14 +21,14 @@ <body> <div id="wrap"> <div id="header"> - <div class="header">The Pyramid Web Application Development Framework</div> + <div class="header">The Pyramid Web Framework</div> </div> <div id="top"> <div class="top align-center"> <img src="{{request.application_url}}/static/logo.png" width="300" height="80" alt="Logo"/> <p class="app-welcome"> Welcome to <span class="app-name">{{project}}</span>, an application generated by<br/> - the Pyramid web application development framework. + the Pyramid Web Framework. </p> </div> </div> diff --git a/docs/getting_started/top_ten.rst b/docs/getting_started/top_ten.rst index 071f41d1c..a11d2898c 100644 --- a/docs/getting_started/top_ten.rst +++ b/docs/getting_started/top_ten.rst @@ -28,3 +28,5 @@ Custom views - Asset specifications - Traversal + +- Transactions
\ No newline at end of file |
