python:bottle + eel 模仿 mdict 查英汉词典

Eel 是一个轻量的 Python 库,用于制作简单的类似于离线 HTML/JS GUI 应用程序,并具有对 Python 功能和库的完全访问权限。

Eel 托管一个本地 Web 服务器,允许您使用 Python 注释函数(annotate functions),可以从 JavaScript 调用python函数,也可以从python调用JavaScript函数。

Eel 基于 Bottle 和 gevent 构建,它们提供了类似于 JavaScript 的异步事件循环。

用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 eel

Eel-0.16.0.tar.gz (24 kB)
bottle-websocket-0.2.9.tar.gz (2.0 kB)
whichcraft-0.6.1-py2.py3-none-any.whl (5.2 kB)
gevent_websocket-0.10.1-py3-none-any.whl (22 kB)
gevent-23.9.1-cp310-cp310-win_amd64.whl (1.5 MB)
zope.event-5.0-py3-none-any.whl (6.8 kB)

编写 mdict_eel.py 如下

# -*- coding: utf-8 -*-
""" web server 用于查询英汉词典 """
import os
import sys
import json
import time
from readmdict import MDX
import bottle
from bottle import route, post, request, static_file
import eel
import win32com.client  # TTS
sapi = win32com.client.Dispatch("SAPI.SpVoice")

start_time = time.time()
os.chdir("/mdict")
# 加载.mdx文件
filename = "your.mdx"
mdx = MDX(filename)
headwords = [*mdx]       # 单词名列表
items = [*mdx.items()]   # 释义html源码列表
n = len(headwords)
m = len(items)
if n == m:
    print(f'{filename} 加载成功:共{n}条')
    end_time = time.time()
    print('cost %f second' % (end_time - start_time))
else:
    print(f'ERROR:加载失败 {n}!={m}')
    sys.exit(1)

app = bottle.Bottle()

# 静态资源的目录通常称为public或static
@app.route('/')
def server_static(filepath):
    return static_file(filepath, root='./')

def eng_han(txt):
    """ 英译中 """
    if not txt.isascii():
        return 'Maybe text is not english'
    word = txt.encode()
    word1 = txt.capitalize().encode() # 第1个字母变大写
    global headwords, items
    try: # 查词,返回单词和html文件
        if word in headwords:
            wordIndex = headwords.index(word)
        else:
            wordIndex = headwords.index(word1)
        word,html = items[wordIndex]
        result = html.decode()
        if result.startswith('@@@LINK='):
            w = result[8:].strip()
            result = '' +w+ ''
        else:
            result = result.replace('{txt} is not in word_list."
    return result

@app.route('/prefix')
def prefix():
    """ 前缀匹配 """
    try:
        txt = request.query.txt
    except:
        return '1: get txt error'
    if len(txt.strip()) ==0:
        return 'text is null'
    print(txt)
    if len(txt) > 1:
        alist = []
        word = txt.strip().lower() # 字母变小写
        for hw in headwords:
            hws = hw.decode().lower()
            if hws.startswith(word):
                hws = hw.decode()
                alist.append(hws)
        if len(alist) > 0:
            result = json.dumps(alist)
        else:
            result = '["not found"]'
    else:
        result = '["length too short"]'
    return result

@app.route('/trans')
def trans():
    """ method=GET 英译中"""
    try:
        txt = request.query.txt
    except:
        return '1: get txt error'
    if len(txt.strip()) ==0:
        return 'text is null'
    print(txt)
    result = eng_han(txt)
    return result

@app.route('/trans', method='POST')
def trans():
    """ 英译中 """
    try:
        #txt = request.forms.get('txt')
        txt = request.POST.get('txt')
    except:
        return '1: get txt error'
    if len(txt.strip()) ==0:
        return 'text is null'
    print(txt)
    result = eng_han(txt)
    return result

eel.init('.')

@eel.expose           # 暴露python函数给js
def py_speak(txt):
    """ text TTS """
    if txt.strip() !='':
        sapi.Speak(txt)

#eel.start('index.html', app=middleware)
eel.start('index5.html', app=app, size=(1000,600))
#eel.start('index5.html', app=app, mode="edge", size=(1000,600))

编写 index5.html 如下




    
    
    
    查询英汉词典 
    
    



  

运行 python mdict_eel.py 

个人感觉:bottle + eel 比 pywebview + cef 要好用些。

python:bottle + eel 模仿 mdict 查英汉词典_第1张图片

你可能感兴趣的:(python,web服务,python,bottle,eel)