Skip to content

Commit

Permalink
Restructured the Hello Flask Act to not jump into building a function…
Browse files Browse the repository at this point in the history
… so quickly
  • Loading branch information
palewire committed Mar 5, 2016
1 parent 3131ea8 commit 2715794
Show file tree
Hide file tree
Showing 11 changed files with 179 additions and 125 deletions.
46 changes: 23 additions & 23 deletions app.py
Expand Up @@ -5,29 +5,29 @@
app = Flask(__name__)


def get_csv():
csv_path = './static/la-riots-deaths.csv'
csv_file = open(csv_path, 'r')
csv_obj = csv.DictReader(csv_file)
csv_list = list(csv_obj)
return csv_list


@app.route("/")
def index():
template = 'index.html'
object_list = get_csv()
return render_template(template, object_list=object_list)


@app.route('/<row_id>/')
def detail(row_id):
template = 'detail.html'
object_list = get_csv()
for row in object_list:
if row['id'] == row_id:
return render_template(template, object=row)
abort(404)
# def get_csv():
# csv_path = './static/la-riots-deaths.csv'
# csv_file = open(csv_path, 'r')
# csv_obj = csv.DictReader(csv_file)
# csv_list = list(csv_obj)
# return csv_list
#
#
# @app.route("/")
# def index():
# template = 'index.html'
# object_list = get_csv()
# return render_template(template, object_list=object_list)
#
#
# @app.route('/<row_id>/')
# def detail(row_id):
# template = 'detail.html'
# object_list = get_csv()
# for row in object_list:
# if row['id'] == row_id:
# return render_template(template, object=row)
# abort(404)


if __name__ == '__main__':
Expand Down
Binary file modified docs/_build/doctrees/environment.pickle
Binary file not shown.
Binary file modified docs/_build/doctrees/index.doctree
Binary file not shown.
Binary file added docs/_build/html/_images/hello-flask-404.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
89 changes: 54 additions & 35 deletions docs/_build/html/_sources/index.txt
Expand Up @@ -122,7 +122,7 @@ test to see what version, if any, is there waiting for you by typing the followi
$ python -V

If you don't have Python installed (a more likely fate for Windows users) try downloading and installing it from `here
<http://www.python.org/download/releases/2.7.6/>`_. In Windows, it's also crucial to make sure that the
<https://www.python.org/download/releases/2.7.8/>`_. In Windows, it's also crucial to make sure that the
Python program is available on your system's ``PATH`` so it can be called from anywhere on the command line. `This screencast <http://showmedo.com/videotutorials/video?name=960000&fromSeriesID=96>`_ can guide
you through that process.

Expand Down Expand Up @@ -300,9 +300,40 @@ application's "backend," routing data to the appropriate pages.
from flask import Flask
app = Flask(__name__) # Note the double underscores on each side!

Next we will configure Flask to make a page at your site's root URL, where we will publish the complete list of people who died during the riots using a template called ``index.html``.
Next we will configure Flask to make a page at your site's root URL.

That starts by importing ``render_template``, a Flask function we can use to combine data with HTML.
Configure Flask to boot up a test server when you run ``app.py``.

.. code-block:: python
:emphasize-lines: 4-6

from flask import Flask
app = Flask(__name__)

if __name__ == '__main__':
# Fire up the Flask test server
app.run(debug=True, use_reloader=True)

.. note::

You're probably asking, "What the heck is ``if __name__ == '__main__'``?" The short answer: It's just one of the weird things in Python you have to memorize. But it's worth the brain space because it allows you to run any Python script as a program.

Anything indented inside that particular ``if`` clause is executed when the script is called from the command line. In this case, that means booting up your web site using Flask's built-in ``app.run`` function.

Don't forget to save your changes. Then run ``app.py`` on the command-line and open up your browser to `localhost:5000 <http://localhost:5000>`_

.. code-block:: bash

$ python app.py

Here's what you should see. A website with nothing to show.

.. image:: /_static/hello-flask-404.png

Next we'll put a page there. Our goal is to publish the complete list of
people who died during the riots using a template called ``index.html``.

That starts by importing ``render_template``, a Flask function we can use to combine data with HTML to make a webpage.

.. code-block:: python
:emphasize-lines: 2
Expand All @@ -311,7 +342,11 @@ That starts by importing ``render_template``, a Flask function we can use to com
from flask import render_template
app = Flask(__name__)

Next create a function called ``index`` that returns our rendered ``index.html`` template.
if __name__ == '__main__':
# Fire up the Flask test server
app.run(debug=True, use_reloader=True)

Then create a function called ``index`` that returns our rendered ``index.html`` template.

.. code-block:: python
:emphasize-lines: 5-8
Expand All @@ -324,7 +359,12 @@ Next create a function called ``index`` that returns our rendered ``index.html``
template = 'index.html'
return render_template(template)

Finally use one of Flask's coolest tricks, the ``app.route`` decorater to connect that function with the root URL of our site, ``/``.
if __name__ == '__main__':
# Fire up the Flask test server
app.run(debug=True, use_reloader=True)

Now use one of Flask's coolest tricks, the ``app.route`` decorater, to connect
that function with the root URL of our site, ``/``.

.. code-block:: python
:emphasize-lines: 5
Expand All @@ -338,7 +378,11 @@ Finally use one of Flask's coolest tricks, the ``app.route`` decorater to connec
template = 'index.html'
return render_template(template)

Now return to your command-line interface and create a directory to store your templates in `the default location Flask expects <http://flask.pocoo.org/docs/quickstart/#rendering-templates>`_.
if __name__ == '__main__':
# Fire up the Flask test server
app.run(debug=True, use_reloader=True)

Return to your command-line interface and create a directory to store your templates in `the default location Flask expects <http://flask.pocoo.org/docs/quickstart/#rendering-templates>`_.

.. code-block:: bash

Expand All @@ -359,38 +403,13 @@ Open it up in your text editor and write something clever.

Hello World!

Return to ``app.py`` and configure Flask to boot up a test server when you run it.

.. code-block:: python
:emphasize-lines: 10-12

from flask import Flask
from flask import render_template
app = Flask(__name__)

@app.route("/")
def index():
template = 'index.html'
return render_template(template)

if __name__ == '__main__':
app.run(debug=True, use_reloader=True)

.. note::

You're probably asking, what the heck is ``if __name__ == '__main__'``? It's just one of the weird things in Python you have to memorize. But it's worth the brain space because it allows you to run any Python script all by itself.

Anything indented inside that particular ``if`` clause is executed when the script is called from the command line. In this case, that means booting up your web site using Flask's built-in ``app.run`` function.

Don't forget to save your changes. Then run ``app.py`` on the command-line and open up your browser to `localhost:5000 <http://localhost:5000>`_

.. code-block:: bash

$ python app.py
Head back to your browser and visit `localhost:5000 <http://localhost:5000>`_ again. You should see
the contents of your template displayed on the page.

.. image:: /_static/hello-flask-hello-world.png

Now return to the command line and commit your work to your Git repository.
We're approaching the end of this act, so it's time to save your work by returning to the
command line and committing these changes to your Git repository.

.. note::

Expand Down
Binary file added docs/_build/html/_static/hello-flask-404.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 2715794

Please sign in to comment.