Write me a Python function to flatten a nested list.
Chat - Code block
Display code with a language label and copy-to-clipboard button using the ChatCodeBlock component.
- Automatic rendering —
ChatBoxrenders code fences from markdown asChatCodeBlockautomatically. No extra config needed. - Language label — the language specified in the code fence (for example,
```python) appears in the header bar. - Copy button — clicking copies the raw code string to the clipboard and shows a check mark for 2 seconds.
- Custom highlighter — the standalone section below the chat shows the
highlighterprop with a minimal Python keyword coloriser (no library required).
Standalone with custom highlighter
def flatten(lst):
result = []
for item in lst:
if isinstance(item, list):
result.extend(flatten(item))
else:
result.append(item)
return resultBasic usage
Use ChatCodeBlock as a standalone component by passing children (the code string) and an optional language:
import { ChatCodeBlock } from '@mui/x-chat';
<ChatCodeBlock language="typescript">
{`const greet = (name: string) => \`Hello, \${name}!\`;`}
</ChatCodeBlock>;
Custom labels
Set language to any string — it is displayed as-is in the header:
<ChatCodeBlock language="bash">{`pnpm add @mui/x-chat`}</ChatCodeBlock>
Syntax highlighting
ChatCodeBlock intentionally does not bundle a syntax-highlighting library. Pass a highlighter function to integrate your preferred library (Shiki, Prism, highlight.js, etc.):
import { ChatCodeBlock } from '@mui/x-chat';
import { codeToHtml } from 'shiki';
function ShikiBlock({ code, language }) {
const [html, setHtml] = React.useState('');
React.useEffect(() => {
codeToHtml(code, { lang: language, theme: 'github-light' }).then(setHtml);
}, [code, language]);
return (
<ChatCodeBlock
language={language}
highlighter={() => <span dangerouslySetInnerHTML={{ __html: html }} />}
>
{code}
</ChatCodeBlock>
);
}
Automatic rendering in chat
When using ChatBox, any code fence in a markdown assistant message is automatically rendered as a ChatCodeBlock. This requires no additional configuration — the renderMarkdown function used internally by ChatMessageContent emits ChatCodeBlock for every code fence it encounters.
To customize the rendering further, override partProps.text.renderText on ChatMessageContent.