• 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

  • 相关阅读:
    Java 拷贝
    Day24力扣打卡
    记录下学的性能优化
    解决仪器掉线备忘
    R语言caTools包进行数据划分、scale函数进行数据缩放、class包的knn函数构建K近邻分类器
    分享一个清理工具栏和插件的小工具
    简单对比Java、Python、Go、Rust等常见语言计算斐波拉契数的性能
    Doris 5 处理Sentinel-1 生成干涉图 interferogram
    关于jQuery_属性选择器的介绍和基本使用
    高频js手写题之实现数组扁平化、深拷贝、总线模式
  • 原文地址:https://blog.csdn.net/belldeep/article/details/133409638