Skip to content

Commit

Permalink
Merge pull request squeaky-pl#42 from jacobbridges/html-example#38
Browse files Browse the repository at this point in the history
New example: Serving HTML with japronto (squeaky-pl#38)
  • Loading branch information
squeaky-pl authored Feb 9, 2017
2 parents 179095e + 3661725 commit 20064de
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 0 deletions.
34 changes: 34 additions & 0 deletions examples/8_html/html.py
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)

16 changes: 16 additions & 0 deletions examples/8_html/index.html
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>
44 changes: 44 additions & 0 deletions tutorial/8_html.md
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).

0 comments on commit 20064de

Please sign in to comment.