自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

手把手教你使用Python打造一款摸魚倒計界面

開發(fā) 后端
前段時間在微博看到一段摸魚人的倒計時模板,感覺還挺有趣的。于是我用了一小時的時間寫了個頁面出來 摸魚辦地址 (當(dāng)然是摸魚的時間啦)。

[[442972]]

大家好,我是吳老板。

前言

前段時間在微博看到一段摸魚人的倒計時模板,感覺還挺有趣的。

于是我用了一小時的時間寫了個頁面出來 摸魚辦地址 (當(dāng)然是摸魚的時間啦)。

模板是這樣的:

摸魚辦公室 🐟

【摸魚辦公室】今天是 2021-11-30 星期二

你好,摸魚人,工作再累,一定不要忘記摸魚哦 ! 有事沒事起身去茶水間去廊道去天臺走走,別老在工位上坐著。多喝點(diǎn)水,錢是老板的,但命是自己的 !

🐟 距離 周末 放假還有 2 天

🐟 距離 元旦 放假還有 3 天

🐟 距離 過年 放假還有 34 天

🐟 距離 清明節(jié) 放假還有 97 天

🐟 距離 勞動節(jié) 放假還有 123 天

🐟 距離 端午節(jié) 放假還有 156 天

🐟 距離 中秋節(jié) 放假還有 255 天

🐟 距離 國慶節(jié) 放假還有 276 天

  • 由于前端是單頁面服務(wù),直接擼一個原始的 html 網(wǎng)頁就行。
  • FastAPI 對于異步請求是一把好手、更輕、性能更佳。
  • 掛上一層 Nginx 讓它看起來像那么回事兒。

實現(xiàn)過程

  • 首先要知道、除了靜態(tài)文字之外的比如當(dāng)前日期、距離節(jié)日放假的天數(shù)等都是動態(tài)返回的,我需要使用 Jinja2 模板進(jìn)行動態(tài)綁定。
  • 我應(yīng)該把重點(diǎn)放在時間的處理上。
  • 而且在這個模板中,有陽歷的節(jié)日,也是陰歷的節(jié)日,我需要轉(zhuǎn)換。

