In your route that serves the signup form, read the file line by line, find the length of the resulting list, and pass the result to flask.render_template
:
signup.html
:
<html>
<body>
<p> Join {{num}} other user{{plural}} who have already signed up!</p>
<form action = '/get_data' method='POST'>
<input type='email' name='email' placeholder='enter your email'>
<input type='submit'>Submit</input>
</form>
<body>
</html>
In the route:
@app.route('/signup', methods=['GET'])
def signup():
current_users = len([i for i in open('filename.txt')])
return flask.render_template('signup.html', num = current_users, plural = '' if current_users == 1 else 's')
@app.route('/get_data', methods=['POST'])
def get_email():
with open('filename.txt', 'a') as f:
f.write('{}\n'.format(flask.request.form["email"]))
return '<h1>Thanks!</h1>'