Skip to content

Commit

Permalink
Feat/webapp (#16)
Browse files Browse the repository at this point in the history
feat: webapp using streamlit

---------

Signed-off-by: Frost Ming <[email protected]>
Co-authored-by: yihong <[email protected]>
  • Loading branch information
frostming and yihong0618 authored Jan 4, 2024
1 parent d177558 commit 8235a03
Show file tree
Hide file tree
Showing 8 changed files with 1,229 additions and 15 deletions.
21 changes: 18 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,22 @@ git clone https://github.com/yihong0618/epubhv.git
cd epubhv && bash ./setup.sh
```

## Use
## Using pipx

If you are using [pipx](https://pypi.org/project/pipx/), you can directly run `epubhv` with:

```console
pipx run epubhv a.epub
```

## Use the web

```console
pip install epubhv[web]
streamlit run web.py
```

## Use CLI

```console
epubhv a.epub # will generate a file a-v.epub that is vertical
Expand All @@ -29,13 +44,13 @@ epubhv tests/test_epub # will generate all epub files to epub-v

# you can specify the punctuation style
epubhv e.epub --convert s2t --punctuation auto
# you can add `ruby` for Japanese(furigana) and Chinese(pinyin)
# you can add `ruby` for Japanese(furigana) and Chinese(pinyin)
epubhv e.epub --h --ruby
# if you want to learn `cantonese` 粤语
epubhv f.epub --h --ruby --cantonese
```

**About [cantonese](https://jyutping.org/docs/cantonese/)**
**About [cantonese](https://jyutping.org/docs/cantonese/)**

## Contribution

Expand Down
1 change: 1 addition & 0 deletions epubhv/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .epubhv import EPUBHV as EPUBHV
2 changes: 1 addition & 1 deletion epubhv/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def main() -> None:
parser.add_argument(
"--punctuation",
dest="punctuation",
choices=["auto", "t2s", "s2t", "s2s", "t2t", "none"],
choices=["auto", "t2s", "s2t", "none"],
default="auto",
help="""convert punctuation to specific locale and direction (default: auto)
Expand Down
7 changes: 4 additions & 3 deletions epubhv/epubhv.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ def convert(self, method: str = "to_vertical") -> None:
with open(html_file, "w", encoding="utf-8") as file:
file.write(ruby_soup.prettify())

def pack(self, method: str = "to_vertical", dest: Path = Path.cwd()) -> None:
def pack(self, method: str = "to_vertical", dest: Path = Path.cwd()) -> Path:
lang = "original"
if self.convert_to is not None:
lang = self.convert_to
Expand All @@ -395,8 +395,9 @@ def pack(self, method: str = "to_vertical", dest: Path = Path.cwd()) -> None:
)
os.rename(src=f"{pack_to}.zip", dst=pack_to)
shutil.rmtree(self.book_path)
return pack_to

def run(self, method: str = "to_vertical", dest: Path = Path.cwd()) -> None:
def run(self, method: str = "to_vertical", dest: Path = Path.cwd()) -> Path:
assert method in [
"to_horizontal",
"to_vertical",
Expand All @@ -411,4 +412,4 @@ def run(self, method: str = "to_vertical", dest: Path = Path.cwd()) -> None:
raise Exception("Only support epub to vertical or horizontal for now")

self.convert(method=method)
self.pack(method=method, dest=dest)
return self.pack(method=method, dest=dest)
1,018 changes: 1,011 additions & 7 deletions pdm.lock

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ dependencies = [
"ToJyutping",
]

requires-python = ">=3.8"
requires-python = ">=3.8,!=3.9.7"
readme = "README.md"
license = { text = "MIT" }

Expand All @@ -28,6 +28,11 @@ Homepage = "https://github.com/yihong0618/epubhv"

[project.scripts]
epubhv = "epubhv.cli:main"

[project.optional-dependencies]
web = [
"streamlit>=1.29.0",
]
[build-system]
requires = ["pdm-backend"]
build-backend = "pdm.backend"
Expand Down
68 changes: 68 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# This file is @generated by PDM.
# Please do not edit it manually.

altair==5.2.0
attrs==23.2.0
backports-zoneinfo==0.2.1; python_version < "3.9"
beautifulsoup4==4.12.2
blinker==1.7.0
cachetools==5.3.2
certifi==2023.11.17
charset-normalizer==3.3.2
click==8.1.7
colorama==0.4.6; sys_platform == "win32" or platform_system == "Windows"
cssutils==2.7.1
docstring-parser==0.15
fugashi==1.3.0
gitdb==4.0.11
gitpython==3.1.40
idna==3.6
importlib-metadata==6.11.0
importlib-resources==6.1.1; python_version < "3.9"
jieba==0.42.1
jinja2==3.1.2
jsonschema==4.20.0
jsonschema-specifications==2023.12.1
langdetect==1.0.9
lxml==4.9.3
markdown-it-py==3.0.0
markupsafe==2.1.3
mdurl==0.1.2
mypy-extensions==1.0.0
numpy==1.24.4
opencc-python-reimplemented==0.1.7
packaging==23.2
pandas==2.0.3
pillow==10.2.0
pkgutil-resolve-name==1.3.10; python_version < "3.9"
protobuf==4.25.1
pyarrow==14.0.2
pydeck==0.8.1b0
pygments==2.17.2
pygtrie==2.5.0
pypinyin==0.50.0
python-dateutil==2.8.2
pytz==2023.3.post1
referencing==0.32.0
requests==2.31.0
rich==13.7.0
rpds-py==0.16.2
six==1.16.0
smmap==5.0.1
soupsieve==2.5
streamlit==1.29.0
tenacity==8.2.3
tojyutping==0.2.3
toml==0.10.2
toolz==0.12.0
tornado==6.4
typed-argument-parser==1.8.1
typing-extensions==4.9.0
typing-inspect==0.9.0
tzdata==2023.4
tzlocal==5.2
unidic-lite==1.0.8
urllib3==2.1.0
validators==0.22.0
watchdog==3.0.0; platform_system != "Darwin"
zipp==3.17.0
120 changes: 120 additions & 0 deletions web.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
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 src="http://code.jquery.com/jquery-3.2.1.min.js"></script>
<script>
$('<a href="data:text/csv;base64,{b64}" download="{download_filename}">')[0].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)

0 comments on commit 8235a03

Please sign in to comment.