python:基于flask有道智云API在线翻译网页

2023-11-06 23:20

本文主要是介绍python:基于flask有道智云API在线翻译网页,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

 基于 Flask 的网页翻译系统,用户可以在页面中输入需要翻译的文本,并选择翻译语言,系统将通过有道智云API返回翻译结果。

系统应该支持多种语言的翻译,能够准确地翻译文本,并将翻译结果返回给用户。用户应该能够在界面中看到翻译的结果,并可以进行复制或分享。

index.html

<!DOCTYPE html>
<html>
<head><title>基于flask&amp;有道智云API在线翻译</title><style>body {font-family: Arial, Helvetica, sans-serif;margin: 0;padding: 0;background-color: #f2f2f2;}h1 {text-align: center;margin-top: 50px;font-size: 36px;}form {margin-top: 30px;max-width: 100%;margin-left: auto;margin-right: auto;background-color: #fff;padding: 20px;border-radius: 10px;box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);}label {font-weight: bold;}textarea {width: 100%;margin-top: 5px;margin-bottom: 15px;border-radius: 5px;padding: 5px;}select {margin-top: 5px;margin-bottom: 15px;border-radius: 5px;padding: 5px;}input[type="submit"] {display: block;margin-top: 20px;margin-left: auto;margin-right: auto;padding: 10px 20px;font-size: 18px;font-weight: bold;text-transform: uppercase;color: #fff;background-color: #4CAF50;border-radius: 5px;border: none;cursor: pointer;transition: all 0.3s ease;}input[type="submit"]:hover {background-color: #3E8E41;}h2 {margin-top: 30px;font-size: 24px;}p {margin-top: 10px;font-size: 18px;font-weight: bold;background-color: #D5F5E3;padding: 10px;border-radius: 5px;border: 1px solid #4CAF50;}m {margin-top: 10px;font-size: 18px;font-weight: bold;background-color: #f5f5f5;padding: 10px;border-radius: 5px;}</style>
</head>
<body><h1>基于flask&amp;有道智云API在线翻译</h1><form method="post" action="/"><label for="input_text">需要翻译的文本:</label><br><textarea id="input_text" name="input_text" rows="5" cols="40">{% if input_text %}{{ input_text }}{% endif %}</textarea><br><label for="target_language">需要翻译的语言:</label><br><select id="target_language" name="target_language"><option value="zh-CHS" {% if target_language == "zh-CHS" %}selected{% endif %}>Chinese Simplified</option><option value="zh-CHT" {% if target_language == "zh-CHT" %}selected{% endif %}>Chinese Traditional</option><option value="en" {% if target_language == "en" %}selected{% endif %}>English</option><option value="ja" {% if target_language == "ja" %}selected{% endif %}>Japanese</option><option value="ko" {% if target_language == "ko" %}selected{% endif %}>Korean</option><option value="fr" {% if target_language == "fr" %}selected{% endif %}>French</option><option value="es" {% if target_language == "es" %}selected{% endif %}>Spanish</option><option value="pt" {% if target_language == "pt" %}selected{% endif %}>Portuguese</option><option value="it" {% if target_language == "it" %}selected{% endif %}>Italian</option><option value="ru" {% if target_language == "ru" %}selected{% endif %}>Russian</option><option value="vi" {% if target_language == "vi" %}selected{% endif %}>Vietnamese</option><option value="de" {% if target_language == "de" %}selected{% endif %}>German</option><option value="ar" {% if target_language == "ar" %}selected{% endif %}>Arabic</option><option value="id" {% if target_language == "id" %}selected{% endif %}>Indonesian</option><option value="af" {% if target_language == "af" %}selected{% endif %}>Afrikaans</option><option value="bs" {% if target_language == "bs" %}selected{% endif %}>Bosnian</option><option value="bg" {% if target_language == "bg" %}selected{% endif %}>Bulgarian</option><option value="yue" {% if target_language == "yue" %}selected{% endif %}>Cantonese</option><option value="ca" {% if target_language == "ca" %}selected{% endif %}>Catalan</option><option value="hr" {% if target_language == "hr" %}selected{% endif %}>Croatian</option><option value="cs" {% if target_language == "cs" %}selected{% endif %}>Czech</option><option value="da" {% if target_language == "da" %}selected{% endif %}>Danish</option><option value="nl" {% if target_language == "nl" %}selected{% endif %}>Dutch</option><option value="et" {% if target_language == "et" %}selected{% endif %}>Estonian</option><option value="fj" {% if target_language == "fj" %}selected{% endif %}>Fijian</option><option value="fi" {% if target_language == "fi" %}selected{% endif %}>Finnish</option><option value="el" {% if target_language == "el" %}selected{% endif %}>Greek</option><option value="ht" {% if target_language == "ht" %}selected{% endif %}>Haitian Creole</option><option value="he" {% if target_language == "he" %}selected{% endif %}>Hebrew</option><option value="hi" {% if target_language == "hi" %}selected{% endif %}>Hindi</option><option value="mww" {% if target_language == "mww" %}selected{% endif %}>Hmong Daw</option><option value="hu" {% if target_language == "hu" %}selected{% endif %}>Hungarian</option><option value="sw" {% if target_language == "sw" %}selected{% endif %}>Swahili</option><option value="tlh" {% if target_language == "tlh" %}selected{% endif %}>Klingon</option><option value="lv" {% if target_language == "lv" %}selected{% endif %}>Latvian</option><option value="lt" {% if target_language == "lt" %}selected{% endif %}>Lithuanian</option><option value="ms" {% if target_language == "ms" %}selected{% endif %}>Malay</option><option value="mt" {% if target_language == "mt" %}selected{% endif %}>Maltese</option><option value="no" {% if target_language == "no" %}selected{% endif %}>Norwegian</option><option value="fa" {% if target_language == "fa" %}selected{% endif %}>Persian</option><option value="pl" {% if target_language == "pl" %}selected{% endif %}>Polish</option><option value="otq" {% if target_language == "otq" %}selected{% endif %}>Queretaro Otomi</option><option value="ro" {% if target_language == "ro" %}selected{% endif %}>Romanian</option><option value="sr-Cyrl" {% if target_language == "sr-Cyrl" %}selected{% endif %}>Serbian (Cyrillic)</option><option value="sr-Latn" {% if target_language == "sr-Latn" %}selected{% endif %}>Serbian (Latin)</option><option value="sk" {% if target_language == "sk" %}selected{% endif %}>Slovak</option><option value="sl" {% if target_language == "sl" %}selected{% endif %}>Slovenian</option><option value="sv" {% if target_language == "sv" %}selected{% endif %}>Swedish</option><option value="ty" {% if target_language == "ty" %}selected{% endif %}>Tahitian</option><option value="th" {% if target_language == "th" %}selected{% endif %}>Thai</option><option value="to" {% if target_language == "to" %}selected{% endif %}>Tongan</option><option value="tr" {% if target_language == "tr" %}selected{% endif %}>Turkish</option><option value="uk" {% if target_language == "uk" %}selected{% endif %}>Ukrainian</option><option value="ur" {% if target_language == "ur" %}selected{% endif %}>Urdu</option><option value="cy" {% if target_language == "cy" %}selected{% endif %}>Welsh</option><option value="yua" {% if target_language == "yua" %}selected{% endif %}>Yucatec Maya</option><option value="sq" {% if target_language == "sq" %}selected{% endif %}>Albanian</option><option value="am" {% if target_language == "am" %}selected{% endif %}>Amharic</option><option value="hy" {% if target_language == "hy" %}selected{% endif %}>Armenian</option><option value="az" {% if target_language == "az" %}selected{% endif %}>Azerbaijani</option><option value="bn" {% if target_language == "bn" %}selected{% endif %}>Bengali</option><option value="eu" {% if target_language == "eu" %}selected{% endif %}>Basque</option><option value="be" {% if target_language == "be" %}selected{% endif %}>Belarusian</option><option value="ceb" {% if target_language == "ceb" %}selected{% endif %}>Cebuano</option><option value="co" {% if target_language == "co" %}selected{% endif %}>Corsican</option><option value="eo" {% if target_language == "eo" %}selected{% endif %}>Esperanto</option><option value="tl" {% if target_language == "tl" %}selected{% endif %}>Filipino</option><option value="fy" {% if target_language == "fy" %}selected{% endif %}>Frisian</option><option value="gl" {% if target_language == "gl" %}selected{% endif %}>Galician</option><option value="ka" {% if target_language == "ka" %}selected{% endif %}>Georgian</option><option value="gu" {% if target_language == "gu" %}selected{% endif %}>Gujarati</option><option value="ha" {% if target_language == "ha" %}selected{% endif %}>Hausa</option><option value="haw" {% if target_language == "haw" %}selected{% endif %}>Hawaiian</option><option value="is" {% if target_language == "is" %}selected{% endif %}>Icelandic</option><option value="ig" {% if target_language == "ig" %}selected{% endif %}>Igbo</option><option value="ga" {% if target_language == "ga" %}selected{% endif %}>Irish</option><option value="jw" {% if target_language == "jw" %}selected{% endif %}>Javanese</option><option value="kn" {% if target_language == "kn" %}selected{% endif %}>Kannada</option><option value="kk" {% if target_language == "kk" %}selected{% endif %}>Kazakh</option><option value="km" {% if target_language == "km" %}selected{% endif %}>Khmer</option><option value="ku" {% if target_language == "ku" %}selected{% endif %}>Kurdish</option><option value="ky" {% if target_language == "ky" %}selected{% endif %}>Kyrgyz</option><option value="lo" {% if target_language == "lo" %}selected{% endif %}>Lao</option><option value="la" {% if target_language == "la" %}selected{% endif %}>Latin</option><option value="lb" {% if target_language == "lb" %}selected{% endif %}>Luxembourgish</option><option value="mk" {% if target_language == "mk" %}selected{% endif %}>Macedonian</option><option value="mg" {% if target_language == "mg" %}selected{% endif %}>Malagasy</option><option value="ml" {% if target_language == "ml" %}selected{% endif %}>Malayalam</option><option value="mi" {% if target_language == "mi" %}selected{% endif %}>Maori</option><option value="mr" {% if target_language == "mr" %}selected{% endif %}>Marathi</option><option value="mn" {% if target_language == "mn" %}selected{% endif %}>Mongolian</option><option value="my" {% if target_language == "my" %}selected{% endif %}>Burmese</option><option value="ne" {% if target_language == "ne" %}selected{% endif %}>Nepali</option><option value="ny" {% if target_language == "ny" %}selected{% endif %}>Chichewa</option><option value="ps" {% if target_language == "ps" %}selected{% endif %}>Pashto</option><option value="pa" {% if target_language == "pa" %}selected{% endif %}>Punjabi</option><option value="sm" {% if target_language == "sm" %}selected{% endif %}>Samoan</option><option value="gd" {% if target_language == "gd" %}selected{% endif %}>Scots Gaelic</option><option value="st" {% if target_language == "st" %}selected{% endif %}>Sesotho</option><option value="sn" {% if target_language == "sn" %}selected{% endif %}>Shona</option><option value="sd" {% if target_language == "sd" %}selected{% endif %}>Sindhi</option><option value="si" {% if target_language == "si" %}selected{% endif %}>Sinhala</option><option value="so" {% if target_language == "so" %}selected{% endif %}>Somali</option><option value="su" {% if target_language == "su" %}selected{% endif %}>Sundanese</option><option value="tg" {% if target_language == "tg" %}selected{% endif %}>Tajik</option><option value="ta" {% if target_language == "ta" %}selected{% endif %}>Tamil</option><option value="te" {% if target_language == "te" %}selected{% endif %}>Telugu</option><option value="auto" {% if target_language == "auto" %}selected{% endif %}>	auto</option></select><br><input type="submit" value="翻译"></form>{% if translated_text %}<div class="out"><m>原始文本:</m><p>{{ input_text }}</p><m>翻译语言:</m><p>{{ target_language }}</p><m>翻译结果:</m><p>{{ translated_text }}</p></div>{% endif %}
</body>
</html>

main.py
 

from flask import Flask, render_template, request
import requests
import json
import hashlib
import random
import timeapp = Flask(__name__)
app.config['SECRET_KEY'] = 'dev'# 有道翻译 API 服务器端点,需要先申请App Key和App Secret
api_endpoint = "https://openapi.youdao.com/api"
app_key = "自己获取"
app_secret = "自己获取"def generate_sign_params(query):"""使用app_key和app_secret生成有道翻译API请求参数中的sign参数"""salt = str(random.randint(1, 65536))sign_param = app_key + query + salt + app_secretmd5 = hashlib.md5()md5.update(sign_param.encode('utf-8'))sign = md5.hexdigest()return {'sign': sign, 'salt': salt}@app.route('/', methods=['GET', 'POST'])
def index():if request.method == 'POST':# 处理表单提交请求input_text = request.form['input_text']target_language = request.form['target_language']# 检测输入语言detect_params = generate_sign_params(input_text)detect_params.update({'q': input_text,'from': 'auto','to': 'auto','appKey': app_key})detect_response = requests.get(api_endpoint, params=detect_params)if detect_response.status_code != 200:# 如果响应状态码不是200,则说明请求出错return 'Error: Could not retrieve language.'detected_language = detect_response.json().get('lang')print(str(detected_language))# 调用翻译 API 进行翻译translate_params = generate_sign_params(input_text)translate_params.update({'q': input_text,'from': detected_language,'to': target_language,'appKey': app_key})translate_response = requests.get(api_endpoint, params=translate_params)if translate_response.status_code != 200:# 如果响应状态码不是200,则说明请求出错return 'Error: Could not translate the text.'# 处理翻译结果并返回translated_text = translate_response.json().get('translation')[0]return render_template('index.html', input_text=input_text,target_language=target_language,translated_text=translated_text)# 渲染首页return render_template('index.html')if __name__ == '__main__':app.run(debug=True)

这篇关于python:基于flask有道智云API在线翻译网页的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/359669

相关文章

使用Python删除Excel中的行列和单元格示例详解

《使用Python删除Excel中的行列和单元格示例详解》在处理Excel数据时,删除不需要的行、列或单元格是一项常见且必要的操作,本文将使用Python脚本实现对Excel表格的高效自动化处理,感兴... 目录开发环境准备使用 python 删除 Excphpel 表格中的行删除特定行删除空白行删除含指定

Python通用唯一标识符模块uuid使用案例详解

《Python通用唯一标识符模块uuid使用案例详解》Pythonuuid模块用于生成128位全局唯一标识符,支持UUID1-5版本,适用于分布式系统、数据库主键等场景,需注意隐私、碰撞概率及存储优... 目录简介核心功能1. UUID版本2. UUID属性3. 命名空间使用场景1. 生成唯一标识符2. 数

Python办公自动化实战之打造智能邮件发送工具

《Python办公自动化实战之打造智能邮件发送工具》在数字化办公场景中,邮件自动化是提升工作效率的关键技能,本文将演示如何使用Python的smtplib和email库构建一个支持图文混排,多附件,多... 目录前言一、基础配置:搭建邮件发送框架1.1 邮箱服务准备1.2 核心库导入1.3 基础发送函数二、

Python包管理工具pip的升级指南

《Python包管理工具pip的升级指南》本文全面探讨Python包管理工具pip的升级策略,从基础升级方法到高级技巧,涵盖不同操作系统环境下的最佳实践,我们将深入分析pip的工作原理,介绍多种升级方... 目录1. 背景介绍1.1 目的和范围1.2 预期读者1.3 文档结构概述1.4 术语表1.4.1 核

基于Python实现一个图片拆分工具

《基于Python实现一个图片拆分工具》这篇文章主要为大家详细介绍了如何基于Python实现一个图片拆分工具,可以根据需要的行数和列数进行拆分,感兴趣的小伙伴可以跟随小编一起学习一下... 简单介绍先自己选择输入的图片,默认是输出到项目文件夹中,可以自己选择其他的文件夹,选择需要拆分的行数和列数,可以通过

Python中反转字符串的常见方法小结

《Python中反转字符串的常见方法小结》在Python中,字符串对象没有内置的反转方法,然而,在实际开发中,我们经常会遇到需要反转字符串的场景,比如处理回文字符串、文本加密等,因此,掌握如何在Pyt... 目录python中反转字符串的方法技术背景实现步骤1. 使用切片2. 使用 reversed() 函

Python中将嵌套列表扁平化的多种实现方法

《Python中将嵌套列表扁平化的多种实现方法》在Python编程中,我们常常会遇到需要将嵌套列表(即列表中包含列表)转换为一个一维的扁平列表的需求,本文将给大家介绍了多种实现这一目标的方法,需要的朋... 目录python中将嵌套列表扁平化的方法技术背景实现步骤1. 使用嵌套列表推导式2. 使用itert

使用Docker构建Python Flask程序的详细教程

《使用Docker构建PythonFlask程序的详细教程》在当今的软件开发领域,容器化技术正变得越来越流行,而Docker无疑是其中的佼佼者,本文我们就来聊聊如何使用Docker构建一个简单的Py... 目录引言一、准备工作二、创建 Flask 应用程序三、创建 dockerfile四、构建 Docker

Python使用vllm处理多模态数据的预处理技巧

《Python使用vllm处理多模态数据的预处理技巧》本文深入探讨了在Python环境下使用vLLM处理多模态数据的预处理技巧,我们将从基础概念出发,详细讲解文本、图像、音频等多模态数据的预处理方法,... 目录1. 背景介绍1.1 目的和范围1.2 预期读者1.3 文档结构概述1.4 术语表1.4.1 核

Python使用pip工具实现包自动更新的多种方法

《Python使用pip工具实现包自动更新的多种方法》本文深入探讨了使用Python的pip工具实现包自动更新的各种方法和技术,我们将从基础概念开始,逐步介绍手动更新方法、自动化脚本编写、结合CI/C... 目录1. 背景介绍1.1 目的和范围1.2 预期读者1.3 文档结构概述1.4 术语表1.4.1 核