资源说明:A TODO list made with the flask framework for PyClass at Noisebridge
TODO at Noisebridge
Note that in this case, flask will be serving the css for us. In most cases of production
deployment you will want to have your webserver do this. We will talk more about this in
later classes.
Let's add a some directories for service static content (*must* be called static)::
> mkdir static
> mkdir static/css
> mkdir static/javascript
> mkdir static/images
> touch static/css/noiselist.css
Now let's add some styles to static/css/noiselist.css::
footer{
background-image: url(https://www.noisebridge.net/NB-logo-red-black-med.png);
background-position: bottom right;
background-repeat: no-repeat;
min-height: 130px;
}
div.content{
margin-top: 70px;
}
This is just a basic logo that let's us know that we are serving up the correct content.
Let's update our front page to use bootstraps styles. For brevity I will just point to
the raw source since its a lot. Update hello.html with the
code at::
https://raw.github.com/noisebridge/flask-noiselist/d1137326c11cb908ddc6d59598913e439d5b1f83/src/noiselist/templates/hello.html
Reload and party.
Hooking up to Data
------------------
Flask passes arguments to the templating language just like web2 py does. To quickly
pass in a list of items to display on the front page, update __init__.py to say::
def index():
todo_list = ["Watch TV",
"Contemplate Work",
"Go to Bed",
]
return render_template('hello.html', todos=todo_list)
Then in hello.html we update the list to pull from the todos passed in::
Introduction
------------
This is the second installment of our tour of web frameworks. This will
be a simple TODO list.
Again, this tutorial is probably not suited to Windows users. Please pair
with a friend if you are working in a windows environment. Sorry!
Up and Running
--------------
Just like in the last class, you want to run bootstrap and buildout. Unlike the last
class, the cloned noiselist is the full solution so you can view the commit log to see
how the app was built step by step. To get the first step::
> git clone ttps://github.com/noisebridge/flask-noiselist.git
> cd flask-noiselist
> virtualenv .
> git checkout ba4ebf111f
Because things have been updated, we need the latest bootstrap.py::
> git checkout master bootstrap.py
> ./bin/python bootstrap.py
> ./bin/buildout
To get the server running in foreground mode, do::
> ./bin/flask-ctl debug fg
Your app will be running at http://127.0.0.1:5000 with a simple hello world
placeholder.
Take a minute to notice the differences between this app and web2py. There is
no admin console and no formatting by default. Flask is really a micro framwork.
Notice as well that we are starting in foreground mode, and that you don't
have to kill a process or terminal to restart. Foreground mode will autodetect
changes and reload new code automatically. In doubt, simply Ctrl-C and restart
the server with './bin/flask-ctl debug fg'. This also means that any pdbing
will take you directly to this console.
Technical Note: Please be aware that the extra virtualenv step is likely not
required if your system has always used buildout or virtual envs. I leave it
here for anyone who has already mucked up their global packages. It's good
practice anyways :)
Modifying These Instructions
----------------------------
Since this is written by a human, please feel free to update the instructions in this
file itself and commit back. If you need permissions, feel free to contact me or just
fork and I will merge back. If there is interest in class we will discuss how this
works with github.
Folder Structure
----------------
Unlike web2py, you can not edit the application through the web (TTW). All the files
you will be editing will be in flask-noiselist/src. Take note that the app itself
should be in egg format.
script.py has a bunch of stuff you don't need to worry about in the moment. This
basically just sets up the app for running and testing. You should never have to
modify this.
The only python file we will be working with is __init__.py. Because the app we are
doing is small, this will be sufficient for all of our code, although it is not
really considered to be best practice.
Templating
----------
Note that unlike web2py, you are presented with a completely blank slate. We will be
touching a few different files in this exercize than the previous one because of this.
Let's get a few pretty things ironed out first so we know what we want the end result
to look like.
First, notice that we don't have any templates yet. Flask uses Jinja2, a "standalone"
templating engine. Most modern frameworks should be integrating with 1 or more different
templating engines. This is beneficial to you because you can learn one framework and
not have to learn a new templating language every time (web2py has its own templating
language). Jinja2 is very popular and widely used (and more importantly that means it
is well supported).
First we need to create a directory for holding our templates::
> cd src/noiselist
> mkdir templates
And lets take our Hello page and make it into a template by adding a template::
> touch templates/hello.html
In that file, let's add a few lines to show how our list will look in the end::
TODO at Noisebridge
My Personal TODO List
- Finish evaluating pull requests
- Finish writing up class work
- Swim and enjoy the sun
Current TODOs
-
{% for todo in todos %}
- {{ todo }} {% endfor %}
-
{% for todo in todos %}
- {{ todo.description }} {% endfor %}
-
Redirect
--------
Last but not least, let's add a redirect so that when the user submits a form, they go back
to the front page. In __init__.py::
from flask import redirect, url_for
...
db.session.add(todo)
db.session.commit()
return redirect(url_for('index'))
Note that the redirect here is saying to redirect the the url that the index function services!!!
Homework
--------
* Follow the rest of the tutorial at http://flask.pocoo.org/docs/quickstart to support multiple users.
* Check the input of the form and if it is empty, flash an error message. For considerable tips,
see http://flask.pocoo.org/docs/patterns/flashing/#message-flashing-pattern
More Info
---------
* Flask Documentation: http://flask.pocoo.org/docs/
* About Jinja2: http://jinja.pocoo.org/docs/
* Bootstrap: http://twitter.github.com/bootstrap/
* SQLAlchemy: http://www.sqlalchemy.org/
* SQLAlchemy in Flask: http://packages.python.org/Flask-SQLAlchemy
* For more info on this buildout itself, please see http://flask.pocoo.org/snippets/27/
本源码包内暂不包含可直接显示的源代码文件,请下载源码包。
English
