Perintah dasar dan
pemahaman dasar web python
Contoh program python
tanpa web design(html) :
from flask import Flask #import
library flask
app = Flask(__name__) #variabel package menyimpan file aplikasi
@app.route('/') #rute
penghubung ke url khususnya index
def hello(): #fungsi
hello
return 'hello kita
coba string python, coba lagi' # mengembalikan fungsi proses fungsi hello
@app.route('/coba')
def coba():
return 'kita coba
halaman selanjutnya'
menjalankan program di cmd windows:
1.
Membuat environment
> py -3 -m venv venv
2.
Mengaktifkan environment
> venv\Scripts\activate
3.
Install
Flask
> pip install Flask
4.
export file
python ke flask
>set FLASK_APP=web1.py
5.
memunculkan
bug di browser dan reload otomatis
>set FLASK_ENV=development
Jalankan program dengan flask run
>flask run
Tampilan
lengkapnya setelah virtual environtment di buat dan di install flask:
C:\>venv\Scripts\activate
(venv) C:\>cd python
(venv) C:\python>set
FLASK_APP=web1.py
(venv) C:\python>set
FLASK_UNV=development
(venv) C:\python>py web1.py
(venv) C:\python>flask run
* Serving Flask app "web1.py"
* Environment: production
WARNING: This is a development server. Do not use it in a production
deployment.
Use a production WSGI server instead.
* Debug mode: off
* Running on http://127.0.0.1:5000/ (Press
CTRL+C to quit)
127.0.0.1 - - [30/Jun/2020 11:37:52]
"[37mGET / HTTP/1.1[0m" 200 -
127.0.0.1 - - [30/Jun/2020 11:37:52]
"[33mGET /favicon.ico HTTP/1.1[0m" 404 -
127.0.0.1 - - [30/Jun/2020 11:38:24]
"[37mGET /coba HTTP/1.1[0m" 200 -
Lalu http://127.0.0.1:5000/ masukkan ip ini ke browser sehingga hasilnya :
dan ini..
0 Comments