Skip to content

Commit

Permalink
Ux/consolidate space mgmt (#89)
Browse files Browse the repository at this point in the history
* chore: Re-arrange admin docs layout.

* chore: Update admin docs UI layout.

* chore: Rename consolidated space to Admin Spaces.

* Enhancement/make selector sticky (#84)

* feat: Make the space selector box stick to the top of the page.

* chore: Add custom styling for personal ask.

* chore: Remove unused imports.

* chore: Add an avatar to chat messages

* chore: Added script to format avatars.

* feat: Make the space selector box stick to the top of the page.

* chore: Remove unused imports.

* Update user avatar settings.

* chore: Attach event listeners to space selector.

* chore: Cleanup imports.

* chore: Update chat_ui script.

* feat: Make the space selector box stick to the top of the page.

* chore: Use username as email for gravatar requests.

* chore: Use identicon as avatar default

* chore: Set username in auth session state.

* chore: Bump llama-index to 0.8.5.post2

* Update get username function
  • Loading branch information
osala-eng committed Sep 4, 2023
1 parent 02f4fbf commit 5048f66
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 73 deletions.
28 changes: 5 additions & 23 deletions web/admin_docs.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,16 @@
"""Page: Admin / Manage Documents."""

import streamlit as st
from docq.config import SpaceType
from docq.domain import SpaceKey
from st_pages import add_page_title
from utils.handlers import list_shared_spaces
from utils.layout import auth_required, documents_ui, show_space_details_ui
from utils.layout import admin_docs_ui, auth_required, create_space_ui

auth_required(requiring_admin=True)

add_page_title()

PARAM_NAME = "sid"
st.session_state["admin_docs_alert"] = st.empty()

if PARAM_NAME in st.experimental_get_query_params():
space = SpaceKey(SpaceType.SHARED, int(st.experimental_get_query_params()[PARAM_NAME][0]))

tab_spaces, tab_docs = st.tabs(["Space Details", "Manage Documents"])

with tab_docs:
documents_ui(space)

with tab_spaces:
show_space_details_ui(space)


else:
st.subheader("Select a space from below:")
spaces = list_shared_spaces()
selected = st.selectbox("Spaces", spaces, format_func=lambda x: x[1])
## Create Space UI
create_space_ui()

if selected:
st.experimental_set_query_params(**{PARAM_NAME: selected[0]})
admin_docs_ui(PARAM_NAME)
13 changes: 9 additions & 4 deletions web/admin_spaces.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
"""Page: Admin / Manage Spaces."""

"""Page: Admin / Manage Documents."""
import streamlit as st
from st_pages import add_page_title
from utils.layout import auth_required, create_space_ui, list_spaces_ui
from utils.layout import admin_docs_ui, auth_required, create_space_ui

auth_required(requiring_admin=True)

add_page_title()

PARAM_NAME = "sid"
st.session_state["admin_docs_alert"] = st.empty()

## Create Space UI
create_space_ui()
list_spaces_ui(True)

admin_docs_ui(PARAM_NAME)
1 change: 0 additions & 1 deletion web/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
Page("web/admin_settings.py", "Admin_Settings"),
Page("web/admin_spaces.py", "Admin_Spaces"),
Page("web/admin_space_groups.py", "Admin_Space_Groups"),
Page("web/admin_docs.py", "Admin_Docs"),
Page("web/admin_users.py", "Admin_Users"),
Page("web/admin_user_groups.py", "Admin_User_Groups"),
Page("web/admin_logs.py", "Admin_Logs"),
Expand Down
164 changes: 119 additions & 45 deletions web/utils/layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import streamlit as st
from docq.access_control.main import SpaceAccessType
from docq.config import FeatureType, LogType, SystemSettingsKey
from docq.config import FeatureType, LogType, SpaceType, SystemSettingsKey
from docq.domain import DocumentListItem, FeatureKey, SpaceKey
from st_pages import hide_pages
from streamlit.components.v1 import html
Expand Down Expand Up @@ -418,10 +418,15 @@ def chat_ui(feature: FeatureKey) -> None:
key=f"chat_shared_spaces_{feature.value()}",
)
st.checkbox("Including your documents", value=True, key="chat_personal_space")
if st.button("Load chat history earlier"):
query_chat_history(feature)
if st.button("New chat"):
handle_create_new_chat(feature)

load_history, create_new_chat = st.columns([3, 1])
with load_history:
if st.button("Load chat history earlier"):
query_chat_history(feature)
with create_new_chat:
if st.button("New chat"):
handle_create_new_chat(feature)

day = format_datetime(get_chat_session(feature.type_, SessionKeyNameForChat.CUTOFF))
st.markdown(f"#### {day}")

Expand Down Expand Up @@ -536,10 +541,10 @@ def _render_space_data_source_config_input_fields(data_source: Tuple, prefix: st
)


def create_space_ui() -> None:
def create_space_ui(expanded: bool = False) -> None:
"""Create a new space."""
data_sources = list_space_data_source_choices()
with st.expander("### + New Space"):
with st.expander("### + New Space", expanded=expanded):
st.text_input("Name", value="", key="create_space_name")
st.text_input("Summary", value="", key="create_space_summary")
ds = st.selectbox(
Expand All @@ -550,7 +555,14 @@ def create_space_ui() -> None:
)
if ds:
_render_space_data_source_config_input_fields(ds, "create_space_")
st.button("Create Space", on_click=handle_create_space)
if st.button("Create Space"):
space = handle_create_space()
alert: DeltaGenerator = st.session_state["admin_docs_alert"]
if isinstance(space, SpaceKey) and alert:
alert.success(f"Successfully created space with ID: {space.id_}")
st.session_state["admin_docs_active_space"] = space.id_
elif alert:
alert.error(f"Failed to create space. Error: {space}")


def _render_view_space_details_with_container(
Expand All @@ -568,52 +580,63 @@ def _render_view_space_details_with_container(
return container


def _render_edit_space_details(space_data: Tuple, data_source: Tuple) -> None:
def _render_edit_space_details_form(space_data: Tuple, data_source: Tuple) -> None:
id_, name, summary, archived, ds_type, ds_configs, _, _ = space_data
with st.form(key=f"update_space_details_{id_}"):
st.text_input("Name", value=name, key=f"update_space_details_{id_}_name")
st.text_input("Summary", value=summary, key=f"update_space_details_{id_}_summary")
st.checkbox("Is Archived", value=archived, key=f"update_space_details_{id_}_archived")
st.selectbox(
"Data Source",
options=[data_source],
index=0,
key=f"update_space_details_{id_}_ds_type",
disabled=True,
format_func=lambda x: x[1],
)
_render_space_data_source_config_input_fields(data_source, f"update_space_details_{id_}_", ds_configs)
st.form_submit_button("Save", on_click=handle_update_space_details, args=(id_,))


def _render_edit_space_details(space_data: Tuple, data_source: Tuple) -> None:
id_, *_ = space_data

if st.button("Edit", key=f"update_space_{id_}_button"):
with st.form(key=f"update_space_details_{id_}"):
st.text_input("Name", value=name, key=f"update_space_details_{id_}_name")
st.text_input("Summary", value=summary, key=f"update_space_details_{id_}_summary")
st.checkbox("Is Archived", value=archived, key=f"update_space_details_{id_}_archived")
st.selectbox(
"Data Source",
options=[data_source],
index=0,
key=f"update_space_details_{id_}_ds_type",
disabled=True,
format_func=lambda x: x[1],
)
_render_space_data_source_config_input_fields(data_source, f"update_space_details_{id_}_", ds_configs)
st.form_submit_button("Save", on_click=handle_update_space_details, args=(id_,))
_render_edit_space_details_form(space_data, data_source)


def _render_manage_space_permissions(space_data: Tuple) -> None:
def _render_manage_space_permissions_form(space_data: Tuple) -> None:
id_, *_ = space_data
permissions = get_shared_space_permissions(id_)

with st.form(key=f"manage_space_permissions_{id_}"):
st.checkbox(
"Public Access",
value=permissions[SpaceAccessType.PUBLIC],
key=f"manage_space_permissions_{id_}_{SpaceAccessType.PUBLIC.name}",
)
st.multiselect(
"Users",
options=[(u[0], u[1]) for u in list_users()],
default=permissions[SpaceAccessType.USER],
key=f"manage_space_permissions_{id_}_{SpaceAccessType.USER.name}",
format_func=lambda x: x[1],
)
st.multiselect(
"Groups",
options=[(g[0], g[1]) for g in list_user_groups()],
default=permissions[SpaceAccessType.GROUP],
key=f"manage_space_permissions_{id_}_{SpaceAccessType.GROUP.name}",
format_func=lambda x: x[1],
)
st.form_submit_button("Save", on_click=handle_manage_space_permissions, args=(id_,))


def _render_manage_space_permissions(space_data: Tuple) -> None:
id_, *_ = space_data

if st.button("Permissions", key=f"manage_space_permissions_{id_}_button"):
with st.form(key=f"manage_space_permissions_{id_}"):
st.checkbox(
"Public Access",
value=permissions[SpaceAccessType.PUBLIC],
key=f"manage_space_permissions_{id_}_{SpaceAccessType.PUBLIC.name}",
)
st.multiselect(
"Users",
options=[(u[0], u[1]) for u in list_users()],
default=permissions[SpaceAccessType.USER],
key=f"manage_space_permissions_{id_}_{SpaceAccessType.USER.name}",
format_func=lambda x: x[1],
)
st.multiselect(
"Groups",
options=[(g[0], g[1]) for g in list_user_groups()],
default=permissions[SpaceAccessType.GROUP],
key=f"manage_space_permissions_{id_}_{SpaceAccessType.GROUP.name}",
format_func=lambda x: x[1],
)
st.form_submit_button("Save", on_click=handle_manage_space_permissions, args=(id_,))
_render_manage_space_permissions_form(space_data)


def list_spaces_ui(admin_access: bool = False) -> None:
Expand Down Expand Up @@ -645,3 +668,54 @@ def show_space_details_ui(space: SpaceKey) -> None:
def list_logs_ui(type_: LogType) -> None:
"""List logs per log type."""
st.info("Logs are coming soon.")


def _editor_view(q_param: str) -> None:
if q_param in st.experimental_get_query_params():
space = SpaceKey(SpaceType.SHARED, int(st.experimental_get_query_params()[q_param][0]))
s = get_shared_space(space.id_)
ds = get_space_data_source_choice_by_type(s[4])

tab_spaces, tab_docs, tab_edit, tab_permissions = st.tabs(
["Space Details", "Manage Documents", "Edit Space", "Permissions"]
)

with tab_docs:
documents_ui(space)
with tab_spaces:
show_space_details_ui(space)
with tab_edit:
_render_edit_space_details_form(s, ds)
with tab_permissions:
_render_manage_space_permissions_form(s)


def admin_docs_ui(q_param: str = None) -> None:
"""Manage Documents UI."""
st.subheader("Select a space from below:")
spaces = list_shared_spaces()

def _on_change() -> None:
del st.session_state["admin_docs_active_space"]

try: # Get the space id from the query param with prefence to the newly created space.
_sid = int(st.experimental_get_query_params()[q_param][0]) if q_param in st.experimental_get_query_params() else None
except ValueError:
_sid = None
new_space = st.session_state.get("admin_docs_active_space", _sid)
default_sid = next(
(i for i, s in enumerate(spaces) if s[0] == new_space), None
)

selected = st.selectbox(
"Spaces",
spaces,
format_func=lambda x: x[1],
on_change=_on_change,
label_visibility="collapsed",
index=default_sid if default_sid else 0,
)

if selected:
st.experimental_set_query_params(**{q_param: selected[0]})
_editor_view(q_param)

0 comments on commit 5048f66

Please sign in to comment.