3.3. Creating own web content¶
The final chapter is all about creating your first own view with groundwork-web.
Therefore we will create a simple HTML page, which presents the changes of each watcher and has a button to delete the history of each watcher.
The final result will look like this:
3.3.2. View function¶
Now we add the view itself to our plugin:
1 2 3 4 5 6 7 8 9 10 11 12 13 | menu_csv.register("History", link=url_for("csv._history_view"))
def _history_view(self):
if request.method == 'POST':
csv_file = request.form['csv_file']
csv_file_object = self.CsvFile.query.filter_by(name=csv_file).first()
versions = self.Version.query.filter_by(csv_file=csv_file_object).all()
for version in versions:
self.db.session.delete(version)
self.db.commit()
flash("Versions of %s deleted" % request.form['csv_file'])
watchers = self.get_csv_history()
|
Lines 11+12 are the most important ones, because line 11 gets the history data and line 12 renders our HTML page.
The function self.web.render
takes as fist argument the HTML template file name.
After this we can provide as many keyword based arguments as we like. They will be available inside our template.
Lines 3-10 cares about the correct history cleaning, if the button gets pressed.
The code gets executed only, if the request is coming via POST
(Line 3).
After deletion, we want to inform the user that everything got deleted as requests. So we use flash()
.
3.3.3. Template¶
And finally lets create our HTML template file.
Inside the folder of our plugin CsvDocumentPlugin
, we create a folder called templates
.
And there we add a file csv_history.html
with the following content.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | {% extends 'master.html' %}
{% block body %}
<h1>CSV History <small>View and edit history</small></h1>
<h2>History</h2>
{% for watcher in watchers %}
<h3>{{watcher.name}}</h3>
<form action="" method="post">
<input type="hidden" name="csv_file" value="{{watcher.name}}">
<input type="submit" value="Clean history">
</form>
{% for version in watcher.version %}
<h4>{{version.version}}</h4>
<h5>Missing rows</h5>
{% for row in version.missing_row %}
{% for key,value in row.row.items() %}
{{ key }} : <b>{{value}}</b>
{% endfor %}
<br>
{% endfor %}
<h5>New rows</h5>
{% for row in version.new_row %}
{% for key,value in row.row.items() %}
{{ key }} : <b>{{value}}</b>
{% endfor %}
<br>
{% endfor %}
{% endfor %}
{% endfor %}
{% endblock %}
|
The template uses the jinja template language, which is quite easy to read.
In Line 8 we request the first time our variable watchers
, which is available because we have care about this in
the function render()
above.
We uses some for-loops to output our data with {{ .. }}.
3.3.4. Final words¶
That’s it. Everything is done and running and we are able to see our watchers working.
If you have any questions or ideas, please get in contact with us. The easiest way is by creating an issue on the github pages of groundwork-tutorial or groundwork .