(app: MarimoApp)
| 58 | * @returns The serialized Python code string |
| 59 | */ |
| 60 | export function serializeMarimoFormat(app: MarimoApp): string { |
| 61 | const lines: string[] = [] |
| 62 | |
| 63 | // Check if we have any markdown or SQL cells (both use mo.md() or mo.sql()) |
| 64 | const hasMarkdownOrSqlCells = app.cells.some(cell => cell.cellType === 'markdown' || cell.cellType === 'sql') |
| 65 | |
| 66 | // Add import - use 'as mo' alias if there are markdown or SQL cells |
| 67 | if (hasMarkdownOrSqlCells) { |
| 68 | lines.push('import marimo as mo') |
| 69 | } else { |
| 70 | lines.push('import marimo') |
| 71 | } |
| 72 | lines.push('') |
| 73 | |
| 74 | // Add version marker |
| 75 | lines.push(`__generated_with = "${app.generatedWith || MARIMO_VERSION}"`) |
| 76 | |
| 77 | // Add app initialization - use mo or marimo based on import |
| 78 | const appOptions: string[] = [] |
| 79 | if (app.width) { |
| 80 | appOptions.push(`width="${app.width}"`) |
| 81 | } |
| 82 | if (app.title) { |
| 83 | appOptions.push(`title="${escapeString(app.title)}"`) |
| 84 | } |
| 85 | const marimoRef = hasMarkdownOrSqlCells ? 'mo' : 'marimo' |
| 86 | const optionsStr = appOptions.length > 0 ? appOptions.join(', ') : '' |
| 87 | lines.push(`app = ${marimoRef}.App(${optionsStr})`) |
| 88 | lines.push('') |
| 89 | lines.push('') |
| 90 | |
| 91 | // Add cells |
| 92 | for (const cell of app.cells) { |
| 93 | // Build decorator |
| 94 | const decoratorOptions: string[] = [] |
| 95 | if (cell.hidden) { |
| 96 | decoratorOptions.push('hide_code=True') |
| 97 | } |
| 98 | if (cell.disabled) { |
| 99 | decoratorOptions.push('disabled=True') |
| 100 | } |
| 101 | const decoratorStr = decoratorOptions.length > 0 ? `@app.cell(${decoratorOptions.join(', ')})` : '@app.cell' |
| 102 | lines.push(decoratorStr) |
| 103 | |
| 104 | // Build function signature |
| 105 | const funcName = cell.functionName || '__' |
| 106 | const params = cell.dependencies?.join(', ') || '' |
| 107 | lines.push(`def ${funcName}(${params}):`) |
| 108 | |
| 109 | // Add cell content |
| 110 | if (cell.cellType === 'markdown') { |
| 111 | // Wrap markdown in mo.md() |
| 112 | const escaped = escapeTripleQuote(cell.content) |
| 113 | lines.push(` mo.md(r"""`) |
| 114 | for (const contentLine of escaped.split('\n')) { |
| 115 | // Don't add indentation to empty or whitespace-only lines |
| 116 | if (contentLine.trim() === '') { |
| 117 | lines.push('') |
no test coverage detected