[PYTHON] Create a CSV reader in Flask

Last time I converted PDF to text, so this time I will create a CSV reader. The code itself is a file upload, so it looks almost the same.

csvopnener.py


import os
import csv
from flask import Flask, render_template, request, redirect, url_for, send_from_directory, session
from werkzeug.utils import secure_filename
app = Flask(__name__)

UPLOAD_FOLDER = './uploads'
ALLOWED_EXTENSIONS = set(['csv'])
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['SECRET_KEY'] = os.urandom(24)

def allowed_file(filename):
    return '.' in filename and \
        filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS

@app.route('/')
def index():
    return render_template('csv.html')


@app.route('/show_csv', methods=['GET', 'POST'])
def show_csv():
    if request.method == 'POST':
        send_data = request.files['send_data']
        if send_data and allowed_file(send_data.filename):
            filename = secure_filename(send_data.filename)
            send_data.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
            f = open('uploads/' + filename, 'r')
            f_reader = csv.reader(f)
            result = list(f_reader)

            return render_template('csv.html', result=result)


@app.route('/uploads/<filename>')
def uploaded_file(filename):
    return send_from_directory(app.config['UPLOAD_FOLDER'], filename)

if __name__ == '__main__':
    app.debug = True
    app.run()

csv.html


{% extends "base.html" %}
{% block content %}
<form method="post" action="{{ url_for('show_csv') }}" enctype="multipart/form-data">
  <input type="file" id="send_data" name="send_data">
  <input type="submit" value="Send">
</form>
<div>
  {% if result %}
  <dl>
  {% for i in result %}
    <dt>{{ i[0] }}<dt>
    <dd>{{ i[1] }}</dd>
  {% endfor %}
  </dl>
  {% endif %}
</div>
{% endblock %}

Run

スクリーンショット 2017-06-06 12.24.55.png

I prepared a CSV file for such a test.

スクリーンショット 2017-06-06 12.25.30.png

I felt that it would be nice to add a line to CSV if I customized it.

Recommended Posts

Create a CSV reader in Flask
Create a function in Python
Create a dictionary in Python
Create a DI Container in Python
Create a binary file in Python
Create a Kubernetes Operator in Python
Create a random string in Python
Create a LINE Bot in Django
Create a simple GUI app in Python
Create a JSON object mapper in Python
Create a simple web app with flask
Create a Python-GUI app in Docker (PySimpleGUI)
Launch a Flask app in Python Anywhere
[GPS] Create a kml file in Python
Create a web service with Docker + Flask
Create and deploy Flask apps in PTVS
Create a web service in Flask-SQLAlchemy + PostgreSQL
Csv in python
Create a Vim + Python test environment in 1 minute
Create a GIF file using Pillow in Python
Create an executable file in a scripting language
I want to create a window in Python
Create a standard normal distribution graph in Python
How to create a JSON file in Python
Create a virtual environment with conda in Python
Create a bulletin board with Heroku, Flask, SQLAlchemy
Create a protein sequence mutation library in pandas
Create a custom search command in Splunk (Streaming Command)
Create a simple momentum investment model in Python
Create a new page in confluence with Python
Create a datetime object from a string in Python (Python 3.3)
Create a package containing global commands in Python
How to create a Rest Api in Django
Until you create a new app in Django
Create a MIDI file in Python using pretty_midi
Create a loop antenna pattern in Python in KiCad
[Docker] Create a jupyterLab (python) environment in 3 minutes!
Create a web server in Go language (net/http) (2)
Create a filter to get an Access Token in the Graph API (Flask)
Create a data collection bot in Python using Selenium
I want to transition with a button in flask
Create a Python module
Create SpatiaLite in Python
[LINE Messaging API] Create a rich menu in Python
Create a plugin to run Python Doctest in Vim (2)
Create a plugin to run Python Doctest in Vim (1)
dict in dict Makes a dict a dict
In Python, create a decorator that dynamically accepts arguments Create a decorator
Create a Bootable LV
Create a web server in Go language (net / http) (1)
Create a Python environment
Python script to create a JSON file from a CSV file
Create a fake Minecraft server in Python with Quarry
2 ways to read all csv files in a folder
Image uploader in Flask
Create a slack bot
Create a new csv with pandas based on the local csv
Create a plugin that always highlights arbitrary text in Sublime Text 2
Create a local scope in Python without polluting the namespace
Create a list in Python with all followers on twitter
Created a reading record book in conjunction with PostgreSQL in Flask