updated interface

This commit is contained in:
Robert 2021-01-03 02:26:21 +01:00
parent ff9343a5ab
commit fde88b9648
2 changed files with 26 additions and 4 deletions

25
app.py
View file

@ -1,14 +1,31 @@
from flask import Flask, render_template, request from flask import Flask, render_template, request
from bs4 import BeautifulSoup
import urllib, requests
app = Flask(__name__) app = Flask(__name__)
@app.route('/') @app.route('/')
def main(): def main():
return render_template('index.html', svg="") return render_template('index.html', svg="", name="")
@app.route('/submit_kanji', methods=['GET']) @app.route('/submit_kanji', methods=['GET'])
def submit_kanji(): def submit_kanji():
try: try:
with open(f"assets/0{format(ord(request.args.get('kanji')), 'x')}.svg", encoding='utf-8') as file: character = request.args.get('kanji')
return render_template('index.html', svg=file.read())
url = "https://jisho.org/search/{0}".format(urllib.parse.quote_plus(character + "#kanji"))
r = requests.get(url, headers={
"User-Agent": "little-web-project",
"From": "http://lauchism.com"
})
if r.status_code != 200:
print(f"ERROR: Failed to access Jisho API... {r.status_code}")
return None
body = BeautifulSoup(r.text, features="html.parser")
names = body.find("div", {"class": "kanji-details__main-meanings"})
with open(f"assets/0{format(ord(character), 'x')}.svg", encoding='utf-8') as file:
return render_template('index.html', svg=file.read(), name=names.string)
except: except:
return render_template('index.html', svg="no") return render_template('index.html', svg="no", name="no")

View file

@ -31,6 +31,7 @@
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head> </head>
<body> <body>
<h1>search for kanji</h1>
<form action="{{ url_for('submit_kanji') }}" method="get"> <form action="{{ url_for('submit_kanji') }}" method="get">
<input type="text" name="kanji"> <input type="text" name="kanji">
<input type="submit"> <input type="submit">
@ -39,6 +40,10 @@
{{svg|safe}} {{svg|safe}}
</span> </span>
<h2>
{{name}}
</h2>
<script> <script>
var allParts = $("svg > g > g *[kvg\\:element]"); var allParts = $("svg > g > g *[kvg\\:element]");