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

Eda notebook #1283

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 2 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
12 changes: 12 additions & 0 deletions packages/libs/eda/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ import './index.css';

// snackbar
import makeSnackbarProvider from '@veupathdb/coreui/lib/components/notifications/SnackbarProvider';
import NotebookRoute from './lib/notebook/NotebookRoute';

// Set singleAppMode to the name of one app, if the eda should use one instance of one app only.
// Otherwise, let singleAppMode remain undefined or set it to '' to allow multiple app instances.
Expand Down Expand Up @@ -169,9 +170,20 @@ initialize({
<Link to="/maps/studies">All studies</Link>
</li>
</ul>
<h3>Notebook Links</h3>
<ul>
<li>
<Link to="/notebook">All notebooks</Link>
</li>
</ul>
</div>
),
},
{
path: '/notebook',
exact: false,
component: () => <NotebookRoute edaServiceUrl={edaEndpoint} />,
},
{
path: '/eda',
exact: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { colors, Warning } from '@veupathdb/coreui';
// Material UI CSS declarations
const useStyles = makeStyles((theme) => ({
chips: {
display: 'flex',
display: 'inline-flex',
flexWrap: 'wrap',
'& > *:not(:last-of-type)': {
// Spacing between chips
Expand Down
2 changes: 1 addition & 1 deletion packages/libs/eda/src/lib/core/components/VariableLink.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@ export const VariableLink = forwardRef(
tabIndex={0}
style={finalStyle}
onKeyDown={(event) => {
event.preventDefault();
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 was breaking keyboard accessibility.

if (disabled) {
return;
}
if (event.key === 'Enter' || event.key === ' ') {
linkConfig.onClick(value);
event.preventDefault();
}
}}
onClick={(event) => {
Expand Down
35 changes: 35 additions & 0 deletions packages/libs/eda/src/lib/notebook/EdaNotebook.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
.EdaNotebook {
.Heading {
display: flex;
gap: 2em;
align-items: baseline;
}

.Paper {
max-width: 1250px;
padding: 1em;
margin: 1em auto;
background-color: #f3f3f3;
box-shadow: 0 0 2px #b5b5b5;

> * + * {
margin-block-start: 1rem;
}
h2,
h3 {
padding: 0;
}
h3 {
font-size: 1em;
font-weight: 400;
line-height: 1.5;
}
}

.Title {
fieldset {
padding: 0;
margin: 0;
}
}
}
101 changes: 101 additions & 0 deletions packages/libs/eda/src/lib/notebook/EdaNotebookAnalysis.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// Notes
// =====
//
// - For now, we will only support "fixed" notebooks. If we want to allow "custom" notebooks,
// we have to make some decisions.
// - Do we want a top-down data flow? E.g., subsetting is global for an analysis.
// - Do we want to separate compute config from visualization? If so, how do we
// support that in the UI?
// - Do we want text-based cells?
// - Do we want download cells? It could have a preview.
//

import React, { useCallback, useMemo } from 'react';
import { useAnalysis, useStudyRecord } from '../core';
import { safeHtml } from '@veupathdb/wdk-client/lib/Utils/ComponentUtils';
import { SaveableTextEditor } from '@veupathdb/wdk-client/lib/Components';
import { ExpandablePanel } from '@veupathdb/coreui';
import { NotebookCell as NotebookCellType } from './Types';
import { NotebookCell } from './NotebookCell';

import './EdaNotebook.scss';

interface NotebookSettings {
/** Ordered array of notebook cells */
cells: NotebookCellType[];
}

const NOTEBOOK_UI_SETTINGS_KEY = '@@NOTEBOOK@@';

interface Props {
analysisId: string;
}

export function EdaNotebookAnalysis(props: Props) {
const { analysisId } = props;
const studyRecord = useStudyRecord();
const analysisState = useAnalysis(
analysisId === 'new' ? undefined : analysisId
);
const { analysis } = analysisState;
const notebookSettings = useMemo((): NotebookSettings => {
const storedSettings =
analysis?.descriptor.subset.uiSettings[NOTEBOOK_UI_SETTINGS_KEY];
if (storedSettings == null)
return {
cells: [
{
type: 'subset',
title: 'Subset data',
},
],
};
return storedSettings as any as NotebookSettings;
}, [analysis]);
const updateCell = useCallback(
(cell: Partial<Omit<NotebookCellType, 'type'>>, cellIndex: number) => {
const oldCell = notebookSettings.cells[cellIndex];
const newCell = { ...oldCell, ...cell };
const nextCells = notebookSettings.cells.concat();
nextCells[cellIndex] = newCell;
const nextSettings = {
...notebookSettings,
cells: nextCells,
};
analysisState.setVariableUISettings({
[NOTEBOOK_UI_SETTINGS_KEY]: nextSettings,
});
},
[analysisState, notebookSettings]
);
return (
<div className="EdaNotebook">
<div className="Heading">
<h1>EDA Notebook</h1>
</div>
<div className="Paper">
<div>
<h2>
<SaveableTextEditor
className="Title"
value={analysisState.analysis?.displayName ?? ''}
onSave={analysisState.setName}
/>
</h2>
<h3>Study: {safeHtml(studyRecord.displayName)}</h3>
</div>
{notebookSettings.cells.map((cell, index) => (
<ExpandablePanel title={cell.title} subTitle={{}} themeRole="primary">
<div style={{ padding: '1em' }}>
<NotebookCell
analysisState={analysisState}
cell={cell}
updateCell={(update) => updateCell(update, index)}
/>
</div>
</ExpandablePanel>
))}
</div>
</div>
);
}
37 changes: 37 additions & 0 deletions packages/libs/eda/src/lib/notebook/EdaNotebookLandingPage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import React from 'react';
import { useWdkStudyRecords } from '../core/hooks/study';
import { useConfiguredSubsettingClient } from '../core/hooks/client';
import { Link, useRouteMatch } from 'react-router-dom';
import { safeHtml } from '@veupathdb/wdk-client/lib/Utils/ComponentUtils';

interface Props {
edaServiceUrl: string;
}

export function EdaNotebookLandingPage(props: Props) {
const subsettingClient = useConfiguredSubsettingClient(props.edaServiceUrl);
const datasets = useWdkStudyRecords(subsettingClient);
const { url } = useRouteMatch();
return (
<div>
<h1>EDA Notebooks</h1>
<div>
<h2>Start a new notebook</h2>
<ul>
{datasets?.map((dataset) => (
<li>
{safeHtml(
dataset.displayName,
{ to: `${url}/${dataset.attributes.dataset_id as string}/new` },
Link
)}
</li>
))}
</ul>
</div>
<hr />
<div>MY NOTEBOOKS</div>
<div>SHARED NOTEBOOKS</div>
</div>
);
}
28 changes: 28 additions & 0 deletions packages/libs/eda/src/lib/notebook/NotebookCell.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { AnalysisState } from '../core';
import { NotebookCell as NotebookCellType } from './Types';
import { SubsettingNotebookCell } from './SubsettingNotebookCell';

interface Props {
analysisState: AnalysisState;
cell: NotebookCellType;
updateCell: (cell: Partial<Omit<NotebookCellType, 'type'>>) => void;
}

/**
* Top-level component that delegates to imeplementations of NotebookCell variants.
*/
export function NotebookCell(props: Props) {
const { cell, analysisState, updateCell } = props;
switch (cell.type) {
case 'subset':
return (
<SubsettingNotebookCell
cell={cell}
analysisState={analysisState}
updateCell={updateCell}
/>
);
default:
return null;
}
}
64 changes: 64 additions & 0 deletions packages/libs/eda/src/lib/notebook/NotebookRoute.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import React, { ComponentType } from 'react';
import { Route, Switch, useRouteMatch } from 'react-router-dom';
import { EdaNotebookLandingPage } from './EdaNotebookLandingPage';
import { EdaNotebookAnalysis } from './EdaNotebookAnalysis';
import {
EDAWorkspaceContainer,
useConfiguredAnalysisClient,
useConfiguredComputeClient,
useConfiguredDataClient,
useConfiguredDownloadClient,
useConfiguredSubsettingClient,
} from '../core';
import { DocumentationContainer } from '../core/components/docs/DocumentationContainer';
import { QueryClientProvider } from '@tanstack/react-query';
import { queryClient } from '../core/api/queryClient';

interface Props {
edaServiceUrl: string;
datasetId?: string;
analysisId?: string;
}

export default function NotebookRoute(props: Props) {
const { edaServiceUrl } = props;
const match = useRouteMatch();
const analysisClient = useConfiguredAnalysisClient(edaServiceUrl);
const subsettingClient = useConfiguredSubsettingClient(edaServiceUrl);
const downloadClient = useConfiguredDownloadClient(edaServiceUrl);
const dataClient = useConfiguredDataClient(edaServiceUrl);
const computeClient = useConfiguredComputeClient(edaServiceUrl);

return (
<DocumentationContainer>
<QueryClientProvider client={queryClient}>
<Switch>
<Route
exact
path={match.path}
render={() => (
<EdaNotebookLandingPage edaServiceUrl={edaServiceUrl} />
)}
/>
<Route
path={`${match.path}/:datasetId/:analysisId`}
render={(props) => (
<EDAWorkspaceContainer
studyId={props.match.params.datasetId}
analysisClient={analysisClient}
subsettingClient={subsettingClient}
downloadClient={downloadClient}
dataClient={dataClient}
computeClient={computeClient}
>
<EdaNotebookAnalysis
analysisId={props.match.params.analysisId}
/>
</EDAWorkspaceContainer>
)}
/>
</Switch>
</QueryClientProvider>
</DocumentationContainer>
);
}
58 changes: 58 additions & 0 deletions packages/libs/eda/src/lib/notebook/SubsettingNotebookCell.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { useMemo } from 'react';
import { useEntityCounts } from '../core/hooks/entityCounts';
import { useStudyEntities } from '../core/hooks/workspace';
import { NotebookCellComponentProps } from './Types';
import { VariableLinkConfig } from '../core/components/VariableLink';
import FilterChipList from '../core/components/FilterChipList';
import Subsetting from '../workspace/Subsetting';

export function SubsettingNotebookCell(
props: NotebookCellComponentProps<'subset'>
) {
const { analysisState, cell, updateCell } = props;
const { selectedVariable } = cell;
const entities = useStudyEntities();
const totalCountsResult = useEntityCounts();
const filteredCountsResult = useEntityCounts(
analysisState.analysis?.descriptor.subset.descriptor
);
const variableLinkConfig = useMemo(
(): VariableLinkConfig => ({
type: 'button',
onClick: (selectedVariable) => {
updateCell({ selectedVariable });
},
}),
[updateCell]
);
return (
<div>
<div>
<FilterChipList
filters={analysisState.analysis?.descriptor.subset.descriptor}
entities={entities}
selectedEntityId={selectedVariable?.entityId}
selectedVariableId={selectedVariable?.variableId}
removeFilter={(filter) => {
analysisState.setFilters((filters) =>
filters.filter(
(f) =>
f.entityId !== filter.entityId ||
f.variableId !== filter.variableId
)
);
}}
variableLinkConfig={variableLinkConfig}
/>
</div>
<Subsetting
analysisState={analysisState}
entityId={selectedVariable?.entityId ?? ''}
variableId={selectedVariable?.variableId ?? ''}
totalCounts={totalCountsResult.value}
filteredCounts={filteredCountsResult.value}
variableLinkConfig={variableLinkConfig}
/>
</div>
);
}
Loading
Loading