Skip to content

Commit 95bf2b3

Browse files
author
Arnaud Coomans
committed
Added example with custom template
1 parent 872478f commit 95bf2b3

File tree

2 files changed

+194
-0
lines changed

2 files changed

+194
-0
lines changed

examples/custom/blog.py

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
from os import path
2+
from json import dumps
3+
4+
from flask import Flask, redirect, request
5+
from flask.ext.autodoc import Autodoc
6+
7+
8+
app = Flask(__name__)
9+
app.debug = True
10+
auto = Autodoc(app)
11+
12+
users = []
13+
posts = []
14+
15+
16+
class User(object):
17+
18+
def __init__(self, username):
19+
self.username = username
20+
users.append(self)
21+
self.id = users.index(self)
22+
23+
def __repr__(self):
24+
return dumps(self.__dict__)
25+
26+
27+
class Post(object):
28+
29+
def __init__(self, title, content, author):
30+
self.title = title
31+
self.content = content
32+
posts.append(self)
33+
self.id = posts.index(self)
34+
35+
def __repr__(self):
36+
return dumps(self.__dict__)
37+
38+
39+
u = User('acoomans')
40+
Post('First post', 'This is the first awesome post', u)
41+
Post('Second post', 'This is another even more awesome post', u)
42+
43+
44+
@app.route('/')
45+
@app.route('/posts')
46+
@auto.doc(groups=['posts', 'public', 'private'])
47+
def get_posts():
48+
"""Return all posts."""
49+
return '%s' % posts
50+
51+
52+
@app.route('/post/<int:id>')
53+
@auto.doc(groups=['posts', 'public', 'private'])
54+
def get_post(id):
55+
"""Return the post for the given id."""
56+
return '%s' % posts[id]
57+
58+
59+
@app.route('/post', methods=["POST"])
60+
@auto.doc(groups=['posts', 'private'])
61+
def post_post():
62+
"""Create a new post.
63+
Form Data: title, content, authorid.
64+
"""
65+
authorid = request.form.get('authorid', None)
66+
Post(request.form['title'],
67+
request.form['content'],
68+
users[authorid])
69+
return redirect("/posts")
70+
71+
72+
@app.route('/users')
73+
@auto.doc(groups=['users', 'public', 'private'])
74+
def get_users():
75+
"""Return all users."""
76+
return '%s' % users
77+
78+
79+
@app.route('/user/<int:id>')
80+
@auto.doc(groups=['users', 'public', 'private'])
81+
def get_user(id):
82+
"""Return the user for the given id."""
83+
return '%s' % users[id]
84+
85+
86+
@app.route('/users', methods=['POST'])
87+
@auto.doc(groups=['users', 'private'])
88+
def post_user(id):
89+
"""Creates a new user.
90+
Form Data: username.
91+
"""
92+
User(request.form['username'])
93+
redirect('/users')
94+
95+
96+
@app.route('/admin', methods=['GET'])
97+
@auto.doc(groups=['private'])
98+
def admin():
99+
"""Admin interface."""
100+
return 'Admin interface'
101+
102+
103+
@app.route('/doc/')
104+
@app.route('/doc/public')
105+
def public_doc():
106+
return auto.html(groups=['public'], title='Blog Documentation with Custom template', template="autodoc_custom.html")
107+
108+
109+
@app.route('/doc/private')
110+
def private_doc():
111+
return auto.html(groups=['private'], title='Private Documentation with Custom template', template="autodoc_custom.html")
112+
113+
114+
if __name__ == '__main__':
115+
app.run()
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<html>
2+
<head>
3+
<title>
4+
{% if title is defined -%}
5+
{{title}}
6+
{% else -%}
7+
Documentation
8+
{% endif -%}
9+
</title>
10+
<style>
11+
* {
12+
margin: 0;
13+
padding: 0;
14+
font-family: Verdana, "Helvetica Neue", Arial, Helvetica, Geneva, sans-serif;
15+
}
16+
17+
body {
18+
margin: 10px;
19+
}
20+
21+
div.mapping {
22+
margin: 20px 20px;
23+
}
24+
25+
.location { font-style: italic;}
26+
27+
ul.methods:before { content: "Methods: "; }
28+
ul.methods li {
29+
display: inline;
30+
list-style: none;
31+
}
32+
ul.methods li:after { content: ","; }
33+
ul.methods li:last-child:after { content: ""; }
34+
35+
ul.arguments:before { content: "Arguments: "; }
36+
ul.arguments li {
37+
display: inline;
38+
list-style: none;
39+
}
40+
ul.arguments .argument { font-style:italic }
41+
ul.arguments .default:not(:empty):before { content: "("; }
42+
ul.arguments .default:not(:empty):after { content: ")"; }
43+
ul.arguments li:after { content: ","; }
44+
ul.arguments li:last-child:after { content: ""; }
45+
46+
.docstring:before { content: "Description: "; }
47+
</style>
48+
</head>
49+
<body>
50+
<h1>
51+
{% if title is defined -%}
52+
{{title}}
53+
{% else -%}
54+
Documentation
55+
{% endif -%}
56+
</h1>
57+
58+
{% for doc in autodoc %}
59+
<div class="mapping">
60+
<a id="rule-{{doc.rule|urlencode}}" class="rule"><h2>{{doc.rule|escape}}</h2></a>
61+
<p class="location">{{doc.location.filename}}:{{doc.location.line}}</p>
62+
<ul class="methods">
63+
{% for method in doc.methods -%}
64+
<li class="method">{{method}}</li>
65+
{% endfor %}
66+
</ul>
67+
<ul class="arguments">
68+
{% for arg in doc.args %}
69+
<li>
70+
<span class="argument">{{arg}}</span>
71+
<span class="default">{{doc.defaults[arg]}}</span>
72+
</li>
73+
{% endfor %}
74+
</ul>
75+
<p class="docstring">{{doc.docstring|urlize|nl2br}}</p>
76+
</div>
77+
{% endfor %}
78+
</body>
79+
</html>

0 commit comments

Comments
 (0)