@deepnote/blocks 3.0.1 → 3.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -28,6 +28,48 @@ yaml = __toESM(yaml);
28
28
  let ts_dedent = require("ts-dedent");
29
29
  ts_dedent = __toESM(ts_dedent);
30
30
 
31
+ //#region src/blocks.ts
32
+ var UnsupportedBlockTypeError = class extends Error {
33
+ constructor(message) {
34
+ super(message);
35
+ this.name = "UnsupportedBlockTypeError";
36
+ }
37
+ };
38
+
39
+ //#endregion
40
+ //#region src/blocks/executable-blocks.ts
41
+ const executableBlockTypes = new Set([
42
+ "code",
43
+ "sql",
44
+ "notebook-function",
45
+ "visualization",
46
+ "button",
47
+ "big-number",
48
+ "input-text",
49
+ "input-textarea",
50
+ "input-checkbox",
51
+ "input-select",
52
+ "input-slider",
53
+ "input-date",
54
+ "input-date-range",
55
+ "input-file"
56
+ ]);
57
+ /**
58
+ * Type guard to check if a block is an executable block.
59
+ * Executable blocks can have outputs and be executed by the runtime.
60
+ */
61
+ function isExecutableBlock(block) {
62
+ return executableBlockTypes.has(block.type);
63
+ }
64
+ /**
65
+ * Checks if a block type string represents an executable block.
66
+ * Convenience function for when you only have the type string.
67
+ */
68
+ function isExecutableBlockType(type) {
69
+ return executableBlockTypes.has(type);
70
+ }
71
+
72
+ //#endregion
31
73
  //#region src/deserialize-file/deepnote-file-schema.ts
32
74
  /** Preprocesses any content value to empty string for blocks that don't use content */
33
75
  const emptyContent = () => zod.z.preprocess(() => "", zod.z.literal("").optional());
@@ -420,6 +462,17 @@ const deepnoteFileSchema = zod.z.object({
420
462
  }),
421
463
  version: zod.z.string()
422
464
  });
465
+ const deepnoteSnapshotSchema = deepnoteFileSchema.extend({
466
+ environment: environmentSchema.unwrap(),
467
+ execution: executionSchema.unwrap(),
468
+ metadata: zod.z.object({
469
+ checksum: zod.z.string().optional(),
470
+ createdAt: zod.z.string(),
471
+ exportedAt: zod.z.string().optional(),
472
+ modifiedAt: zod.z.string().optional(),
473
+ snapshotHash: zod.z.string()
474
+ })
475
+ });
423
476
 
424
477
  //#endregion
425
478
  //#region src/deserialize-file/parse-yaml.ts
@@ -529,15 +582,6 @@ function deserializeDeepnoteFile(yamlContent) {
529
582
  return result.data;
530
583
  }
531
584
 
532
- //#endregion
533
- //#region src/blocks.ts
534
- var UnsupportedBlockTypeError = class extends Error {
535
- constructor(message) {
536
- super(message);
537
- this.name = "UnsupportedBlockTypeError";
538
- }
539
- };
540
-
541
585
  //#endregion
542
586
  //#region src/blocks/image-blocks.ts
