Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions editable_text.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
How are you my love
20 changes: 10 additions & 10 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
appdirs==1.4.3
click==6.7
Flask==0.12
itsdangerous==0.24
Jinja2==2.9.5
MarkupSafe==1.0
packaging==16.8
pyparsing==2.2.0
six==1.10.0
Werkzeug==0.11.15
appdirs
click
Flask
itsdangerous
Jinja2
MarkupSafe
packaging
pyparsing
six
Werkzeug
37 changes: 26 additions & 11 deletions routes.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,34 @@
from flask import Flask, render_template
from flask import Flask, render_template, request, redirect, url_for

app = Flask(__name__)

# two decorators, same function
# Path to the text file
TEXT_FILE = 'editable_text.txt'

# Load text from file
def load_text():
try:
with open(TEXT_FILE, 'r') as file:
return file.read()
except FileNotFoundError:
return ''

# Save text to file
def save_text(text):
with open(TEXT_FILE, 'w') as file:
file.write(text)

@app.route('/')
@app.route('/index.html')
@app.route('/index.html') # get method
def index():
return render_template('index.html', the_title='Tiger Home Page')

@app.route('/symbol.html')
def symbol():
return render_template('symbol.html', the_title='Tiger As Symbol')
text = load_text()
return render_template('index.html', the_title='Text Editor', text=text)

@app.route('/myth.html')
def myth():
return render_template('myth.html', the_title='Tiger in Myth and Legend')
@app.route('/save', methods=['POST'])
def save():
text = request.form['editor']
save_text(text)
return redirect(url_for('index'))

if __name__ == '__main__':
app.run(debug=True)
42 changes: 15 additions & 27 deletions templates/index.html
Original file line number Diff line number Diff line change
@@ -1,27 +1,15 @@
{% extends 'base.html' %}

{% block body %}

<!-- to be filled by the Python script -->
<h1>{{ the_title }}</h1>

<!-- note how items from the "static" folder are linked -->
<img alt="Photo: Bengal Tiger" src="{{ url_for('static', filename='images/tiger.jpg') }}">
<p class="credit">Photo by John and Karen Hollingsworth, retouched by Zwoenitzer (Fish &amp; Wildlife Service, ID WO0409-33F) [Public domain] <a href="https://commons.wikimedia.org/wiki/File%3APanthera_tigris_tigris.jpg" target="_blank">via Wikimedia Commons</a></p>

<p>This is the index page for my example Flask app.</p>

<ul>
<!-- note how items from routes in the the Python script are linked -->
<li><a href="{{ url_for('myth') }}">In myth and legend</a></li>
<li><a href="{{ url_for('symbol') }}">As a symbol</a></li>
</ul>

<p>The tiger (<em>Panthera tigris</em>) is the largest cat species, most recognisable for their pattern of dark vertical stripes on reddish-orange fur with a lighter underside. The species is classified in the genus <em>Panthera</em> with the lion, leopard, jaguar and snow leopard. Tigers are apex predators, primarily preying on ungulates such as deer and bovids. They are territorial and generally solitary but social animals, often requiring large contiguous areas of habitat that support their prey requirements. This, coupled with the fact that they are indigenous to some of the more densely populated places on Earth, has caused significant conflicts with humans.</p>

<p>Tigers once ranged widely across eastern Eurasia, from the Black Sea in the west, to the Indian Ocean in the south, and from Kolyma to Sumatra in the east. Over the past 100 years, they have lost 93% of their historic range, and have been extirpated from Western and Central Asia, from the islands of Java and Bali, and from large areas of Southeast, Southern and Eastern Asia. Today, they range from the Siberian taiga to open grasslands and tropical mangrove swamps. The remaining six tiger subspecies have been classified as endangered by the International Union for Conservation of Nature (IUCN). The global population in the wild is estimated to number between 3,062 and 3,948 individuals, down from around 100,000 at the start of the 20th century, with most remaining populations occurring in small pockets isolated from each other, of which about 2,000 exist on the Indian subcontinent. A 2016 global census estimated the population of wild tigers at approximately 3,890 individuals.</p>

<p>(Text from <a href="https://en.wikipedia.org/wiki/Tiger" target="_blank">Wikipedia</a>)</p>


{% endblock %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{ the_title }}</title>
</head>
<body>
<h1>{{ the_title }}</h1>
<form action="{{ url_for('save') }}" method="post">
<textarea name="editor" rows="10" cols="50">{{ text }}</textarea><br>
<button type="submit">Save</button>
</form>
</body>
</html>