python使用Flask框架創(chuàng)建一個(gè)簡(jiǎn)單的動(dòng)態(tài)日歷效果
0. 運(yùn)行效果
運(yùn)行代碼,然后在瀏覽器中訪問 http://127.0.0.1:5000/,將看到一個(gè)動(dòng)態(tài)日歷,能夠通過點(diǎn)擊按鈕切換月份。
1. 安裝 Flask
首先,確保你已經(jīng)安裝了Flask。如果沒有,可以使用以下命令安裝:
pip install Flask
測(cè)試:
from flask import Flask #from flask import Flask, render_template, request app = Flask(__name__) @app.route('/') #@app.route('/', methods=['GET', 'POST']) def hello_world(): return 'Hello, World!' #return render_template('calendar.html') # 僅在開發(fā)環(huán)境中使用 if __name__ == '__main__': app.run(debug=True) # 僅用于開發(fā)環(huán)境
打開 http://127.0.0.1:5000 ,可看到helloworld
2. 項(xiàng)目結(jié)構(gòu)
/項(xiàng)目目錄 ├── app.py ├── static │ └── bg.jpg # 確保你將背景圖放在這里 └── templates └── calendar.html
3. 創(chuàng)建 Flask 應(yīng)用
接下來,創(chuàng)建一個(gè)名為 app.py 的文件。
導(dǎo)入必要的庫(kù):
from flask import Flask, render_template, request import calendar from datetime import datetime
其中,render_template: 用于渲染 HTML 模板。request: 用于處理 HTTP 請(qǐng)求數(shù)據(jù)。calendar: Python 的日歷模塊,用于生成日歷。datetime: 用于獲取當(dāng)前日期和時(shí)間。
創(chuàng)建 Flask 應(yīng)用,__name__
表示當(dāng)前模塊的名稱。
app = Flask(__name__)
定義路由
@app.route('/', methods=['GET', 'POST'])
使用 datetime.now() 獲取當(dāng)前時(shí)間,并提取出當(dāng)前的年月份。
now = datetime.now() # Get current date and time year = now.year month = now.month
表單提交,如果請(qǐng)求是 POST 方法(通常是表單提交),則從表單中獲取用戶選擇的月份和年份。根據(jù)用戶的操作(前一個(gè)月或下一個(gè)月),更新 month 和 year 變量。
if request.method == 'POST': month = int(request.form.get('month')) year = int(request.form.get('year')) if request.form.get('action') == 'prev': if month == 1: month = 12 year -= 1 else: month -= 1 elif request.form.get('action') == 'next': if month == 12: month = 1 year += 1 else: month += 1
生成日歷,渲染html模板
cal = calendar.monthcalendar(year, month) month_year = f"{year}年{month}月" return render_template('calendar.html', calendar=cal, month_year=month_year, year=year, month=month, now=now)
代碼如下:
from flask import Flask, render_template, request import calendar from datetime import datetime app = Flask(__name__) @app.route('/', methods=['GET', 'POST']) def index(): now = datetime.now() # Get current date and time year = now.year month = now.month if request.method == 'POST': month = int(request.form.get('month')) year = int(request.form.get('year')) if request.form.get('action') == 'prev': if month == 1: month = 12 year -= 1 else: month -= 1 elif request.form.get('action') == 'next': if month == 12: month = 1 year += 1 else: month += 1 # Generate calendar cal = calendar.monthcalendar(year, month) month_year = f"{year}年{month}月" return render_template('calendar.html', calendar=cal, month_year=month_year, year=year, month=month, now=now) if __name__ == '__main__': app.run(debug=True)
4. 創(chuàng)建 HTML 模板
在與 app.py 相同的目錄下,創(chuàng)建一個(gè)名為 templates 的文件夾,并在其中創(chuàng)建一個(gè)名為 calendar.html 的文件,添加代碼:
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8"> <title>動(dòng)態(tài)日歷</title> <style> body { background: url('/static/bg.jpg') no-repeat center center fixed; background-size: cover; margin: 0; padding: 20px; font-family: Arial, sans-serif; } .calendar-container { position: absolute; top: 50px; left: 50px; width: 300px; border: 2px solid white; border-radius: 10px; background: rgba(255, 255, 255, 0.9); box-shadow: 0 0 10px rgba(0, 0, 0, 0.3); padding: 10px; } table { width: 100%; border-collapse: collapse; } th, td { text-align: center; padding: 10px; border: 1px solid #ddd; } .today { background-color: red; color: white; } .header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px; } .button { cursor: pointer; font-size: 1.2em; padding: 5px 10px; background: lightgray; border: none; border-radius: 5px; } </style> </head> <body> <div class="calendar-container"> <div class="header"> <form method="post" style="display: inline;"> <input type="hidden" name="month" value="{{ month }}"> <input type="hidden" name="year" value="{{ year }}"> <button class="button" name="action" value="prev">◀</button> </form> <span>{{ month_year }}</span> <form method="post" style="display: inline;"> <input type="hidden" name="month" value="{{ month }}"> <input type="hidden" name="year" value="{{ year }}"> <button class="button" name="action" value="next">▶</button> </form> </div> <table> <tr> <th>日</th> <th>一</th> <th>二</th> <th>三</th> <th>四</th> <th>五</th> <th>六</th> </tr> {% for week in calendar %} <tr> {% for day in week %} {% if day == 0 %} <td></td> {% else %} <td {% if day == now.day and year == now.year and month == now.month %}class="today"{% endif %}>{{ day }}</td> {% endif %} {% endfor %} </tr> {% endfor %} </table> </div> </body> </html>
背景圖路徑設(shè)置,F(xiàn)lask 會(huì)自動(dòng)為 static 目錄中的文件處理 URL,因此可以這樣引用它:
background: url('/static/bg.jpg');
到此這篇關(guān)于python使用Flask框架創(chuàng)建一個(gè)簡(jiǎn)單的動(dòng)態(tài)日歷效果的文章就介紹到這了,更多相關(guān)python Flask動(dòng)態(tài)日歷內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python實(shí)現(xiàn)接口自動(dòng)化封裝導(dǎo)出excel和讀寫excel數(shù)據(jù)
這篇文章主要為大家詳細(xì)介紹了Python如何實(shí)現(xiàn)接口自動(dòng)化封裝導(dǎo)出excel和讀寫excel數(shù)據(jù),文中的示例代碼簡(jiǎn)潔易懂,希望對(duì)大家有所幫助2023-07-07Django中celery的使用項(xiàng)目實(shí)例
Celery是?個(gè) 基于python開發(fā)的分布式異步消息任務(wù)隊(duì)列,通過它可以輕松的實(shí)現(xiàn)任務(wù)的異步處理,下面這篇文章主要給大家介紹了關(guān)于Django中celery使用的相關(guān)資料,需要的朋友可以參考下2022-07-07Python實(shí)現(xiàn)四舍五入的兩個(gè)方法總結(jié)
這篇文章主要介紹了python中實(shí)現(xiàn)四舍五入的兩種方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-09-09Python實(shí)現(xiàn)數(shù)據(jù)結(jié)構(gòu)線性鏈表(單鏈表)算法示例
這篇文章主要介紹了Python實(shí)現(xiàn)數(shù)據(jù)結(jié)構(gòu)線性鏈表(單鏈表)算法,結(jié)合實(shí)例形式分析了Python單鏈表的定義、節(jié)點(diǎn)插入、刪除、打印等相關(guān)操作技巧,需要的朋友可以參考下2019-05-054種非常實(shí)用的python內(nèi)置數(shù)據(jù)結(jié)構(gòu)
這篇文章主要介紹了4種非常實(shí)用的python內(nèi)置數(shù)據(jù)結(jié)構(gòu),幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下2021-04-04