Skip to content

Commit

Permalink
show version in the Help page
Browse files Browse the repository at this point in the history
  • Loading branch information
lone17 committed May 15, 2024
1 parent 1ef5aa4 commit 99808d6
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 25 deletions.
9 changes: 9 additions & 0 deletions flowsettings.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
from importlib.metadata import version
from inspect import currentframe, getframeinfo
from pathlib import Path

Expand All @@ -14,6 +15,14 @@
# change this if your app use a different name
KH_PACKAGE_NAME = "kotaemon_app"

KH_APP_VERSION = None
try:
# Caution: This might produce the wrong version
# https://stackoverflow.com/a/59533071
KH_APP_VERSION = version(KH_PACKAGE_NAME)
except Exception as e:
print(f"Failed to get app version: {e}")

# App can be ran from anywhere and it's not trivial to decide where to store app data.
# So let's use the same directory as the flowsetting.py file.
KH_APP_DATA_DIR = this_dir / "ktem_app_data"
Expand Down
65 changes: 40 additions & 25 deletions libs/ktem/ktem/pages/help.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@
import requests
from theflow.settings import settings

CHANGELOG_CACHE_DIR = Path(settings.KH_APP_DATA_DIR) / "changelogs"


def get_remote_doc(url):
def get_remote_doc(url: str) -> str:
try:
res = requests.get(url)
return res.text
Expand All @@ -17,40 +15,34 @@ def get_remote_doc(url):
return ""


def get_changelogs(version):
# try retrieve from cache
if (CHANGELOG_CACHE_DIR / f"{version}.md").exists():
with open(CHANGELOG_CACHE_DIR / f"{version}.md", "r") as fi:
return fi.read()

release_url = f"https://api.github.com/repos/Cinnamon/kotaemon/releases/{version}"
def download_changelogs(release_url: str) -> str:
try:
res = requests.get(release_url).json()
changelogs = res.get("body", "")

# cache the changelogs
with open(CHANGELOG_CACHE_DIR / f"{version}.md", "w") as fi:
fi.write(changelogs)

return changelogs
except Exception as e:
print(f"Failed to fetch changelogs from {release_url}: {e}")
return ""


class HelpPage:
def __init__(self, app):
def __init__(
self,
app,
doc_dir: str = settings.KH_DOC_DIR,
remote_content_url: str = "https://raw.githubusercontent.com/Cinnamon/kotaemon",
app_version: str | None = settings.KH_APP_VERSION,
changelogs_cache_dir: str
| Path = (Path(settings.KH_APP_DATA_DIR) / "changelogs"),
):
self._app = app
self.doc_dir = Path(settings.KH_DOC_DIR)
self.remote_content_url = "https://raw.githubusercontent.com/Cinnamon/kotaemon"
self.doc_dir = Path(doc_dir)
self.remote_content_url = remote_content_url
self.app_version = app_version
self.changelogs_cache_dir = Path(changelogs_cache_dir)

self.app_version = None
try:
# Caution: This might produce the wrong version
# https://stackoverflow.com/a/59533071
self.app_version = version(settings.KH_PACKAGE_NAME)
except Exception as e:
print(f"Failed to get app version: {e}")
self.changelogs_cache_dir.mkdir(parents=True, exist_ok=True)

about_md_dir = self.doc_dir / "about.md"
if about_md_dir.exists():
Expand All @@ -62,6 +54,8 @@ def __init__(self, app):
)
if about_md:
with gr.Accordion("About"):
if self.app_version:
about_md = f"Version: {self.app_version}\n\n{about_md}"
gr.Markdown(about_md)

user_guide_md_dir = self.doc_dir / "usage.md"
Expand All @@ -77,7 +71,28 @@ def __init__(self, app):
gr.Markdown(user_guide_md)

if self.app_version:
changelogs = get_changelogs("tags/v" + self.app_version)
# try retrieve from cache
changelogs = ""

if (self.changelogs_cache_dir / f"{version}.md").exists():
with open(self.changelogs_cache_dir / f"{version}.md", "r") as fi:
changelogs = fi.read()
else:
release_url_base = (
"https://api.github.com/repos/Cinnamon/kotaemon/releases"
)
changelogs = download_changelogs(
release_url=f"{release_url_base}/tags/v{self.app_version}"
)

# cache the changelogs
if not self.changelogs_cache_dir.exists():
self.changelogs_cache_dir.mkdir(parents=True, exist_ok=True)
with open(
self.changelogs_cache_dir / f"{self.app_version}.md", "w"
) as fi:
fi.write(changelogs)

if changelogs:
with gr.Accordion(f"Changelogs (v{self.app_version})"):
gr.Markdown(changelogs)

0 comments on commit 99808d6

Please sign in to comment.