Skip to content

Commit

Permalink
Refactor caraml ai app into generic streamlit placeholders
Browse files Browse the repository at this point in the history
  • Loading branch information
deadlycoconuts committed Jul 27, 2024
1 parent ab32304 commit b5afa85
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 41 deletions.
5 changes: 2 additions & 3 deletions api/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,8 @@ type UIConfig struct {
StaticPath string `validated:"required"`
IndexPath string `validated:"required"`

ClockworkUIHomepage string `json:"REACT_APP_CLOCKWORK_UI_HOMEPAGE"`
KubeflowUIHomepage string `json:"REACT_APP_KUBEFLOW_UI_HOMEPAGE"`
CaramlAIStreamlitHomepage string `json:"REACT_APP_CARAML_AI_STREAMLIT_HOMEPAGE"`
ClockworkUIHomepage string `json:"REACT_APP_CLOCKWORK_UI_HOMEPAGE"`
KubeflowUIHomepage string `json:"REACT_APP_KUBEFLOW_UI_HOMEPAGE"`

AllowCustomStream bool `json:"REACT_APP_ALLOW_CUSTOM_STREAM"`
AllowCustomTeam bool `json:"REACT_APP_ALLOW_CUSTOM_TEAM"`
Expand Down
15 changes: 10 additions & 5 deletions api/models/v2/application.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package models

type Application struct {
Name string `json:"name" validate:"required"`
Description string `json:"description"`
Homepage string `json:"homepage"`
Configuration *ApplicationConfig `json:"config" validate:"dive"`
IsProjectAgnostic bool `json:"is_project_agnostic"`
Name string `json:"name" validate:"required"`
Description string `json:"description"`
Homepage string `json:"homepage"`
Configuration *ApplicationConfig `json:"config" validate:"dive"`
IsProjectAgnostic bool `json:"is_project_agnostic"`
StreamlitPlaceholderPageConfig *StreamlitPlaceholderPageConfig `json:"streamlit_placeholder_page_config"`
}

type ApplicationConfig struct {
Expand All @@ -18,3 +19,7 @@ type NavigationMenuItem struct {
Label string `json:"label"`
Destination string `json:"destination"`
}

type StreamlitPlaceholderPageConfig struct {
StreamlitURL string `json:"streamlit_url"`
}
10 changes: 2 additions & 8 deletions ui/packages/app/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
ErrorBoundary,
Login,
MlpApiContextProvider,
ApplicationsContextProvider,
Page404,
Toast
} from "@caraml-dev/ui-lib";
Expand All @@ -12,7 +13,6 @@ import { Route, Routes } from "react-router-dom";
import AppRoutes from "./AppRoutes";
import { PrivateLayout } from "./PrivateLayout";
import config from "./config";
import { CaraMLAIPage } from "./caraml_ai/CaraMLAIPage";

const App = () => (
<EuiProvider>
Expand All @@ -26,17 +26,11 @@ const App = () => (
<Route path="/login" element={<Login />} />

<Route element={<PrivateLayout />}>
<Route path="/*" element={<AppRoutes />} />
<Route path="/*" element={<ApplicationsContextProvider><AppRoutes /></ApplicationsContextProvider>} />
</Route>

<Route path="/pages/404" element={<Page404 />} />

{
config.CARAML_AI_STREAMLIT_HOMEPAGE &&
<Route element={<PrivateLayout />}>
<Route path="/caraml-ai" element={<CaraMLAIPage />} />
</Route>
}
</Routes>
<Toast />
</AuthProvider>
Expand Down
47 changes: 30 additions & 17 deletions ui/packages/app/src/AppRoutes.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,39 @@
import React from "react";
import React, { useContext } from "react";
import { Home, Project } from "./pages";
import { ProjectCreation } from "./project_setting/ProjectCreation";
import ProjectSetting from "./project_setting/ProjectSetting";
import { Navigate, Route, Routes } from "react-router-dom";
import { StreamlitPlaceholderPage } from "./streamlit_placeholder_page/StreamlitPlaceholderPage";
import { ApplicationsContext } from "@caraml-dev/ui-lib";

export const AppRoutes = () => (
<Routes>
{/* LANDING */}
<Route index element={<Home />} />
export const AppRoutes = () => {
const { apps, isLoaded } = useContext(ApplicationsContext);

<Route path="projects">
{/* PROJECT LANDING PAGE */}
<Route path=":projectId" element={<Project />} />
{/* PROJECT SETTING */}
<Route path=":projectId/settings/*" element={<ProjectSetting />} />
{/* New Project */}
<Route path="create" element={<ProjectCreation />} />
</Route>
// If the apps have not been loaded yet, we do not render any of the app related routes - the additional streamlit
// apps need to be retrieved from the MLP API v2/applications endpoint first before we generate each route for them.
// Once those apps are loaded, AppRoutes will be re-rendered,
return isLoaded && (
<Routes>
{/* LANDING */}
<Route index element={<Home />} />

{/* DEFAULT */}
<Route path="*" element={<Navigate to="/pages/404" replace={true} />} />
</Routes>
);
{apps?.map(app => !!app.streamlit_placeholder_page_config &&
<Route key={app.name} path={app.homepage} element={<StreamlitPlaceholderPage app={app} />} />)
}

<Route path="projects">
{/* PROJECT LANDING PAGE */}
<Route path=":projectId" element={<Project />} />
{/* PROJECT SETTING */}
<Route path=":projectId/settings/*" element={<ProjectSetting />} />
{/* New Project */}
<Route path="create" element={<ProjectCreation />} />
</Route>

{/* DEFAULT */}
<Route path="*" element={<Navigate to="/pages/404" replace={true} />} />
</Routes>
)
};

export default AppRoutes;
1 change: 0 additions & 1 deletion ui/packages/app/src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ const config = {
),
CLOCKWORK_UI_HOMEPAGE: getEnv("REACT_APP_CLOCKWORK_UI_HOMEPAGE"),
KUBEFLOW_UI_HOMEPAGE: getEnv("REACT_APP_KUBEFLOW_UI_HOMEPAGE"),
CARAML_AI_STREAMLIT_HOMEPAGE: getEnv("REACT_APP_CARAML_AI_STREAMLIT_HOMEPAGE"),
ALLOW_CUSTOM_STREAM:
getEnv("REACT_APP_ALLOW_CUSTOM_STREAM") != null
? getEnv("REACT_APP_ALLOW_CUSTOM_STREAM")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import React from "react";
import { EuiPageTemplate } from "@elastic/eui";
import config from "./../config";

export const CaraMLAIPage = () => {
const iframe = `<iframe src=${config.CARAML_AI_STREAMLIT_HOMEPAGE} style="height: 80vh; width: 100%;"></iframe>`
export const StreamlitPlaceholderPage = ({ app }) => {
const iframe = `<iframe src=${app.streamlit_placeholder_page_config.streamlit_url} style="height: 80vh; width: 100%;"></iframe>`

function Iframe(props) {
return (<div dangerouslySetInnerHTML={ {__html: props.iframe?props.iframe:""}} />);
Expand All @@ -12,8 +11,8 @@ export const CaraMLAIPage = () => {
<EuiPageTemplate restrictWidth="90%" panelled={false}>
<EuiPageTemplate.Header
bottomBorder={false}
iconType="timelionApp"
pageTitle="CaraML AI"
iconType={app.config.icon}
pageTitle={app.name}
/>

<EuiPageTemplate.Section paddingSize="none">
Expand Down
4 changes: 2 additions & 2 deletions ui/packages/lib/src/providers/application/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ const ApplicationsContext = React.createContext({

export const ApplicationsContextProvider = ({ children }) => {
const location = useLocation();
const [{ data: apps }] = useMlpApi("/v2/applications", {}, []);
const [{ data: apps, isLoaded }] = useMlpApi("/v2/applications", {}, []);

const currentApp = useMemo(
() => apps.find(a => location.pathname.startsWith(a.homepage)),
[apps, location.pathname]
);

return (
<ApplicationsContext.Provider value={{ currentApp, apps }}>
<ApplicationsContext.Provider value={{ currentApp, apps, isLoaded }}>
{children}
</ApplicationsContext.Provider>
);
Expand Down

0 comments on commit b5afa85

Please sign in to comment.