[PYTHON] Use Flask to run external files

What i did

Create a web page in Flask. Click the button to execute an external scraping file.

Flask preparation

Flask installation

pip install Flask

Create the original file

root.py


from flask import Flask

app = Flask(__name__)


@app.route('/')
def hello():
    return 'Hello!'


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

Run

python root.py

afterwards

 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

Is output, so access http://127.0.0.1:5000/.

スクリーンショット (57).png

Hello! Is displayed.

Write HTML using the template engine "Jinja2"

Add import etc.

root.py


# from flask import Flask
#Add ↓
from flask import Flask, render_template

app = Flask(__name__)


@app.route('/')
def hello():
  #return 'Hello!'
  #Add ↓
  return render_template('layout.html', title='Scraping App')


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

Create a templates folder and create layout.html in it. Make sure to send the GET method by clicking the button tag.

layout.html


<!doctype html>
<html>

<head>
  <!--       ↓ render_Contains the title written in the template--> 
  <title>{{ title }}</title>
</head>

<body>
  <div class="member">
    <img src="/static/img/akimoto.jpg " alt="img1">
    <h2>Midsummer Akimoto</h2>
    <form method="GET" action="/scraping">
      <button type="submit">Start Scraping</button>
    </form>
  </div>
</body>

</html>

Create a static folder, create css and img inside, and arrange the appearance.

Final file contents

root.py


from flask import Flask, render_template
#↓ Import the file you want to execute through Flask
import scraping

app = Flask(__name__)


@app.route('/')
def hello():
    return render_template('layout.html', title='Scraping App')

# ↓ /Processing when scraping is received by GET method
@app.route('/scraping')
def get():
    #↓ Function of the file you want to execute
    return scraping.scraping()


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

layout.html


<!doctype html>
<html>

<head>
  <title>{{ title }}</title>
  <link rel="stylesheet" href="/static/css/index.css">
</head>

<body>
  <div class="member">
    <img src="/static/img/akimoto.jpg " alt="img1">
    <h2>Midsummer Akimoto</h2>
    <form method="GET" action="/scraping">
      <button type="submit">Start Scraping</button>
    </form>
  </div>
</body>

</html>

↓ The scraping file executed this time Get images of Nogizaka46 blog by scraping

Final folder structure

Display screen

スクリーンショット (55).png

After clicking the button, the created folder

スクリーンショット (56).png

You can now run Python files from the console through a web page!

Recommended Posts

Use Flask to run external files
Preparing to run Flask on EC2
Minimum knowledge to use Form in Flask
Don't use rm command to delete files
Use boto to upload / download files to s3.
How to use TouchDesigner Python's external module
Read CSV files uploaded to Flask without saving
How to use xml.etree.ElementTree
How to use Python-shell
How to use tf.data
How to use virtualenv
How to use Seaboan
How to use image-match
How to use Pandas 2
How to use Virtualenv
How to use pytest_report_header
Include and use external Kv files in Python Kivy
How to use Bio.Phylo
How to use SymPy
How to use x-means
How to use WikiExtractor.py
How to use IPython
How to use virtualenv
How to use Matplotlib
How to use variables in systemd Unit definition files
How to use iptables
How to use numpy
Reasons to use logarithm
How to use TokyoTechFes2015
How to use venv
How to use dictionary {}
How to use Pyenv
Easy to use SQLite3
How to use list []
How to use python-kabusapi
Python-How to use pyinstaller
How to use OptParse
Save lists, dictionaries and tuples to external files python
How to use return
How to use dotenv
Run Apache2 + WSGI + Flask
How to use pyenv-virtualenv
How to use Go.mod
How to use imutils
How to use import
Understanding how to use Jinja2 makes development with Flask smarter
[Python] Use this to read and write wav files [wavio]
How to use Qt Designer
How to use search sorted
[gensim] How to use Doc2Vec
python3: How to use bottle (2)
Understand how to use django-filter
Use MeCab to fetch readings
[Python] How to use list 1
QSM analysis-How to use MEDI-
How to use FastAPI ③ OpenAPI
How to use Python argparse
How to use IPython Notebook
How to use Pandas Rolling
[Note] How to use virtualenv
How to use redis-py Dictionaries