Skip to content

Commit

Permalink
refactor: reusable menutab display labels
Browse files Browse the repository at this point in the history
  • Loading branch information
devxpy committed Feb 12, 2024
1 parent 09d02c6 commit bed56e5
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 64 deletions.
7 changes: 6 additions & 1 deletion daras_ai_v2/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,12 @@ def _render_header(self):

if tbreadcrumbs:
with st.tag("div", className="me-3 mb-1 mb-lg-0 py-2 py-lg-0"):
render_breadcrumbs(tbreadcrumbs, current_run.is_api_call)
render_breadcrumbs(
tbreadcrumbs,
is_api_call=(
current_run.is_api_call and self.tab == MenuTabs.run
),
)

author = self.run_user or current_run.get_creator()
if not is_root_example:
Expand Down
65 changes: 23 additions & 42 deletions daras_ai_v2/breadcrumbs.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def has_breadcrumbs(self):
return bool(self.root_title or self.published_title)


def render_breadcrumbs(breadcrumbs: TitleBreadCrumbs, is_api_call: bool = False):
def render_breadcrumbs(breadcrumbs: TitleBreadCrumbs, *, is_api_call: bool = False):
st.html(
"""
<style>
Expand Down Expand Up @@ -92,61 +92,42 @@ def get_title_breadcrumbs(
root_breadcrumb = TitleUrl(metadata.short_title, page_cls.app_url())

match tab:
case MenuTabs.run if is_root:
return TitleBreadCrumbs(page_cls.get_recipe_title(), None, None)
case MenuTabs.run if is_example:
assert pr is not None
return TitleBreadCrumbs(
pr.title or prompt_title or recipe_title,
root_title=root_breadcrumb,
published_title=None,
)
case MenuTabs.run if is_run:
return TitleBreadCrumbs(
prompt_title or f"Run: {recipe_title}",
root_title=root_breadcrumb,
published_title=TitleUrl(
pr.title or f"Fork: {pr.published_run_id}",
pr.get_app_url(),
)
if pr and not pr.is_root()
else None,
)
case MenuTabs.examples:
case MenuTabs.examples | MenuTabs.history | MenuTabs.saved:
label = MenuTabs.display_labels[tab]
return TitleBreadCrumbs(
f"Examples: {metadata.short_title}",
f"{label}: {metadata.short_title}",
root_title=root_breadcrumb,
published_title=None,
)
case MenuTabs.run_as_api:
tbreadcrumbs_on_run = get_title_breadcrumbs(
page_cls=page_cls, sr=sr, pr=pr, tab=MenuTabs.run
)
case MenuTabs.run_as_api | MenuTabs.integrations:
label = MenuTabs.display_labels[tab]
tbreadcrumbs_on_run = get_title_breadcrumbs(page_cls=page_cls, sr=sr, pr=pr)
return TitleBreadCrumbs(
f"API: {tbreadcrumbs_on_run.h1_title}",
f"{label}: {tbreadcrumbs_on_run.h1_title}",
root_title=tbreadcrumbs_on_run.root_title or root_breadcrumb,
published_title=tbreadcrumbs_on_run.published_title,
)
case MenuTabs.integrations:
tbreadcrumbs_on_run = get_title_breadcrumbs(
page_cls=page_cls, sr=sr, pr=pr, tab=MenuTabs.run
)
return TitleBreadCrumbs(
f"Integrations: {tbreadcrumbs_on_run.h1_title}",
root_title=tbreadcrumbs_on_run.root_title or root_breadcrumb,
published_title=tbreadcrumbs_on_run.published_title,
)
case MenuTabs.history:
case _ if is_root:
return TitleBreadCrumbs(page_cls.get_recipe_title(), None, None)
case _ if is_example:
assert pr is not None
return TitleBreadCrumbs(
f"History: {metadata.short_title}",
pr.title or prompt_title or recipe_title,
root_title=root_breadcrumb,
published_title=None,
)
case MenuTabs.saved:
case _ if is_run:
if pr and not pr.is_root():
published_title = TitleUrl(
pr.title or f"Fork: {pr.published_run_id}",
pr.get_app_url(),
)
else:
published_title = None
return TitleBreadCrumbs(
f"Saved Runs: {metadata.short_title}",
prompt_title or f"Run: {recipe_title}",
root_title=root_breadcrumb,
published_title=None,
published_title=published_title,
)
case _:
raise ValueError(f"Unknown tab: {tab}")
35 changes: 14 additions & 21 deletions daras_ai_v2/meta_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,27 +111,20 @@ def meta_title_for_page(
pr: PublishedRun | None,
tab: str,
) -> str:
suffix = f" {sep} Gooey.AI"

match tab:
case MenuTabs.examples:
return f"Examples: {metadata.meta_title}" + suffix
case MenuTabs.run_as_api:
return "API: " + meta_title_for_page(
page=page, metadata=metadata, sr=sr, pr=pr, tab=MenuTabs.run
)
case MenuTabs.integrations:
return "Integrations: " + meta_title_for_page(
page=page, metadata=metadata, sr=sr, pr=pr, tab=MenuTabs.run
)
case MenuTabs.history:
return f"History for {metadata.short_title}" + suffix
case MenuTabs.saved:
return f"Saved Runs for {metadata.short_title}" + suffix
case MenuTabs.run if pr and pr.saved_run == sr and pr.is_root():
label = MenuTabs.display_labels[tab]
ret = f"{label}: {metadata.meta_title}"
case MenuTabs.run_as_api | MenuTabs.integrations:
label = MenuTabs.display_labels[tab]
return f"{label} for {meta_title_for_page(page=page, metadata=metadata, sr=sr, pr=pr, tab=MenuTabs.run)}"
case MenuTabs.history | MenuTabs.saved:
label = MenuTabs.display_labels[tab]
ret = f"{label} for {metadata.short_title}"
case _ if pr and pr.saved_run == sr and pr.is_root():
# for root page
return metadata.meta_title + suffix
case MenuTabs.run:
ret = metadata.meta_title
case _:
# non-root runs and examples
parts = []

Expand All @@ -148,9 +141,9 @@ def meta_title_for_page(
part += f" by {user.display_name}"
parts.append(part)

return sep.join(parts) + suffix
case _:
raise ValueError(f"Unknown tab: {tab}")
ret = sep.join(parts)

return f"{ret} {sep} Gooey.AI"


def meta_description_for_page(
Expand Down
8 changes: 8 additions & 0 deletions daras_ai_v2/tabs_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ class MenuTabs:
}
paths_reverse = {v: k for k, v in paths.items()}

display_labels = {
examples: "Examples",
history: "History",
saved: "Saved Runs",
run_as_api: "API",
integrations: "Integrations",
}


def page_tabs(*, tabs, key=None):
selected_menu = option_menu(
Expand Down

0 comments on commit bed56e5

Please sign in to comment.