基于Python绘制一个摸鱼倒计时界面

作者:Python进阶者 时间:2022-09-02 03:03:26 

前言

前段时间在微博看到一段摸鱼人的倒计时模板,感觉还挺有趣的。

基于Python绘制一个摸鱼倒计时界面

于是我用了一小时的时间写了个页面出来 摸鱼办地址 (当然是摸鱼的时间啦)。

模板是这样的:

摸鱼办公室 🐟

【摸鱼办公室】今天是 2021-11-30 星期二

你好,摸鱼人,工作再累,一定不要忘记摸鱼哦 ! 有事没事起身去茶水间去廊道去天台走走,别老在工位上坐着。多喝点水,钱是老板的,但命是自己的 !

🐟 距离 周末 放假还有 2 天

🐟 距离 元旦 放假还有 3 天

🐟 距离 过年 放假还有 34 天

🐟 距离 清明节 放假还有 97 天

🐟 距离 劳动节 放假还有 123 天

🐟 距离 端午节 放假还有 156 天

🐟 距离 中秋节 放假还有 255 天

🐟 距离 国庆节 放假还有 276 天

  • 由于前端是单页面服务,直接撸一个原始的 html 网页就行。

  • FastAPI 对于异步请求是一把好手、更轻、性能更佳。

  • 挂上一层 Nginx 让它看起来像那么回事儿。

实现过程

首先要知道、除了静态文字之外的比如当前日期、距离节日放假的天数等都是动态返回的,我需要使用 Jinja2 模板进行动态绑定。

我应该把重点放在时间的处理上。

而且在这个模板中,有阳历的节日,也是阴历的节日,我需要转换。

初始化一个 FastAPI 对象并声明静态页面的模板目录 (Jinja2Templates)


# -*- coding: utf-8 -*-
import datetime
from fastapi import FastAPI, Request
from fastapi.responses import HTMLResponse
from fastapi.templating import Jinja2Templates
from zhdate import ZhDate as lunar_date

app = FastAPI(
   debug=False,
   title="My API",
   docs_url="/docs",
   openapi_url=f"/openapi.json"
)

templates = Jinja2Templates(directory="templates")

可以看到的是我用到了 zhdate 这个库、主要用于阴历和阳历之间的相互转换。用法如下


today = datetime.date.today()
print(today.year, today.month, today.day)
print("大年时间: ", lunar_date(today.year+1, 1, 1).to_datetime().date())
print("端午时间: ", lunar_date(today.year, 5, 5).to_datetime().date())
print("中秋时间: ", lunar_date(today.year, 8, 15).to_datetime().date())
print("元旦时间: ", f"{today.year+1}-01-01")
print("清明时间: ", f"{today.year}-04-05")
print("劳动时间: ", f"{today.year}-05-01")
print("国庆时间: ", f"{today.year}-10-01")

我们可以梳理一下:

计算距离 大年、元旦 的天数时,要在年份上 +1

计算距离 其他节日 的天数时,要判断天数差是否小于 0,如果是,则年份需要 +1,因为已经过去的节日对此没有意义


distance_big_year = (lunar_date(today.year + 1, 1, 1).to_datetime().date() - today).days

distance_5_5 = (lunar_date(today.year, 5, 5).to_datetime().date() - today).days
distance_5_5 = distance_5_5 if distance_5_5 > 0 else (
       lunar_date(today.year + 1, 5, 5).to_datetime().date() - today).days

distance_8_15 = (lunar_date(today.year, 8, 15).to_datetime().date() - today).days
distance_8_15 = distance_8_15 if distance_8_15 > 0 else (
       lunar_date(today.year + 1, 8, 15).to_datetime().date() - today).days

distance_year = (datetime.datetime.strptime(f"{today.year + 1}-01-01", "%Y-%m-%d").date() - today).days

distance_4_5 = (datetime.datetime.strptime(f"{today.year}-04-05", "%Y-%m-%d").date() - today).days
distance_4_5 = distance_4_5 if distance_4_5 > 0 else (
       datetime.datetime.strptime(f"{today.year + 1}-04-05", "%Y-%m-%d").date() - today).days

distance_5_1 = (datetime.datetime.strptime(f"{today.year}-05-01", "%Y-%m-%d").date() - today).days
distance_5_1 = distance_5_1 if distance_5_1 > 0 else (
       datetime.datetime.strptime(f"{today.year + 1}-05-01", "%Y-%m-%d").date() - today).days

