Python的web開發
python的web開發有2個很有名的框架,那就是Flask和django,Flask相對於django更加輕便簡潔,非常適合小型網站,話不多說,我們來個hello world!
首先建立一個資料夾webapp。
然後從終端進入webapp資料夾,用我上一篇部落格Python工具之virtualenv說到的工具來建立一個虛擬環境,避免系統環境的干擾。
virtualenv venv
這樣我們便建立了一個虛擬環境,虛擬環境的具體使用就不細說了,請看我上一篇Python工具之virtualenv
接著我們在虛擬環境安裝一下Flask
pip install flask
然後在該資料夾下新建python檔案app.py,檔案結構:
-/webapp
|-venv
-app.py
在app.py輸入:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def index():
return '<h1>Hello World!</h1>'
if __name__ == '__main__':
app.run(debug=True) # 如果你啟用了除錯支援,伺服器會在程式碼修改後自動重新載入,並在發生錯誤時提供一個相當有用的偵錯程式。
好了,就是這麼簡單,接著我們把程式跑起來
python app.py
Running on http://127.0.0.1:5000/
flask預設會在5000埠進行監聽,如果你要修改,修改app.run()方法的引數即可。
# 指定埠和ip
app.run(host='0.0.0.0', port=5000, debug=True)
現在訪問http://127.0.0.1:5000/
你就會看見hello world的網頁了。
就是這麼簡單!
有人就說了,難道我返回的網頁要在字串裡面寫?這當然不可能,Flaks要是隻有這點能力,你怎麼可能還見得到它。
好,接下來,我們返回一個html網頁檔案。
我們在webapp下新建一個資料夾templates,裡面放入hello.html網頁,結構如下
|-/webapp
|-/templates
|-hello.html
|-venv
|-app.py
在hello.html中輸入下面內容
<h1>Hello World!</h1>
接著回到app.py中,新增下面的內容:
from flask import Flask, render_template
app = Flask(__name__, template_folder='templates')
@app.route('/')
def index():
return render_template('hello.html')
if __name__ == '__main__':
app.run(debug=True)
我們馬上來執行該檔案python app.py
,在瀏覽器中輸入http://127.0.0.1:5000/
你就又可以看到偉大的hello world!了。
這裡我再來解釋下上面的程式碼:
# 引入flask,render_template。render_template是用來渲染html檔案的
from flask import Flask, render_template
# template_folder是指定html檔案在哪個資料夾裡面
app = Flask(__name__, template_folder='templates')
# app.route指的是路由,這裡的意思是把根目錄指向index()這個函式,index()即檢視函式
@app.route('/')
def index():
# 渲染並返回模板檔案
return render_template('hello.html')
if __name__ == '__main__':
app.run(debug=True)
相關文章
- Python Web開發PythonWeb
- Java和Python的Web開發JavaPythonWeb
- 學python可以做Web開發嗎?python適合Web開發嗎?PythonWeb
- Python中WEB開發(一)PythonWeb
- 《Flask Web開發 基於Python的Web應用開發實戰》簡評FlaskWebPython
- Python Web開發:從 wsgi 開始PythonWeb
- python web開發-flask中日誌的使用PythonWebFlask
- 10款Web開發最佳的Python框架WebPython框架
- Web | 淺談用Python進行Web開發WebPython
- python怎麼做web開發PythonWeb
- python開發本地WEB專案PythonWeb
- Python web 開發框架 PyramidPythonWeb框架
- 開始使用 Python 開發 Web 應用PythonWeb
- 三個最火的Python Web開發框架PythonWeb框架
- python中web開發框架Django的學習PythonWeb框架Django
- 《Flask Web開發:基於Python的Web應用開發實戰》學習筆記(二)FlaskWebPython筆記
- 什麼是Web開發?如何成為一個Python Web開發人員?WebPython
- 如何理解Python web開發技術PythonWeb
- Python常用的web開發工具,你瞭解多少?PythonWeb
- 10個用於Web開發的最好 Python 框架WebPython框架
- 《Python web開發》筆記 一:網頁開發基礎PythonWeb筆記網頁
- 分分鐘教你Python Web開發框架DjangoPythonWeb框架Django
- 【Python】基於Django Web開發清單PythonDjangoWeb
- Python Web 開發學習 - 第一個Python程式PythonWeb
- Python vs Ruby: 誰是最好的 web 開發語言?PythonWeb
- 基於Python+Django+Jquery架構的Web開發PythonDjangojQuery架構Web
- Web開發的發展史Web
- “Python Web應用開發框架Django速成”公開課PythonWeb框架Django
- Web開發Web
- Python Web開發需要學習什麼?Python基礎!PythonWeb
- 《Python Web開發實戰》隨書原始碼PythonWeb原始碼
- python為什麼不適合web開發PythonWeb
- python web專案開發常見步驟PythonWeb
- Python & PyCharm & Django 搭建web開發環境(續)PythonPyCharmDjangoWeb開發環境
- Python 如何開發高效漂亮的輕量級 Web 應用?PythonWeb
- 使用Python快速開發API和Web的工具:ApiLogicServerPythonAPIWebServer
- .Net Web 開發研習(1) —— Web開發的“十事要說”Web
- Python web離不開的WSGIPythonWeb