-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BACKWARD-INCOMPATIBLE: Added the
serve_static_file
shortcut for t…
…he typical static file use case. Thanks to defunkt for the original patch. To migrate, simply remove the ``request`` from any calls to ``static_file``.
- Loading branch information
1 parent
9b33b3b
commit dde698a
Showing
3 changed files
with
63 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,13 @@ | ||
Primary authors: | ||
|
||
* Daniel Lindsley | ||
* Daniel Lindsley (toastdriven) | ||
|
||
|
||
Contributors: | ||
|
||
* Matt Croydon for various patches and suggestions | ||
* Christian Metts for various patches and suggestions | ||
* marly for a patch to an exception's message | ||
* tstromberg for a patch related to printing tracebacks on errors | ||
* mronge for a patch to the static files example | ||
* Matt Croydon (mcroydon) for various patches and suggestions. | ||
* Christian Metts (mintchaos) for various patches and suggestions. | ||
* marly for a patch to an exception's message. | ||
* tstromberg for a patch related to printing tracebacks on errors. | ||
* mronge for a patch to the static files example. | ||
* Chris Wanstrath (defunkt) for static files patches & suggestions. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,47 @@ | ||
from itty import * | ||
|
||
MY_ROOT = os.path.join(os.path.dirname(__file__), 'media') | ||
|
||
|
||
@get('/') | ||
def index(request): | ||
return 'Hello World!' | ||
return '<img src="media/itty.png">' | ||
|
||
# To serve static files, simply setup a standard @get method. You should | ||
# capture the filename/path and get the content-type. If your media root is | ||
# different than where your ``itty.py`` lives, manually setup your root | ||
# directory as well. Finally, use the ``static_file`` handler to serve up the | ||
# directory as well. Finally, use the ``static_file`` helper to serve up the | ||
# file. | ||
@get('/media/(?P<filename>.+)') | ||
def my_media(request, filename): | ||
my_root = os.path.join(os.path.dirname(__file__), 'media') | ||
output = static_file(request, filename=filename, root=my_root) | ||
output = static_file(filename, root=MY_ROOT) | ||
return Response(output, content_type=content_type(filename)) | ||
|
||
|
||
# Alternative, if sane-ish defaults are good enough for you, you can use the | ||
# ``serve_static_file`` handler to do the heavy lifting for you. For example: | ||
@get('/simple/') | ||
def simple(request): | ||
return """ | ||
<html> | ||
<head> | ||
<title>Simple CSS</title> | ||
<link rel="stylesheet" type="text/css" href="/simple_media/default.css"> | ||
</head> | ||
<body> | ||
<h1>Simple CSS is Simple!</h1> | ||
<p>Simple reset here.</p> | ||
</body> | ||
</html> | ||
""" | ||
|
||
# By default, the ``serve_static_file`` will try to guess the correct content | ||
# type. If needed, you can enforce a content type by using the | ||
# ``force_content_type`` kwarg (i.e. ``force_content_type='image/jpg'`` on a | ||
# directory of user uploaded images). | ||
@get('/simple_media/(?P<filename>.+)') | ||
def simple_media(request, filename): | ||
return serve_static_file(request, filename, root=MY_ROOT) | ||
|
||
|
||
run_itty() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters