forked from squeaky-pl/japronto
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request squeaky-pl#42 from jacobbridges/html-example#38
New example: Serving HTML with japronto (squeaky-pl#38)
- Loading branch information
Showing
3 changed files
with
94 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# examples/8_html/html.py | ||
from japronto import Application | ||
from jinja2 import Template | ||
|
||
|
||
# A view can read HTML from a file | ||
def index(request): | ||
with open('index.html') as html_file: | ||
return request.Response(text=html_file.read(), mime_type='text/html') | ||
|
||
|
||
# A view could also return a raw HTML string | ||
def example(request): | ||
return request.Response(text='<h1>Some HTML!</h1>', mime_type='text/html') | ||
|
||
|
||
# A view could also return a rendered jinja2 template | ||
def jinja(request): | ||
template = Template('<h1>Hello {{ name }}!</h1>') | ||
return request.Response(text=template.render(name='World'), | ||
mime_type='text/html') | ||
|
||
|
||
# Create the japronto application | ||
app = Application() | ||
|
||
# Add routes to the app | ||
app.router.add_route('/', index) | ||
app.router.add_route('/example', example) | ||
app.router.add_route('jinja2', jinja) | ||
|
||
# Start the server | ||
app.run(debug=True) | ||
|
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<!doctype html> | ||
|
||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8"> | ||
|
||
<title>japronto</title> | ||
<meta name="description" content="japronto"> | ||
</head> | ||
|
||
<body> | ||
<h1>Hello World!</h1> | ||
|
||
<p>Behold, the power of japronto!</p> | ||
</body> | ||
</html> |
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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# Responding with HTML | ||
|
||
Serving HTML from japronto is as simple as adding a MIME type of `text/html` to the Response. Jinja2 templating can be leveraged as well, although in the meantime you will have to do the heavy lifting of rendering templates before sending in a response. | ||
|
||
Copy and paste following code into a file named `html.py`: | ||
|
||
```python | ||
# examples/8_html/html.py | ||
from japronto import Application | ||
from jinja2 import Template | ||
|
||
|
||
# A view can read HTML from a file | ||
def index(request): | ||
with open('index.html') as html_file: | ||
return request.Response(text=html_file.read(), mime_type='text/html') | ||
|
||
|
||
# A view could also return a raw HTML string | ||
def example(request): | ||
return request.Response(text='<h1>Some HTML!</h1>', mime_type='text/html') | ||
|
||
|
||
# A view could also return a rendered jinja2 template | ||
def jinja(request): | ||
template = Template('<h1>Hello {{ name }}!</h1>') | ||
return request.Response(text=template.render(name='World'), | ||
mime_type='text/html') | ||
|
||
|
||
# Create the japronto application | ||
app = Application() | ||
|
||
# Add routes to the app | ||
app.router.add_route('/', index) | ||
app.router.add_route('/example', example) | ||
app.router.add_route('jinja2', jinja) | ||
|
||
# Start the server | ||
app.run(debug=True) | ||
``` | ||
|
||
The source code for all the examples can be found in [examples directory](https://github.com/squeaky-pl/japronto/tree/master/examples). | ||
|