rtnF

Flask

Pertama, install Flask

pip install flask

Collecting flask
Downloading flask-3.0.0-py3-none-any.whl.metadata (3.6 kB)
Collecting Werkzeug>=3.0.0 (from flask)
Collecting Jinja2>=3.1.2 (from flask)
Collecting itsdangerous>=2.1.2 (from flask)
Requirement already satisfied: click>=8.1.3
Collecting blinker>=1.6.2 (from flask)
Collecting importlib-metadata>=3.6.0 (from flask)
Requirement already satisfied: colorama
Collecting zipp>=0.5 (from importlib-metadata>=3.6.0->flask)
Collecting MarkupSafe>=2.0 (from Jinja2>=3.1.2->flask)
Installing collected packages: zipp, MarkupSafe, itsdangerous, blinker, Werkzeug, Jinja2, importlib-metadata, flask
Successfully installed Jinja2-3.1.2 MarkupSafe-2.1.3 Werkzeug-3.0.1 blinker-1.7.0 flask-3.0.0 importlib-metadata-7.0.0 itsdangerous-2.1.2 zipp-3.17.0

Lalu, install Flask MySQLDB

pip install flask-mysqldb

Collecting flask-mysqldb
Requirement already satisfied: Flask>=1.0.4
Collecting mysqlclient>=2.2.0 (from flask-mysqldb)
Requirement already satisfied: Werkzeug>=3.0.0 
Requirement already satisfied: Jinja2>=3.1.2 
Requirement already satisfied: itsdangerous>=2.1.2 
Requirement already satisfied: click>=8.1.3 
Requirement already satisfied: blinker>=1.6.2
Requirement already satisfied: importlib-metadata>=3.6.0 
Requirement already satisfied: colorama 
Requirement already satisfied: zipp>=0.5
Requirement already satisfied: MarkupSafe>=2.0 
Installing collected packages: mysqlclient, flask-mysqldb
Successfully installed flask-mysqldb-2.0.0 mysqlclient-2.2.0

Lalu, nyalakan MySQLnya. Buka XAMPP Control Panel, tekan tombol start pada MySQL.

Kalau warnanya sudah hijau, MySQLnya sudah aktif, dalam bentuk daemon di background. Kita perlu frontend khusus untuk mengakses MySQL yang masih berupa "backend" ini.

Saya pribadi biasa pakai HeidiSQL.

Buka HeidiSQL. Pilih network type "MariaDB or MySQL", lalu masukan username + password MySQL di XAMMP tersebut.

Saya sendiri sempat lupa dengan passwordnya. Setelah beberapa kali nebak-nebak, akhirnya ketemu juga : username root, tanpa password.

Lewat HeidiSQL, kita bisa membuat database dan tabel MySQL secara nyaman pakai GUI. Langkah selanjutnya, kita buat database baru sekadar untuk testing saja : database "test" dengan tabel "profil" yang berisi kolom "nama" dan "alamat".

Ku pakai tutorial ini sebagai acuan. Tujuan utamanya adalah sekedar memastikan bahwa Flask-nya bisa terkoneksi dengan MySQL dengan baik. Itu saja.

Buat file python baru, namakan "c.py", isinya seperti ini :

from flask import Flask, render_template, request
from flask_mysqldb import MySQL
app = Flask(__name__)
app.config['MYSQL_HOST'] = 'localhost'
app.config['MYSQL_USER'] = 'root'
app.config['MYSQL_DB'] = 'test'
mysql = MySQL(app)
@app.route('/', methods=['GET', 'POST'])
def index():
    if request.method == "POST":
        details = request.form
        Nama = details['name']
        Alamat = details['address']
        cur = mysql.connection.cursor()
        cur.execute("INSERT INTO profil(nama, alamat) VALUES (%s, %s)", (Nama, Alamat))
        mysql.connection.commit()
        cur.close()
        return 'sukses'
    return render_template('index.html')
if __name__ == '__main__':
    app.run()

Di sebelah file "c.py", buat folder "templates". Di dalam folder tersebut, buat file "index.html". Lalu isi file html tersebut dengan kode ini.

<HTML>
<BODY bgcolor="white">
<form method="POST" action="">
    <center>
    <H1> Profil Konsumen </H1> <br>
    Nama <input type = "text" name= "name" /> <br>
    Alamat <input type = "text" name = "address" /> <br>
    <input type = "submit">
    </center>
</form>
</BODY>
</HTML>

Lalu, kembali buka file c.py, dan jalankan file pythonnya. (Karena saya pakai Sublime Text, jadinya tinggal tekan Ctrl + B saja)

 * Serving Flask app 'c'
 * Debug mode: off
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
 * Running on http://127.0.0.1:5000
Press CTRL+C to quit

Buka http://127.0.0.1:5000 di Browser. Kalau tidak ada error, akan muncul sebuah form. Masukkan teks bebas di kolom nama dan alamat, tekan submit. Kalau tidak ada error, akan muncul pesan "sukses".

Lalu cek isi database MySQL menggunakan HeidiSQL. Jika datanya masuk, berarti berhasil. Koneksi antara Flask dengan MySQL sudah terhubung dengan baik dan benar.


Epilogue : The History of Flask

WSGI is the Web Server Gateway Interface. It is a specification that describes how a web server communicates with web applications, and how web applications can be chained together to process one request. WSGI was originally specified as PEP-333 in 2003.

Aaron Swartz started working on Web.py in 2005. Aaron wanted a faster and simpler framework for python web applications. In 2005, while Aaron was working at Reddit.com, he used Web.py to build Reddit.com.

simplejson is a simple, fast, complete, correct and extensible JSON encoder and decoder for Python 3.3+ with legacy support for Python 2.5+. It is pure Python code with no dependencies, but includes an optional C extension for a serious speed boost. (2006)

Werkzeug is a comprehensive WSGI web application library. It began as a simple collection of various utilities for WSGI applications. (2007)

Jinja is a fast, expressive, extensible templating engine. Special placeholders in the template allow writing code similar to Python syntax. Then the template is passed data to render the final document. (2008)

"It seems like everybody likes microframeworks. Not sure what caused that, but there are plenty of them. web.py (Python) and camping (Ruby) where the first of their kind I think. Later others followed and it seemed that people love the idea of software that does not have dependencies and comes in a single file. So I thought, I can do the same and make fun of it, so let's just create a framework based on existing technology and throw everything together in a large single file: denied was born. I just bundled a Werkzeug, simplejson and Jinja2 into a single file and added a bit of code that glues them together." (2011)