Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move to modules instead of classes for home page and common interface #717

Merged
merged 3 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions data_explorer/app/config.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
"""General configuration for the app."""

SESSION_STATE_VARIABLES = [
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should aim to get rid of these completely.

This is currently used to initialize all session state variables at the start of the application, to prevent errors when accessing non-initialized values. But this brings us in broken states anyway. We should make sure the flow is set up so values are only accessed after initialization, or non-initialization is handled in the accessing code.

"base_path",
"pipeline",
"selected_pipeline_path",
"run",
"selected_run_path",
"available_runs",
"component",
"selected_component_path",
"partition",
Expand Down
55 changes: 55 additions & 0 deletions data_explorer/app/interfaces/common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
"""Interface for the data explorer app."""

import os

import streamlit as st
from interfaces.utils import get_index_from_state


def render_common_interface():
cols = st.columns(2)
with cols[0]:
_select_pipeline()
with cols[1]:
_select_run()


def _select_pipeline():
"""Selects a pipeline from available pipelines."""
available_pipelines = [
os.path.basename(item)
for item in st.session_state.file_system.ls(st.session_state.base_path)
]

selected_pipeline = st.selectbox(
"Pipeline",
options=available_pipelines,
index=get_index_from_state("pipeline", available_pipelines),
)
st.session_state.pipeline = selected_pipeline
st.session_state.pipeline_path = st.session_state.base_path / selected_pipeline


def _select_run():
"""Selects a run from available runs within the chosen pipeline."""
pipeline_path = st.session_state.base_path / st.session_state.pipeline

def has_manifest_file(path):
return any("manifest.json" in files for _, _, files in os.walk(path))

available_runs = []
for run in os.listdir(pipeline_path):
run_path = pipeline_path / run
if run_path.is_dir() and run != "cache" and has_manifest_file(run_path):
available_runs.append(os.path.basename(run))

available_runs.sort(reverse=True)

selected_run = st.selectbox(
"Run",
options=available_runs,
index=get_index_from_state("run", available_runs),
)
st.session_state.run = selected_run
st.session_state.run_path = st.session_state.pipeline_path / selected_run
st.session_state.available_runs = available_runs
145 changes: 0 additions & 145 deletions data_explorer/app/interfaces/common_interface.py
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file was split into common.py and sidebar.py

This file was deleted.

22 changes: 9 additions & 13 deletions data_explorer/app/interfaces/dataset_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,16 @@
from pathlib import Path

import dask.dataframe as dd
import fsspec
import pandas as pd
import streamlit as st
from config import DEFAULT_INDEX_NAME, ROWS_TO_RETURN
from fondant.core.manifest import Manifest
from fondant.core.schema import Field
from interfaces.common_interface import MainInterface
from interfaces.utils import get_default_index
from interfaces.utils import get_index_from_state


class DatasetLoaderApp(MainInterface):
def __init__(self):
super().__init__()

class DatasetLoaderApp:
@staticmethod
def _select_component():
"""Select component from available components."""
Expand All @@ -27,13 +24,12 @@ def _select_component():
os.path.basename(item) for item in os.listdir(selected_run_path)
]

default_index = get_default_index("component", available_components)
selected_component = st.selectbox(
"Component",
available_components,
default_index,
options=available_components,
index=get_index_from_state("component", available_components),
)
selected_component_path = os.path.join(selected_run_path, selected_component)
selected_component_path = selected_run_path / selected_component

st.session_state["component"] = selected_component
st.session_state["selected_component_path"] = selected_component_path
Expand Down Expand Up @@ -74,9 +70,9 @@ def _get_field_location(self, manifest: Manifest, field_name: str) -> str:
base_path = st.session_state["base_path"]
field_location = manifest.get_field_location(field_name)

if (
os.path.ismount(base_path) is False
and self.fs.__class__.__name__ == "LocalFileSystem"
if os.path.ismount(base_path) is False and isinstance(
st.session_state.file_system,
fsspec.implementations.local.LocalFileSystem,
):
# Used for local development when running the app locally
field_location = os.path.join(
Expand Down
22 changes: 22 additions & 0 deletions data_explorer/app/interfaces/sidebar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import streamlit as st
from streamlit_extras.app_logo import add_logo


def render_sidebar():
add_logo("content/fondant_logo.png")

with st.sidebar:
# Increase the width of the sidebar to accommodate logo
st.markdown(
"""
<style>
section[data-testid="stSidebar"] {
width: 350px !important;
}
</style>
""",
unsafe_allow_html=True,
)

with st.expander("## General Configuration"):
st.markdown(f"### Base path: \n {st.session_state.base_path}")
7 changes: 4 additions & 3 deletions data_explorer/app/interfaces/utils.py
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a fix for an issue raised by @Hakimovich99, where you cannot switch pipelines because the run selection would error.

Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
import streamlit as st


def get_default_index(key: str, option_list: list) -> int:
def get_index_from_state(key: str, option_list: list) -> int:
"""Get the default index for a selectbox based on previous selection from session state."""
if st.session_state[key] is None:
selected_option = st.session_state.get(key)
if selected_option is None or selected_option not in option_list:
return 0

return option_list.index(st.session_state[key])
return option_list.index(selected_option)
Loading
Loading