distance_10_1 = (datetime.datetime.strptime(f"{today.year}-10-01", "%Y-%m-%d").date() - today).days
distance_10_1 = distance_10_1 if distance_10_1 > 0 else (
       datetime.datetime.strptime(f"{today.year + 1}-10-01", "%Y-%m-%d").date() - today).days

怎么样? 我的命名足够疯狂吧。

接下来需要计算一下距离周末的天数。


def get_week_day(date):
   week_day_dict = {
       0: '星期一',
       1: '星期二',
       2: '星期三',
       3: '星期四',
       4: '星期五',
       5: '星期六',
       6: '星期天',
   }
   day = date.weekday()
   return week_day_dict[day]

week_day_ = get_week_day(today)
print(f"今天是: {week_day_}") # 先获取今天是星期几

按照每周 5 个工作日计算,今天距离周末的天数就是


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

现在将所有的数据组装起来


time_ = [
   {"v_": distance_year, "title": "元旦"},  # 距离元旦
   {"v_": distance_big_year, "title": "过年"},  # 距离过年
   {"v_": distance_4_5, "title": "清明节"},  # 距离清明
   {"v_": distance_5_1, "title": "劳动节"},  # 距离劳动
   {"v_": distance_5_5, "title": "端午节"},  # 距离端午
   {"v_": distance_8_15, "title": "中秋节"},  # 距离中秋
   {"v_": distance_10_1, "title": "国庆节"},  # 距离国庆
]

至于为什么是 List 而不是 Dict,那是我需要做一个根据距离天数的排序,让最先放假的节日放于最前面, 这样看起来会舒服得多。


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

接下来要写一个 路由,将数据传入到 html 页面中去。


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

来看一下完整的代码 (main.py):


# -*- coding: utf-8 -*-
import datetime
from fastapi import FastAPI, Request
from fastapi.responses import HTMLResponse
from fastapi.templating import Jinja2Templates
from zhdate import ZhDate as lunar_date

app = FastAPI(
   debug=False,
   title="My API",
   docs_url=f"/docs",
   openapi_url=f"/openapi.json"
)

templates = Jinja2Templates(directory="templates")

today = datetime.date.today()

# print(today.year, today.month, today.day)
# print("大年时间: ", lunar_date(today.year+1, 1, 1).to_datetime().date())
# print("端午时间: ", lunar_date(today.year, 5, 5).to_datetime().date())
# print("中秋时间: ", lunar_date(today.year, 8, 15).to_datetime().date())
# print("元旦时间: ", f"{today.year+1}-01-01")
# print("清明时间: ", f"{today.year+1}-04-05")
# print("劳动时间: ", f"{today.year+1}-05-01")
# print("国庆时间: ", f"{today.year+1}-10-01")

distance_big_year = (lunar_date(today.year + 1, 1, 1).to_datetime().date() - today).days

distance_5_5 = (lunar_date(today.year, 5, 5).to_datetime().date() - today).days
distance_5_5 = distance_5_5 if distance_5_5 > 0 else (
       lunar_date(today.year + 1, 5, 5).to_datetime().date() - today).days

distance_8_15 = (lunar_date(today.year, 8, 15).to_datetime().date() - today).days
distance_8_15 = distance_8_15 if distance_8_15 > 0 else (
       lunar_date(today.year + 1, 8, 15).to_datetime().date() - today).days

distance_year = (datetime.datetime.strptime(f"{today.year + 1}-01-01", "%Y-%m-%d").date() - today).days

distance_4_5 = (datetime.datetime.strptime(f"{today.year}-04-05", "%Y-%m-%d").date() - today).days
distance_4_5 = distance_4_5 if distance_4_5 > 0 else (
       datetime.datetime.strptime(f"{today.year + 1}-04-05", "%Y-%m-%d").date() - today).days

distance_5_1 = (datetime.datetime.strptime(f"{today.year}-05-01", "%Y-%m-%d").date() - today).days
distance_5_1 = distance_5_1 if distance_5_1 > 0 else (
       datetime.datetime.strptime(f"{today.year + 1}-05-01", "%Y-%m-%d").date() - today).days

