test
├app.py
├articles.csv
├Procfile
├requirements.txt
└templates
├index.html
├layout.html
└index_result.html
Set the virtual environment directly under the directory test and start it.
python3 -m venv .
source bin/activate
Install the required framework and web server.
pip install flask
pip install gunicorn
First, put the bulletin board data in articles.csv.
.csv:articles.csv
Marble,Sleepy
White,I'm hungry
Black,Somehow warm
Marble,Poe Poe Poe
Pontan,No toilet paper
Naochin,Chain
Create app.py.
.py:app.py
# -*- coding: utf-8 -*-
from flask import Flask,request,render_template
app = Flask(__name__)
@app.route('/')
def bbs():
lines = []
#read the csv file with open
with open('articles.csv',encoding='utf-8') as f:
lines = f.readlines() #readlines returns the contents of csv in list format
#index.Return to html
return render_template('index.html',lines=lines)
#Receive post method
@app.route('/result',methods=['POST'])
def result():
#Get the value of article and name in request
article = request.form['article']
name = request.form['name']
#Write to csv file in overwrite mode
with open('articles.csv','a',encoding='utf-8') as f:
f.write(name + ',' + article + '\n')
#index_result.Return to html
return render_template('index_result.html',article=article,name=name)
if __name__ == '__main__':
app.run(debug=False)
Create index.html.
.html:index.html
{% extends 'layout.html' %}
{% block content %}
<h1>Nyanko Bulletin Board</h1>
<form action='/result' method='post'>
<label for='name'>Nyanko's name</label>
<input type='text' name='name'>
<p></p>
<label for='article'>Post</label>
<input type='text' name='article'>
<button type='subimit'>Write</button>
</form>
<p></p>
<p></p>
<table border=1>
<tr><th>Nyanko's name</th><th>Posted content</th></tr>
{% for line in lines: %}
<!--Set a variable called column (set is required for variable set)-->
<!--Using split,Classify by. split returns a list-->
{% set column = line.rstrip().split(',') %}
<tr><td>{{column[0]}}</td><td>{{column[1]}}</td></tr>
{% endfor %}
</table>
{% endblock %}
Create an html template.
.html:layout.html
<!DOCTYPE html>
<html lang='ja'>
<head>
<meta charset='utf-8'>
<title>Nyanko BBS</title>
<style>body{padding:10px;}</style>
</head>
<body>
{% block content %}
{% endblock %}
</body>
</html>
The contents of the form entered in index.html are represented by index_result.html.
.html:layout.html
{% extends 'layout.html' %}
{% block content %}
<h1>Nya-I wrote on the bulletin board</h1>
<p>{{name}}{{article}}</p>
<!--in form/Back to-->
<form action='/' method='get'>
<button type='submit'>Return</button>
</form>
{% endblock %}
The details of deploying to Heroku are as described in the following article, so I will omit the detailed explanation. How to upload with Heroku, Flask, Python, Git (Part 2) I created Procfile and requirements.txt, operated it with git, and successfully deployed it.
If you post "Iwashi" or "I love fish", Successful writing! When I go back, It is properly posted on the bulletin board.Data disappears after a while (30 minutes) when writing csv on Heroku, so I want to create a bulletin board using a database such as sql.
Recommended Posts