• python:PyWebIO 模仿 mdict 查英汉词典


    PyWebIO提供了一系列命令式的交互函数来在浏览器上获取用户输入和进行输出,将浏览器变成了一个“富文本终端”,可以用于构建简单的Web应用或基于浏览器的GUI应用。 使用PyWebIO,开发者能像编写终端脚本一样(基于input 和 put 进行交互)来编写应用,无需具备HTML和 js的相关知识; PyWebIO还可以方便地整合进现有的Web服务。非常适合快速构建对UI要求不高的应用。

     参考 :PyWebIO 中文文档

    用chrome 访问 https://www.lfd.uci.edu/~gohlke/pythonlibs/#python-lzo
    下载 python_lzo-1.14-cp38-cp38-win_amd64.whl
    pip install python_lzo-1.14-cp38-cp38-win_amd64.whl

    pip install readmdict ;

    pip install pywebio

    pywebio-1.8.2.tar.gz (495 kB)
    tornado-6.2-cp37-abi3-win_amd64.whl (425 kB)
    user_agents-2.2.0-py3-none-any.whl (9.6 kB)
    ua_parser-0.18.0-py2.py3-none-any.whl (38 kB)

    编写 mdict_pywebio.py 如下:

    1. # -*- coding: utf-8 -*-
    2. """ 查询英汉 lexicons """
    3. import os
    4. import time
    5. from readmdict import MDX
    6. from pywebio import config
    7. from pywebio.input import input, TEXT, select
    8. from pywebio.output import put_html
    9. def open_mdx():
    10. """ 读.mdx词典文件 """
    11. start_time = time.time()
    12. global headwords, items
    13. os.chdir("/mdict/")
    14. # 加载.mdx文件
    15. fname = "your.mdx"
    16. mdx = MDX(fname)
    17. headwords = [*mdx] # 单词名列表
    18. items = [*mdx.items()] # 释义html源码列表
    19. end_time = time.time()
    20. print('cost %f second' % (end_time - start_time))
    21. def prefix(txt):
    22. """ 前缀匹配 """
    23. global headwords
    24. try:
    25. type(headwords)
    26. except NameError:
    27. print('headwords is undefined.')
    28. return
    29. alist = []
    30. if len(txt) > 1:
    31. word = txt.strip().lower() # 字母变小写
    32. for hw in headwords:
    33. hws = hw.decode().lower()
    34. if hws.startswith(word):
    35. alist.append(hw.decode())
    36. else:
    37. print(f"{txt} input too short")
    38. return alist
    39. def query1(txt):
    40. """ 查询英文单词 """
    41. global headwords
    42. try:
    43. type(headwords)
    44. except NameError:
    45. print('headwords is undefined.')
    46. return
    47. word = txt.lower().encode()
    48. word1 = txt.capitalize().encode() # 第1个字母变大写
    49. try: # 查词,返回单词和html文件
    50. if word in headwords:
    51. wordIndex = headwords.index(word)
    52. else:
    53. wordIndex = headwords.index(word1)
    54. word,html = items[wordIndex]
    55. result = html.decode()
    56. return result
    57. except:
    58. return f"{txt} is not in word_list."
    59. if __name__ == '__main__':
    60. open_mdx()
    61. txt = input("请输入 a word", type=TEXT)
    62. alist = prefix(txt)
    63. word = select("请选择 a word", options=alist)
    64. result = query1(word)
    65. #pywebio.config(title="result", cssfile="your.css")
    66. if result:
    67. put_html(result)
    68. else:
    69. print("result is null")

    把你的词典 css文件 copy to web主目录 在 D:\Python38\Lib\site-packages\pywebio\html\

    运行 python mdict_pywebio.py

  • 相关阅读:
    前端获取资源的方式(ajax、fetch)及其区别
    redis与 缓存击穿、缓存穿透、缓存雪崩
    Java日期查询
    个人练习-Leetcode-1567. Maximum Length of Subarray With Positive Product
    python_unittest
    git常见 操作仓库指令
    SpringMvc第五战-【SpringMvcJSR303和拦截器】
    粒子特效-Xffect
    网页报告不能直接转换成Word、PDF格式怎么办?Spire.doc控件可以轻松解决
    【文章阅读】Frustratingly Simple Few-Shot Object Detection
  • 原文地址:https://blog.csdn.net/belldeep/article/details/133409638