distance_10_1 = (datetime.datetime.strptime(f"{today.year}-10-01", "%Y-%m-%d").date() - today).days
distance_10_1 = distance_10_1 if distance_10_1 > 0 else (
       datetime.datetime.strptime(f"{today.year + 1}-10-01", "%Y-%m-%d").date() - today).days

def get_week_day(date):
   week_day_dict = {
       0: '星期一',
       1: '星期二',
       2: '星期三',
       3: '星期四',
       4: '星期五',
       5: '星期六',
       6: '星期天',
   }
   day = date.weekday()
   return week_day_dict[day]

# print("距离大年: ", distance_big_year)
# print("距离端午: ", distance_5_5)
# print("距离中秋: ", distance_8_15)
# print("距离元旦: ", distance_year)
# print("距离清明: ", distance_4_5)
# print("距离劳动: ", distance_5_1)
# print("距离国庆: ", distance_10_1)
# print("距离周末: ", 5 - today.weekday())

now_ = f"{today.year}年{today.month}月{today.day}日"
week_day_ = get_week_day(today)
time_ = [
   {"v_": 5 - 1 - today.weekday(), "title": "周末"},  # 距离周末
   {"v_": distance_year, "title": "元旦"},  # 距离元旦
   {"v_": distance_big_year, "title": "过年"},  # 距离过年
   {"v_": distance_4_5, "title": "清明节"},  # 距离清明
   {"v_": distance_5_1, "title": "劳动节"},  # 距离劳动
   {"v_": distance_5_5, "title": "端午节"},  # 距离端午
   {"v_": distance_8_15, "title": "中秋节"},  # 距离中秋
   {"v_": distance_10_1, "title": "国庆节"},  # 距离国庆
]

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

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

if __name__ == '__main__':
   import uvicorn

uvicorn.run(app='main:app', host="0.0.0.0", port=8080, reload=True)

最后就到了 html 页面部分了,来看一下主要的传值。


<center>
   【摸鱼办公室】今天是 {{ now_ }} {{ week_day_ }}
   <br><br>
   {% for v_ in time_ %}
       <p>🐟 距离 {{ v_.title }} 放假还有 {{ v_.v_ }} 天</p>
   {% else %}
       <p>沒有任何值</p>
   {% endfor %}

</center>

这样整个的路由构造和页面编写就算是完成了。

最后通过 Nginx 部署到我的站点上。

摸鱼办预览地址

基于Python绘制一个摸鱼倒计时界面

代码已经上传:https://github.com/PY-GZKY/moyu 

来源:https://developer.51cto.com/art/202112/697357.htm

标签:Python,倒计时,界面
0
投稿

猜你喜欢

  • asp显示左边的n个字符自动识别汉字的函数

    2007-09-13 12:16:00
  • 深入探讨SQL Server 2008商务智能(BI)

    2008-12-23 13:56:00
  • golang解析网页利器goquery的使用方法

    2023-10-13 06:36:12
  • Yii2基于Ajax自动获取表单数据的方法

    2023-11-21 00:59:56
  • 用python写个博客迁移工具

    2023-06-09 05:13:14
  • python nohup 实现远程运行不宕机操作

    2023-10-21 02:21:44
  • 随机6+1选号码摇奖程序

    2008-07-18 13:15:00
  • Python 中的集合和字典

    2021-03-18 22:53:30
  • Chrome V8 引擎对 sort 的优化

    2010-02-04 17:27:00
  • 一个可应用在ASP 标记加密文件的MD5的DLL组件

    2008-04-12 07:21:00
  • WEB前端开发高性能优化之JavaScript优化细节

    2009-06-10 14:38:00
  • python GUI库图形界面开发之PyQt5布局控件QGridLayout详细使用方法与实例

    2023-08-29 03:43:39
  • 关于JS中二维数组的声明方法

    2023-08-24 17:39:42
  • python reverse反转部分数组的实例

    2021-09-16 05:30:11
  • python通过百度地图API获取某地址的经纬度详解

    2021-11-06 12:14:45
  • Python pandas DataFrame数据拼接方法

    2022-11-08 08:09:36
  • Python Flask前后端Ajax交互的方法示例

    2021-09-07 09:09:21
  • python中的None与NULL用法说明

    2022-01-01 13:59:15
  • Python爬取成语接龙类网站

    2022-04-09 07:29:58
  • php的ajax框架xajax入门与试用介绍

    2023-09-27 14:46:16
  • asp之家 网络编程 m.aspxhome.com