Create Heroku, Flask, Python, Nyanko bulletin boards with "csv files"

(1) Create a bulletin board using csv


Directory structure

test
├app.py
├articles.csv
├Procfile
├requirements.txt
└templates
  ├index.html
  ├layout.html
  └index_result.html

① Content creation

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 %}

② Deploy to Heroku

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.

スクリーンショット 2020-03-10 23.16.27.png If you post "Iwashi" or "I love fish", スクリーンショット 2020-03-10 23.16.41.png Successful writing! When I go back, スクリーンショット 2020-03-10 23.16.54.png It is properly posted on the bulletin board.

Next time I want to make a bulletin board using sql

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

Create Heroku, Flask, Python, Nyanko bulletin boards with "csv files"
Create a bulletin board with Heroku, Flask, SQLAlchemy
Handle Excel CSV files with Python
I made a Nyanko tweet form with Python, Flask and Heroku
Easy web app with Python + Flask + Heroku
[Python] Quickly create an API with Flask
Csv tinkering with python
[Python] Reading CSV files
Programming with Python Flask
Create an animated GIF local server with Python + Flask
How to upload with Heroku, Flask, Python, Git (Part 1)
How to upload with Heroku, Flask, Python, Git (Part 2)
Remove headings from multiple format CSV files with python
Read csv with python pandas
Touch Flask + run with Heroku
Create 3d gif with python3
Sorting image files with Python (2)
Sort huge files with python
Sorting image files with Python (3)
Sorting image files with Python
Integrate PDF files with Python
Reading .txt files with Python
Write to csv with Python
Web application with Python + Flask ② ③
Create a directory with python
Download csv file with python
Web application with Python + Flask ④
How to import CSV and TSV files into SQLite with Python
Transpose CSV files in Python Part 1
[Python] Loading csv files using pandas
Make Echolalia LINEbot with Python + heroku
SNS Python basics made with Flask
Create Awaitable with Python / C API
Make apache log csv with python
Create a CSV reader in Flask
Recursively unzip zip files with python
[Python] Write to csv file with Python
Create folders from '01' to '12' with python
Handle csv files with Django (django-import-export)
Output to csv file with Python
Run the app with Flask + Heroku
[Python] POST wav files with requests [POST]
[Python] Use Basic/Digest authentication with Flask
Create a virtual environment with Python!
Create an Excel file with Python3
Decrypt files encrypted with OpenSSL with Python 3
Next, use Python (Flask) for Heroku!
Read files in parallel with Python
Reading and writing CSV with Python
Post bulletin board creation with flask
Application development with Docker + Python + Flask
Create "operation log" CSV formatting tool in 5 days with Python Pandas PyInstaller
Create a "Congratulations 2.0" app that collects congratulatory voices with automatic phone answering with Twilio API x Python (Flask) x Heroku
[AWS] Using ini files with Lambda [Python]
Create a Python function decorator with Class
Play audio files from Python with interrupts
Create wordcloud from your tweet with python3
I tried linebot with flask (anaconda) + heroku
Build a blockchain with Python ① Create a class
Read CSV file with python (Download & parse CSV file)
Create a dummy image with Python + PIL.