- 
                Notifications
    You must be signed in to change notification settings 
- Fork 749
feat: Add markdown to the cell level #6479
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
Changes from 8 commits
759232d
              a0781ba
              0f2f471
              433c0e4
              fd10110
              18857df
              859580e
              e53a3fa
              4b11522
              File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
|  | @@ -2009,22 +2009,95 @@ async def instantiate(self, request: CreationRequest) -> None: | |
| if self.graph.cells: | ||
| del request | ||
| LOGGER.debug("App already instantiated.") | ||
| elif request.auto_run: | ||
| return | ||
|  | ||
| # Handle markdown cells specially during kernel-ready initialization | ||
| execution_requests = { | ||
| er.cell_id: er for er in request.execution_requests | ||
| } | ||
| self._handle_markdown_cells_on_instantiate(execution_requests) | ||
|  | ||
| if request.auto_run: | ||
| self.reset_ui_initializers() | ||
| for ( | ||
| object_id, | ||
| initial_value, | ||
| ) in request.set_ui_element_value_request.ids_and_values: | ||
| self.ui_initializers[object_id] = initial_value | ||
|  | ||
| await self.run(request.execution_requests) | ||
| await self.run(list(execution_requests.values())) | ||
| self.reset_ui_initializers() | ||
| else: | ||
| self._uninstantiated_execution_requests = { | ||
| er.cell_id: er for er in request.execution_requests | ||
| } | ||
| for cid in self._uninstantiated_execution_requests: | ||
| CellOp.broadcast_stale(cell_id=cid, stale=True) | ||
| self._uninstantiated_execution_requests = execution_requests | ||
| for cell_id in self._uninstantiated_execution_requests.keys(): | ||
| CellOp.broadcast_stale(cell_id=cell_id, stale=True) | ||
|  | ||
| def _handle_markdown_cells_on_instantiate( | ||
| self, execution_requests: dict[CellId_t, ExecutionRequest] | ||
| ) -> None: | ||
| """Handle markdown cells during kernel-ready initialization. | ||
|  | ||
| For cells that contain only markdown (mo.md calls), this method: | ||
| 1. Compiles the cells to extract markdown content | ||
| 2. Renders the markdown to HTML | ||
| 3. Broadcasts the rendered output immediately | ||
| 4. Marks the cells as completed (not stale) | ||
| 5. Removes them from uninstantiated requests | ||
|  | ||
| NOTE: If 'mo' is not available in the graph definitions, all cells are | ||
| marked as stale. Regular cells are marked as stale as usual. | ||
| """ | ||
| # If 'mo' is not available in the graph, mark all cells as stale | ||
| markdown_cells: dict[CellId_t, str] = {} | ||
| exports_mo = False | ||
| for cid, er in execution_requests.items(): | ||
| # Check if cell already exists in graph (to avoid recompilation) | ||
| cell = self.graph.cells.get(cid) | ||
| error = None | ||
|  | ||
| # If cell doesn't exist in graph, try to compile it | ||
| if cell is None: | ||
| # TODO: Don't bother compiling whole cell. | ||
| # However, since we still need to extract defs | ||
| # for mo / marimo, this is OK for now. | ||
| cell, error = self._try_compiling_cell(cid, er.code, []) | ||
|  | ||
| if cell is None or error is not None: | ||
| continue | ||
|  | ||
| # Check if this is a markdown cell | ||
| if cell.markdown is not None: | ||
| # Remove from uninstantiated requests since it's effectively "run" | ||
| markdown_cells[cid] = cell.markdown | ||
| else: | ||
| # Regular cell - mark as stale | ||
| exports_mo |= "mo" in cell.defs | ||
|  | ||
| # Handle as default if no cells export 'mo' | ||
| if not exports_mo: | ||
| return | ||
|  | ||
| # Remove markdown cells from uninstantiated requests | ||
| for cell_id, content in markdown_cells.items(): | ||
| # Since markdown cell, render and broadcast output | ||
| # Remove cell from outstanding requests | ||
| from marimo._output.md import md | ||
|  | ||
| html_obj = md(content) | ||
| mimetype, html_content = html_obj._mime_() | ||
|  | ||
| # Broadcast the markdown output | ||
| CellOp.broadcast_output( | ||
| channel=CellChannel.OUTPUT, | ||
| mimetype=mimetype, | ||
| data=html_content, | ||
| cell_id=cell_id, | ||
| status="idle", | ||
| ) | ||
|  | ||
| # Mark the cell as not stale (already "run") | ||
| CellOp.broadcast_stale(cell_id=cell_id, stale=False) | ||
| del execution_requests[cell_id] | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. any way to break some of this up outside of the  There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a good point, but we still call some functions on the object | ||
|  | ||
| def load_dotenv(self) -> None: | ||
| dotenvs = self.user_config["runtime"].get("dotenv", []) | ||
|  | ||
Uh oh!
There was an error while loading. Please reload this page.