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 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
24 changes: 22 additions & 2 deletions packages/libs/coreui/src/components/theming/UIThemeProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { css, Global, ThemeProvider } from '@emotion/react';
import { useCoreUIFonts } from '../../hooks';

import { UITheme } from './types';
import colors from '../../definitions/colors';

export type UIThemeProviderProps = {
theme: UITheme;
Expand All @@ -14,13 +15,32 @@ export default function UIThemeProvider({
children,
}: UIThemeProviderProps) {
useCoreUIFonts();
// In addition to making the theme available via React Context,
// we will also expose the theme as custom CSS properties.
return (
<ThemeProvider theme={theme}>
<Global
styles={css`
:root {
${Object.entries(colors).flatMap(([colorName, byHueOrValue]) =>
typeof byHueOrValue === 'string'
? [`--coreui-${colorName}: ${byHueOrValue};`]
: Object.entries(byHueOrValue).map(
([hueName, colorValue]) =>
`--coreui-${colorName}-${hueName}: ${colorValue};`
)
)}

--coreui-color-primary: ${theme.palette.primary.hue[
theme.palette.primary.level
]};
--coreui-color-secondary: ${theme.palette.secondary.hue[
theme.palette.secondary.level
]};
}

*:focus-visible {
outline: 2px solid
${theme.palette.primary.hue[theme.palette.primary.level]};
outline: 2px solid var(--coreui-color-primary);
}
`}
/>
Expand Down
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
116 changes: 116 additions & 0 deletions packages/libs/eda/src/lib/notebook/EdaNotebook.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
.EdaNotebook {
.Heading {
display: flex;
gap: 2em;
align-items: baseline;
}

.Paper {
/* A4 dimensions */
--paper-width: 2480px;
--paper-height: 3508px;
--paper-scale: 0.5;

width: calc(var(--paper-width) * var(--paper-scale));
/* height: calc(var(--paper-height) * var(--paper-scale)); */

padding: 2em;
margin: 1em auto;

/* background-color: #f3f3f3; */
box-shadow: 0 0 2px #b5b5b5;

> * + * {
margin-block-start: 1rem;
}

.Heading {
display: flex;
flex-direction: row;
justify-content: space-between;
gap: 0.5em;

h1 {
padding: 0;
font-size: 1.75em;
}

h2 {
font-size: 1em;
font-weight: bold;
padding: 0.25em 0.5em;
color: var(--coreui-color-primary, black);
border: 2px solid;
border-radius: 0.25em;
background-color: color-mix(
in srgb,
var(--coreui-color-primary) 5%,
transparent
);
}
}

> details {
border: 1px solid;
border-color: color-mix(
in srgb,
var(--coreui-color-primary) 30%,
transparent
);
border-top-left-radius: 0.5em;
border-top-right-radius: 0.5em;
border-bottom-left-radius: 0.5em;
border-bottom-right-radius: 0.5em;

> summary {
padding: 0.75em;
cursor: pointer;
font-size: 1.2em;
font-weight: 500;
background-color: color-mix(
in srgb,
var(--coreui-color-primary) 10%,
transparent
);

&:hover {
background-color: color-mix(
in srgb,
var(--coreui-color-primary) 20%,
transparent
);
}

&:active {
background-color: color-mix(
in srgb,
var(--coreui-color-primary) 15%,
transparent
);
}

transition: background-color 100ms ease-in;
}

&[open] > summary {
border-bottom: 1px solid;
border-color: color-mix(
in srgb,
var(--coreui-color-primary) 30%,
transparent
);
}

> div {
padding: 1em;
}
}
}

.Title {
fieldset {
padding: 0;
margin: 0;
}
}
}
97 changes: 97 additions & 0 deletions packages/libs/eda/src/lib/notebook/EdaNotebookAnalysis.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// 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.css';

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="Paper">
<div className="Heading">
<h1>
<SaveableTextEditor
className="Title"
value={analysisState.analysis?.displayName ?? ''}
onSave={analysisState.setName}
/>
</h1>
<h2>{safeHtml(studyRecord.displayName)}</h2>
</div>
{notebookSettings.cells.map((cell, index) => (
<details>
<summary>{cell.title}</summary>
<NotebookCell
analysisState={analysisState}
cell={cell}
updateCell={(update) => updateCell(update, index)}
/>
</details>
))}
</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;
}
}
Loading
Loading