Programming with Python Flask

Programming in python

Preparation test data

Use drivers.csv for testing https://github.com/noikedan/flask_app/tree/develop

Deliverables

http://54.199.145.235/

background

Convert the Insert creation tool made with Python to the Web. Reason: It is difficult to use with the current one.

Target users (example)

I'm making a web service for portfolio creation at home Those who want to use Insert Creation conveniently as a tool for data creation.

Non-target users

People who think that it is 0 if it can not be used in the field. A person who comes to the developer by saying a review or a customer. A person who comes impulsively every day saying only progress. People who only make complaints.

INPUT Python processing (GITHUB)

https://github.com/noikedan/INSERTSQL/tree/master/pythonInsrtSql

draft

-Upload the file. -Create SQL. -Write to a file. -Download the file.

environment

FLASK The SQL statement assumes postgresql.

Investigation

The file upload method will follow the address below. https://flask.palletsprojects.com/en/1.1.x/quickstart/

image

スクリーンショット 2020-07-07 5.02.00.png スクリーンショット 2020-07-07 5.02.50.png

Output source

https://github.com/noikedan/flask_app/tree/develop

Source code

Index.html
<html>
  <head>
    <tilte>Insert statement creation tool</tilte>
  </head>
  <body>
    <form method="post" action="/todos/uploader" enctype = "multipart/form-data">
      <input type="file" name="file" />
      <input type="submit" value="Create" />
    </form>
    <p>
	<a href="{{ url_for('.download_file') }}">Download</a>
    </p>
  </body>
</html>
InsertApp.py
from flask import Flask, render_template
from flask import request,send_file
app = Flask(__name__)

@app.route('/todos/uploader', methods=['GET', 'POST'])
def upload_file():
    if request.method == 'POST':
        f = request.files['file']
        f.save(f.filename)
        input = './' + f.filename
        output = './output.txt'

        table = input.split('/')[-1].split('.')[0]
        with open(input, encoding='utf-8') as f:
            with open(output, 'w', encoding='utf-8') as g:
                contents = "Insert into " + table + "("
                i = 0
                for row in f:
                    if i == 0:
                        typeList = row.rstrip().split(',')
                    if i == 1:
                        columList = row.rstrip().split(',')
                        k = 0
                        for c in columList:
                            if len(columList) == k + 1:
                                contents = contents + c + 'VALUES ('
                            else:
                                contents = contents + c + ','
                            k = k + 1
                        basecontets = contents
                    if i >= 2:
                        j = 0
                        for r in row.rstrip().split(','):
                            if not 'INTEGER' in typeList[j]:
                                r = "'" + r + "'"
                            if len(row.rstrip().split(',')) == j + 1:
                                basecontets = basecontets + r
                            else:
                                basecontets = basecontets + r + ','
                            j = j + 1
                        basecontets = basecontets + ');' + '\n'
                        g.write(basecontets)
                        basecontets = contents
                    i = i + 1
            print("Creation completed")
    return render_template('index.html')

@app.route('/download')
def download_file():

	path = './output.txt'
	return send_file(path, as_attachment=True)



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

Recommended Posts

Programming with Python Flask
3. 3. AI programming with Python
Python programming with Atom
Competitive programming with python
Programming with Python and Tkinter
Web application with Python + Flask ② ③
Network programming with Python Scapy
Web application with Python + Flask ④
SNS Python basics made with Flask
[Python] Object-oriented programming learned with Pokemon
[Python] Use Basic/Digest authentication with Flask
Easy Python + OpenCV programming with Canopy
Application development with Docker + Python + Flask
FizzBuzz with Python3
Python programming note
Scraping with Python
Statistics with python
Scraping with Python
Python with Go
Integrate with Python
AES256 with python
Tested with Python
Programming in python
python starts with ()
with syntax (Python)
Bingo with python
Zundokokiyoshi with python
Excel with Python
Microcomputer with Python
Cast with python
Competitive programming with python Local environment settings
POST variously with Python and receive with Flask
Page cache in Python + Flask with Flask-Caching
Easy web app with Python + Flask + Heroku
Serial communication with Python
Zip, unzip with python
Primality test with Python
Python with eclipse + PyDev.
Socket communication with Python
Data analysis with python 2
Scraping with Python (preparation)
Try scraping with Python.
Sequential search with Python
"Object-oriented" learning with python
I made a competitive programming glossary with Python
How to enjoy programming with Minecraft (Ruby, Python)
Run Python with VBA
Solve AtCoder 167 with python
Serial communication with python
[Python] Use JSON with Python
Learn Python with ChemTHEATER
Run prepDE.py with python3
Collecting tweets with Python
Kernel Method with Python
Non-blocking with Python + uWSGI
Scraping with Python + PhantomJS
Vienna with Python + Flask web app on Jenkins
Posting tweets with python
Drive WebDriver with python
Use mecab with Python3
[Python] Redirect with CGIHTTPServer