-
Notifications
You must be signed in to change notification settings - Fork 18
/
web.py
122 lines (109 loc) · 4.06 KB
/
web.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import base64
import tempfile
from pathlib import Path
import streamlit as st
import streamlit.components.v1 as components
from epubhv import EPUBHV
LABELS = {
"none": "None",
"auto": "Auto",
"s2t": "Simplified Chinese to Traditional Chinese",
"t2s": "Traditional Chinese to Simplified Chinese",
"s2tw": "Simplified Chinese to Traditional Chinese (Taiwan Standard)",
"tw2s": "Traditional Chinese (Taiwan Standard) to Simplified Chinese",
"s2hk": "Simplified Chinese to Traditional Chinese (Hong Kong variant)",
"hk2s": "Traditional Chinese (Hong Kong variant) to Simplified Chinese",
"s2twp": "Simplified Chinese to Traditional Chinese (Taiwan variant)",
"tw2sp": "Traditional Chinese (Taiwan variant) to Simplified Chinese",
"t2tw": "Traditional Chinese (OpenCC Standard) to Taiwan Standard",
"hk2t": "Traditional Chinese (Hong Kong variant) to Traditional Chinese",
"t2hk": "Traditional Chinese (OpenCC Standard) to Hong Kong variant",
"t2jp": "Traditional Chinese Characters (Kyūjitai) to New Japanese Kanji",
"jp2t": "New Japanese Kanji to Traditional Chinese Characters (Kyūjitai)",
"tw2t": "Traditional Chinese (OpenCC Standard) to Traditional Chinese (Taiwan standard)",
}
def download_button(data: bytes, download_filename: str) -> None:
b64 = base64.b64encode(data).decode()
dl_link = f"""
<html>
<head>
<title>Start Auto Download file</title>
<script>
const a = document.createElement('a')
a.setAttribute('href', "data:text/csv;base64,{b64}")
a.setAttribute('download', "{download_filename}")
a.click()
</script>
</head>
</html>
"""
components.html(dl_link, height=0)
st.set_page_config(
page_title="EPUBHV, a toolset to convert your EPUB",
page_icon="📖",
layout="centered",
initial_sidebar_state="auto",
menu_items=None,
)
st.header("📖 EPUBHV, a toolset to convert your EPUB", divider="rainbow")
st.caption(
"Author: [@yihong0618](https://github.com/yihong0618) | [GitHub](https://github.com/yihong0618/epubhv) | [PyPI](https://pypi.org/project/epubhv/)",
)
def run():
if st.session_state["epubfile"] is None:
st.error("Please upload an epub file")
return
epubfile = st.session_state["epubfile"]
with tempfile.TemporaryDirectory() as tmpdir, st.spinner("Processing..."):
with open(Path(tmpdir) / epubfile.name, "wb") as f:
f.write(epubfile.read())
convert = st.session_state["convert"]
epubhv = EPUBHV(
file_path=Path(tmpdir) / epubfile.name,
need_ruby=st.session_state["need_ruby"],
need_cantonese=st.session_state["need_cantonese"],
convert_to=None if convert == "none" else convert,
convert_punctuation=st.session_state["punctuation"],
)
result = epubhv.run(method=st.session_state["method"], dest=Path(tmpdir))
download_button(result.read_bytes(), result.name)
with st.form(key="my_form"):
epubfile = st.file_uploader("Upload an epub file", type="epub", key="epubfile")
method = st.radio(
"Choose a method",
("to_vertical", "to_horizontal"),
format_func=lambda x: x.replace("_", " ").title(),
horizontal=True,
key="method",
)
need_ruby = st.checkbox("Need ruby", key="need_ruby")
need_cantonese = st.checkbox("Need cantonese", key="need_cantonese")
convert = st.selectbox(
"Transform text",
options=[
"none",
"s2t",
"t2s",
"s2tw",
"tw2s",
"s2hk",
"hk2s",
"s2twp",
"tw2sp",
"t2tw",
"hk2t",
"t2hk",
"t2jp",
"jp2t",
"tw2t",
],
format_func=LABELS.__getitem__,
key="convert",
)
punctuation = st.selectbox(
"Transform punctuation",
options=["auto", "t2s", "s2t", "none"],
format_func=LABELS.__getitem__,
key="punctuation",
)
st.form_submit_button(label="Transform", on_click=run)