/** ******************************************************************************************************************* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. * ******************************************************************************************************************** */ import React, { FunctionComponent, useMemo } from 'react'; import Grid from '../../layouts/Grid'; import Container from '../../layouts/Container'; import ReactMarkdown from 'react-markdown'; import parse from 'html-react-parser'; import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter'; import { coy } from 'react-syntax-highlighter/dist/cjs/styles/prism'; import { CodeComponent } from 'react-markdown/src/ast-to-react'; import Heading from '../../components/Heading'; import Paper from '../../layouts/Paper'; // cats an array of lines together const sourceLines = (lines: string[]) => lines.join(''); const ansiControlRegex = /[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g; const redCellColor = '#FDD'; const greenCellColor = '#DFD'; const plainCellStyle = { padding: '0', margin: '0', border: '1px solid #AAA', }; const indentedCellStyle = { padding: '0px 8px 0px 8px', margin: '0px 0px', border: '1px solid #AAA', }; export interface JupyterNotebookProps { /** * The Notebook Data in JSON string format */ notebookData: string; } interface JupyterNotebookCellProps { cell: any; executionCount: string; language: string; } interface JupyterNotebookOutputCellProps extends JupyterNotebookCellProps { output: any; } interface JupyterNotebookCellRenderTemplateProps { idColumnContent: string; bodyColumnContent: any; bodyColumnStyle: any; } const JupyterNotebookCellRenderTemplate: FunctionComponent = ({ idColumnContent, bodyColumnContent, bodyColumnStyle, }) => { return ( <> {idColumnContent} {bodyColumnContent} ); }; // -- renders a cell of type 'markdown' const JupyterNotebookMarkdownCell: FunctionComponent = ({ cell, executionCount: execution_count, language, }) => { return ( } bodyColumnStyle={indentedCellStyle} /> ); }; // -- renders a cell of type 'heading' const JupyterNotebookHeadingCell: FunctionComponent = ({ cell, executionCount: execution_count, language, }) => { return ( {cell.source[0]} } bodyColumnStyle={indentedCellStyle} /> ); }; // -- renders a cell of type 'stream' const JupyterNotebookOutputStreamCell: React.FC = ({ cell, executionCount, output, language, }) => { return ( } bodyColumnStyle={{ padding: '0px 8px 0px 8px', margin: '0px 0px', backgroundColor: output.name === 'stderr' ? redCellColor : greenCellColor, border: '1px solid #AAA', }} /> ); }; // -- renders a cell of type 'error' const JupyterNotebookOutputErrorCell: React.FC = ({ cell, executionCount, output, language, }) => { return ( } bodyColumnStyle={{ padding: '0px 8px 0px 8px', margin: '0px 0px', border: '1px solid #AAA', backgroundColor: redCellColor, }} /> ); }; // -- renders various data cell types (html, png, text) const JupyterNotebookOutputDataCell: React.FC = ({ cell, executionCount, output, language, }) => { return ( {output.data['text/html'] ? ( parse(sourceLines(output.data['text/html']), { trim: true }) ) : output.data['image/png'] ? ( {'notebook ) : output.data['text/plain'] ? ( ) : ( unknown )} } bodyColumnStyle={plainCellStyle} /> ); }; // -- helper class used in syntax highlighting the code const CodeBlock: CodeComponent = ({ inline = false, className, children }) => { const match = /language-(\w+)/.exec(className || ''); const codeLanguage = useMemo(() => { const langs = ['python', 'java', 'javascript', 'c++', 'typescript', 'objective-c', 'json']; return langs.find((lang) => match?.[1]?.startsWith(lang)) || 'javascript'; }, [match]); return ( {children} ); }; // -- renders a cell of type 'code' const JupyterNotebookCodeCell: React.FC = ({ cell, executionCount, language }) => { return ( <> {cell.metadata?.jupyter?.outputs_hidden ? ( <> ) : ( } bodyColumnStyle={plainCellStyle} /> )} {cell.metadata?.jupyter?.source_hidden ? ( <> ) : ( cell.outputs.map((output: any, outputIdx: number) => { if (output.output_type === 'stream') { return ( ); } else if (output.output_type === 'execute_result' || output.output_type === 'display_data') { return ( ); } else if (output.output_type === 'error') { return ( ); } else { return <>; } }) )} ); }; // -- router to render the two main input cell types const JupyterNotebookCell: React.FC = ({ cell, executionCount, language }) => { switch (cell.cell_type) { case 'markdown': return ; case 'code': return ; case 'heading': return ; default: return <>; } }; /** * Renders a Jupyter Notebook Viewer. */ const JupyterNotebook: FunctionComponent = ({ notebookData }) => { const notebook = JSON.parse(notebookData); return ( {notebook.cells?.map((cell: any, index: number) => { return ( ); })} ); }; export default JupyterNotebook;