初始化一個 FastAPI 對象并聲明靜態(tài)頁面的模板目錄 (Jinja2Templates)

  1. # -*- coding: utf-8 -*- 
  2. import datetime 
  3. from fastapi import FastAPI, Request 
  4. from fastapi.responses import HTMLResponse 
  5. from fastapi.templating import Jinja2Templates 
  6. from zhdate import ZhDate as lunar_date 
  7.  
  8. app = FastAPI( 
  9.     debug=False
  10.     title="My API"
  11.     docs_url="/docs"
  12.     openapi_url=f"/openapi.json" 
  13.  
  14. templates = Jinja2Templates(directory="templates"

可以看到的是我用到了 zhdate 這個庫、主要用于陰歷和陽歷之間的相互轉(zhuǎn)換。用法如下

  1. today = datetime.date.today() 
  2. print(today.year, today.month, today.day
  3. print("大年時間: ", lunar_date(today.year+1, 1, 1).to_datetime().date()) 
  4. print("端午時間: ", lunar_date(today.year, 5, 5).to_datetime().date()) 
  5. print("中秋時間: ", lunar_date(today.year, 8, 15).to_datetime().date()) 
  6. print("元旦時間: ", f"{today.year+1}-01-01"
  7. print("清明時間: ", f"{today.year}-04-05"
  8. print("勞動時間: ", f"{today.year}-05-01"
  9. print("國慶時間: ", f"{today.year}-10-01"

我們可以梳理一下:

  • 計算距離 大年、元旦 的天數(shù)時,要在年份上 +1
  • 計算距離 其他節(jié)日 的天數(shù)時,要判斷天數(shù)差是否小于 0,如果是,則年份需要 +1,因為已經(jīng)過去的節(jié)日對此沒有意義
  1. distance_big_year = (lunar_date(today.year + 1, 1, 1).to_datetime().date() - today).days 
  2.  
  3. distance_5_5 = (lunar_date(today.year, 5, 5).to_datetime().date() - today).days 
  4. distance_5_5 = distance_5_5 if distance_5_5 > 0 else ( 
  5.         lunar_date(today.year + 1, 5, 5).to_datetime().date() - today).days 
  6.  
  7. distance_8_15 = (lunar_date(today.year, 8, 15).to_datetime().date() - today).days 
  8. distance_8_15 = distance_8_15 if distance_8_15 > 0 else ( 
  9.         lunar_date(today.year + 1, 8, 15).to_datetime().date() - today).days 
  10.  
  11. distance_year = (datetime.datetime.strptime(f"{today.year + 1}-01-01""%Y-%m-%d").date() - today).days 
  12.  
  13. distance_4_5 = (datetime.datetime.strptime(f"{today.year}-04-05""%Y-%m-%d").date() - today).days 
  14. distance_4_5 = distance_4_5 if distance_4_5 > 0 else ( 
  15.         datetime.datetime.strptime(f"{today.year + 1}-04-05""%Y-%m-%d").date() - today).days 
  16.  
  17. distance_5_1 = (datetime.datetime.strptime(f"{today.year}-05-01""%Y-%m-%d").date() - today).days 
  18. distance_5_1 = distance_5_1 if distance_5_1 > 0 else ( 
  19.         datetime.datetime.strptime(f"{today.year + 1}-05-01""%Y-%m-%d").date() - today).days 
  20.  
  21. distance_10_1 = (datetime.datetime.strptime(f"{today.year}-10-01""%Y-%m-%d").date() - today).days 
  22. distance_10_1 = distance_10_1 if distance_10_1 > 0 else ( 
  23.         datetime.datetime.strptime(f"{today.year + 1}-10-01""%Y-%m-%d").date() - today).days 

怎么樣? 我的命名足夠瘋狂吧。

接下來需要計算一下距離周末的天數(shù)。

  1. def get_week_day(date): 
  2.     week_day_dict = { 
  3.         0: '星期一'
  4.         1: '星期二'
  5.         2: '星期三'
  6.         3: '星期四'
  7.         4: '星期五'
  8.         5: '星期六'
  9.         6: '星期天'
  10.     } 
  11.     day = date.weekday() 
  12.     return week_day_dict[day
  13.  
  14. week_day_ = get_week_day(today) 
  15. print(f"今天是: {week_day_}") # 先獲取今天是星期幾 

按照每周 5 個工作日計算,今天距離周末的天數(shù)就是

  1. 5 - today.weekday() # today.weekday() 今天距離周末 

現(xiàn)在將所有的數(shù)據(jù)組裝起來

  1. time_ = [ 
  2.     {"v_": distance_year, "title""元旦"},  # 距離元旦 
  3.     {"v_": distance_big_year, "title""過年"},  # 距離過年 
  4.     {"v_": distance_4_5, "title""清明節(jié)"},  # 距離清明 
  5.     {"v_": distance_5_1, "title""勞動節(jié)"},  # 距離勞動 
  6.     {"v_": distance_5_5, "title""端午節(jié)"},  # 距離端午 
  7.     {"v_": distance_8_15, "title""中秋節(jié)"},  # 距離中秋 
  8.     {"v_": distance_10_1, "title""國慶節(jié)"},  # 距離國慶 

至于為什么是 List 而不是 Dict,那是我需要做一個根據(jù)距離天數(shù)的排序,讓最先放假的節(jié)日放于最前面, 這樣看起來會舒服得多。

  1. time_ = sorted(time_, key=lambda x: x['v_'], reverse=False

接下來要寫一個 路由,將數(shù)據(jù)傳入到 html 頁面中去。

  1. @app.get("/", response_class=HTMLResponse) 
  2. async def readme(request: Request): 
  3.     return templates.TemplateResponse("readme.html"
  4.                                       {"request": request, "time_": time_, "now_": now_, "week_day_": week_day_}) 

來看一下完整的代碼 (main.py):

  1. # -*- coding: utf-8 -*- 
  2. import datetime 
  3. from fastapi import FastAPI, Request 
  4. from fastapi.responses import HTMLResponse 
  5. from fastapi.templating import Jinja2Templates 
  6. from zhdate import ZhDate as lunar_date 
  7.  
  8. app = FastAPI( 
  9.     debug=False
  10.     title="My API"
  11.     docs_url=f"/docs"
  12.     openapi_url=f"/openapi.json" 
  13.  
  14. templates = Jinja2Templates(directory="templates"
  15.  
  16. today = datetime.date.today() 
  17.  
  18. # print(today.year, today.month, today.day
  19. # print("大年時間: ", lunar_date(today.year+1, 1, 1).to_datetime().date()) 
  20. # print("端午時間: ", lunar_date(today.year, 5, 5).to_datetime().date()) 
  21. # print("中秋時間: ", lunar_date(today.year, 8, 15).to_datetime().date()) 
  22. # print("元旦時間: ", f"{today.year+1}-01-01"
  23. # print("清明時間: ", f"{today.year+1}-04-05"
  24. # print("勞動時間: ", f"{today.year+1}-05-01"
  25. # print("國慶時間: ", f"{today.year+1}-10-01"
  26.  
  27. distance_big_year = (lunar_date(today.year + 1, 1, 1).to_datetime().date() - today).days 
  28.  
  29. distance_5_5 = (lunar_date(today.year, 5, 5).to_datetime().date() - today).days 
  30. distance_5_5 = distance_5_5 if distance_5_5 > 0 else ( 
  31.         lunar_date(today.year + 1, 5, 5).to_datetime().date() - today).days 
  32.  
  33. distance_8_15 = (lunar_date(today.year, 8, 15).to_datetime().date() - today).days 
  34. distance_8_15 = distance_8_15 if distance_8_15 > 0 else ( 
  35.         lunar_date(today.year + 1, 8, 15).to_datetime().date() - today).days 
  36.  
  37. distance_year = (datetime.datetime.strptime(f"{today.year + 1}-01-01""%Y-%m-%d").date() - today).days 
  38.  
  39. distance_4_5 = (datetime.datetime.strptime(f"{today.year}-04-05""%Y-%m-%d").date() - today).days 
  40. distance_4_5 = distance_4_5 if distance_4_5 > 0 else ( 
  41.         datetime.datetime.strptime(f"{today.year + 1}-04-05""%Y-%m-%d").date() - today).days 
  42.  
  43. distance_5_1 = (datetime.datetime.strptime(f"{today.year}-05-01""%Y-%m-%d").date() - today).days 
  44. distance_5_1 = distance_5_1 if distance_5_1 > 0 else ( 
  45.         datetime.datetime.strptime(f"{today.year + 1}-05-01""%Y-%m-%d").date() - today).days 
  46.  
  47. distance_10_1 = (datetime.datetime.strptime(f"{today.year}-10-01""%Y-%m-%d").date() - today).days 
  48. distance_10_1 = distance_10_1 if distance_10_1 > 0 else ( 
  49.         datetime.datetime.strptime(f"{today.year + 1}-10-01""%Y-%m-%d").date() - today).days 
  50.  
  51.  
  52. def get_week_day(date): 
  53.     week_day_dict = { 
  54.         0: '星期一'
  55.         1: '星期二'
  56.         2: '星期三'
  57.         3: '星期四'
  58.         4: '星期五'
  59.         5: '星期六'
  60.         6: '星期天'
  61.     } 
  62.     day = date.weekday() 
  63.     return week_day_dict[day
  64.  
  65.  
  66. # print("距離大年: ", distance_big_year) 
  67. # print("距離端午: ", distance_5_5) 
  68. # print("距離中秋: ", distance_8_15) 
  69. # print("距離元旦: ", distance_year) 
  70. # print("距離清明: ", distance_4_5) 
  71. # print("距離勞動: ", distance_5_1) 
  72. # print("距離國慶: ", distance_10_1) 
  73. # print("距離周末: ", 5 - today.weekday()) 
  74.  
  75. now_ = f"{today.year}年{today.month}月{today.day}日" 
  76. week_day_ = get_week_day(today) 
  77. time_ = [ 
  78.     {"v_": 5 - 1 - today.weekday(), "title""周末"},  # 距離周末 
  79.     {"v_": distance_year, "title""元旦"},  # 距離元旦 
  80.     {"v_": distance_big_year, "title""過年"},  # 距離過年 
  81.     {"v_": distance_4_5, "title""清明節(jié)"},  # 距離清明 
  82.     {"v_": distance_5_1, "title""勞動節(jié)"},  # 距離勞動 
  83.     {"v_": distance_5_5, "title""端午節(jié)"},  # 距離端午 
  84.     {"v_": distance_8_15, "title""中秋節(jié)"},  # 距離中秋 
  85.     {"v_": distance_10_1, "title""國慶節(jié)"},  # 距離國慶 
  86.  
  87. time_ = sorted(time_, key=lambda x: x['v_'], reverse=False
  88.  
  89.  
  90. @app.get("/", response_class=HTMLResponse) 
  91. async def readme(request: Request): 
  92.     return templates.TemplateResponse("readme.html"
  93.                                       {"request": request, "time_": time_, "now_": now_, "week_day_": week_day_}) 
  94.  
  95.  
  96. if __name__ == '__main__'
  97.     import uvicorn 
  98.  
  99.     uvicorn.run(app='main:app', host="0.0.0.0", port=8080, reload=True

最后就到了 html 頁面部分了,來看一下主要的傳值。

  1. <center> 
  2.     【摸魚辦公室】今天是 {{ now_ }} {{ week_day_ }} 
  3.     <br><br> 
  4.     {% for v_ in time_ %} 
  5.         <p>🐟 距離 {{ v_.title }} 放假還有 {{ v_.v_ }} 天</p> 
  6.     {% else %} 
  7.         <p>沒有任何值</p> 
  8.     {% endfor %} 
  9.  
  10. </center> 

這樣整個的路由構(gòu)造和頁面編寫就算是完成了。

最后通過 Nginx 部署到我的站點(diǎn)上。

摸魚辦預(yù)覽地址

代碼已經(jīng)上傳至 摸魚辦:

https://github.com/PY-GZKY/moyu

 

責(zé)任編輯:姜華 來源: Python爬蟲與數(shù)據(jù)挖掘
相關(guān)推薦

2022-02-17 10:26:17

JavaScript掃雷游戲前端

2021-11-01 10:26:07

CanvasAPI畫布技術(shù)HTML5

2022-01-24 11:02:27

PySimpleGUPython計算器

2023-05-22 10:04:24

2021-02-01 08:41:06

Java考試系統(tǒng)

2022-01-02 07:00:48

Python

2017-09-14 09:09:04

php應(yīng)用LibreOfficeWord轉(zhuǎn)HTML

2021-02-04 15:52:46

Java考試系統(tǒng)

2021-01-04 09:55:26

Java移動互聯(lián)網(wǎng)

2021-01-05 09:04:20

Javatxt文件

2021-06-10 07:49:28

Python詞云圖wordcloud

2021-07-14 09:00:00

JavaFX開發(fā)應(yīng)用

2018-09-09 15:38:55

SD-WAN網(wǎng)絡(luò)WAN

2021-01-10 08:14:01

Go語言TCP掃描器

2021-01-13 09:03:48

Java游戲函數(shù)

2021-01-12 05:05:15

Java對碰游戲

2021-08-13 09:01:31

Python小游戲Python基礎(chǔ)

2022-12-07 08:42:35

2022-02-15 09:03:07

Pythonmofish庫代碼

2021-08-02 07:35:19

Nacos配置中心namespace
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號