543
587
  function escapeHtmlAttribute(value) {
@@ -938,6 +982,53 @@ function isInputDateRangeBlock(block) {
938
982
  return block.type === "input-date-range";
939
983
  }
940
984
 
985
+ //#endregion
986
+ //#region src/blocks/notebook-function-blocks.ts
987
+ function isNotebookFunctionBlock(block) {
988
+ return block.type === "notebook-function";
989
+ }
990
+ function createPythonCodeForNotebookFunctionBlock(block) {
991
+ const notebookId = block.metadata?.function_notebook_id;
992
+ const inputs = block.metadata?.function_notebook_inputs ?? {};
993
+ const exportMappings = block.metadata?.function_notebook_export_mappings ?? {};
994
+ if (!notebookId) return ts_dedent.dedent`
995
+ # Notebook Function: Not configured
996
+ pass
997
+ `;
998
+ const enabledExports = Object.entries(exportMappings).filter(([, mapping]) => mapping.enabled);
999
+ const inputsAsString = JSON.stringify(inputs);
1000
+ const exportMappingsDict = {};
1001
+ for (const [exportName, mapping] of enabledExports) exportMappingsDict[exportName] = mapping.variable_name;
1002
+ const exportMappingsAsString = JSON.stringify(exportMappingsDict);
1003
+ const inputsComment = `Inputs: ${inputsAsString}`;
1004
+ const exportsComment = enabledExports.length > 0 ? `Exports: ${enabledExports.map(([name, mapping]) => `${name} -> ${mapping.variable_name}`).join(", ")}` : "Exports: (none)";
1005
+ const functionCall = ts_dedent.dedent`
1006
+ _dntk.run_notebook_function(
1007
+ ${escapePythonString(notebookId)},
1008
+ inputs=${inputsAsString},
1009
+ export_mappings=${exportMappingsAsString}
1010
+ )
1011
+ `;
1012
+ if (enabledExports.length === 0) return ts_dedent.dedent`
1013
+ # Notebook Function: ${notebookId}
1014
+ # ${inputsComment}
1015
+ # ${exportsComment}
1016
+ ${functionCall}
1017
+ `;
1018
+ if (enabledExports.length === 1) return ts_dedent.dedent`
1019
+ # Notebook Function: ${notebookId}
1020
+ # ${inputsComment}
1021
+ # ${exportsComment}
1022
+ ${enabledExports[0][1]?.variable_name} = ${functionCall}
1023
+ `;
1024
+ return ts_dedent.dedent`
1025
+ # Notebook Function: ${notebookId}
1026
+ # ${inputsComment}
1027
+ # ${exportsComment}
1028
+ ${enabledExports.map(([, mapping]) => mapping.variable_name).join(", ")} = ${functionCall}
1029
+ `;
1030
+ }
1031
+
941
1032
  //#endregion
942
1033
  //#region src/blocks/sql-utils.ts
943
1034
  function convertToEnvironmentVariableName(str) {
@@ -1013,18 +1104,24 @@ function createPythonCode(block, executionContext) {
1013
1104
  if (isVisualizationBlock(block)) return createPythonCodeForVisualizationBlock(block);
1014
1105
  if (isButtonBlock(block)) return createPythonCodeForButtonBlock(block, executionContext);
1015
1106
  if (isBigNumberBlock(block)) return createPythonCodeForBigNumberBlock(block);
1107
+ if (isNotebookFunctionBlock(block)) return createPythonCodeForNotebookFunctionBlock(block);
1016
1108
  throw new UnsupportedBlockTypeError(`Creating python code from block type ${block.type} is not supported yet.`);
1017
1109
  }
1018
1110
 
1019
1111
  //#endregion
1112
+ exports.UnsupportedBlockTypeError = UnsupportedBlockTypeError;
1020
1113
  exports.createMarkdown = createMarkdown;
1021
1114
  exports.createPythonCode = createPythonCode;
1022
1115
  exports.decodeUtf8NoBom = decodeUtf8NoBom;
1023
1116
  exports.deepnoteBlockSchema = deepnoteBlockSchema;
1024
1117
  exports.deepnoteFileSchema = deepnoteFileSchema;
1118
+ exports.deepnoteSnapshotSchema = deepnoteSnapshotSchema;
1025
1119
  exports.deserializeDeepnoteFile = deserializeDeepnoteFile;
1026
1120
  exports.environmentSchema = environmentSchema;
1027
1121
  exports.executionErrorSchema = executionErrorSchema;
1028
1122
  exports.executionSchema = executionSchema;
1029
1123
  exports.executionSummarySchema = executionSummarySchema;
1124
+ exports.isExecutableBlock = isExecutableBlock;
1125
+ exports.isExecutableBlockType = isExecutableBlockType;
1126
+ exports.parseYaml = parseYaml;
1030
1127
  exports.stripMarkdown = stripMarkdown;