@deepnote/blocks 3.0.0 → 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.js CHANGED
@@ -2,6 +2,48 @@ import { z } from "zod";
2
2
  import { parseDocument } from "yaml";
3
3
  import { dedent } from "ts-dedent";
4
4
 
5
+ //#region src/blocks.ts
6
+ var UnsupportedBlockTypeError = class extends Error {
7
+ constructor(message) {
8
+ super(message);
9
+ this.name = "UnsupportedBlockTypeError";
10
+ }
11
+ };
12
+
13
+ //#endregion
14
+ //#region src/blocks/executable-blocks.ts
15
+ const executableBlockTypes = new Set([
16
+ "code",
17
+ "sql",
18
+ "notebook-function",
19
+ "visualization",
20
+ "button",
21
+ "big-number",
22
+ "input-text",
23
+ "input-textarea",
24
+ "input-checkbox",
25
+ "input-select",
26
+ "input-slider",
27
+ "input-date",
28
+ "input-date-range",
29
+ "input-file"
30
+ ]);
31
+ /**
32
+ * Type guard to check if a block is an executable block.
33
+ * Executable blocks can have outputs and be executed by the runtime.
34
+ */
35
+ function isExecutableBlock(block) {
36
+ return executableBlockTypes.has(block.type);
37
+ }
38
+ /**
39
+ * Checks if a block type string represents an executable block.
40
+ * Convenience function for when you only have the type string.
41
+ */
42
+ function isExecutableBlockType(type) {
43
+ return executableBlockTypes.has(type);
44
+ }
45
+
46
+ //#endregion
5
47
  //#region src/deserialize-file/deepnote-file-schema.ts
6
48
  /** Preprocesses any content value to empty string for blocks that don't use content */
7
49
  const emptyContent = () => z.preprocess(() => "", z.literal("").optional());
@@ -46,7 +88,7 @@ const baseBlockFields = {
46
88
  id: z.string(),
47
89
  blockGroup: z.string(),
48
90
  sortingKey: z.string(),
49
- contentHash: z.string().regex(/^(md5|sha256):[a-f0-9]+$/i).optional(),
91
+ contentHash: z.string().regex(/^([a-z0-9]+:)?[a-f0-9]+$/i).optional(),
50
92
  version: z.number().optional()
51
93
  };
52
94
  const executableBlockFields = {
@@ -394,6 +436,17 @@ const deepnoteFileSchema = z.object({
394
436
  }),
395
437
  version: z.string()
396
438
  });
439
+ const deepnoteSnapshotSchema = deepnoteFileSchema.extend({
440
+ environment: environmentSchema.unwrap(),
441
+ execution: executionSchema.unwrap(),
442
+ metadata: z.object({
443
+ checksum: z.string().optional(),
444
+ createdAt: z.string(),
445
+ exportedAt: z.string().optional(),
446
+ modifiedAt: z.string().optional(),
447
+ snapshotHash: z.string()
448
+ })
449
+ });
397
450
 
398
451
  //#endregion
399
452
  //#region src/deserialize-file/parse-yaml.ts
@@ -503,15 +556,6 @@ function deserializeDeepnoteFile(yamlContent) {
503
556
  return result.data;
504
557
  }
505
558
 
506
- //#endregion
507
- //#region src/blocks.ts
508
- var UnsupportedBlockTypeError = class extends Error {
509
- constructor(message) {
510
- super(message);
511
- this.name = "UnsupportedBlockTypeError";
512
- }
513
- };
514
-
515
559
  //#endregion
516
560
  //#region src/blocks/image-blocks.ts
517
561
  function escapeHtmlAttribute(value) {
@@ -912,6 +956,53 @@ function isInputDateRangeBlock(block) {
912
956
  return block.type === "input-date-range";
913
957
  }
914
958
 
959
+ //#endregion
960
+ //#region src/blocks/notebook-function-blocks.ts
961
+ function isNotebookFunctionBlock(block) {
962
+ return block.type === "notebook-function";
963
+ }
964
+ function createPythonCodeForNotebookFunctionBlock(block) {
965
+ const notebookId = block.metadata?.function_notebook_id;
966
+ const inputs = block.metadata?.function_notebook_inputs ?? {};
967
+ const exportMappings = block.metadata?.function_notebook_export_mappings ?? {};
968
+ if (!notebookId) return dedent`
969
+ # Notebook Function: Not configured
970
+ pass
971
+ `;
972
+ const enabledExports = Object.entries(exportMappings).filter(([, mapping]) => mapping.enabled);
973
+ const inputsAsString = JSON.stringify(inputs);
974
+ const exportMappingsDict = {};
975
+ for (const [exportName, mapping] of enabledExports) exportMappingsDict[exportName] = mapping.variable_name;
976
+ const exportMappingsAsString = JSON.stringify(exportMappingsDict);
977
+ const inputsComment = `Inputs: ${inputsAsString}`;
978
+ const exportsComment = enabledExports.length > 0 ? `Exports: ${enabledExports.map(([name, mapping]) => `${name} -> ${mapping.variable_name}`).join(", ")}` : "Exports: (none)";
979
+ const functionCall = dedent`
980
+ _dntk.run_notebook_function(
981
+ ${escapePythonString(notebookId)},
982
+ inputs=${inputsAsString},
983
+ export_mappings=${exportMappingsAsString}
984
+ )
985
+ `;
986
+ if (enabledExports.length === 0) return dedent`
987
+ # Notebook Function: ${notebookId}
988
+ # ${inputsComment}
989
+ # ${exportsComment}
990
+ ${functionCall}
991
+ `;
992
+ if (enabledExports.length === 1) return dedent`
993
+ # Notebook Function: ${notebookId}
994
+ # ${inputsComment}
995
+ # ${exportsComment}
996
+ ${enabledExports[0][1]?.variable_name} = ${functionCall}
997
+ `;
998
+ return dedent`
999
+ # Notebook Function: ${notebookId}
1000
+ # ${inputsComment}
1001
+ # ${exportsComment}
1002
+ ${enabledExports.map(([, mapping]) => mapping.variable_name).join(", ")} = ${functionCall}
1003
+ `;
1004
+ }
1005
+
915
1006
  //#endregion
916
1007
  //#region src/blocks/sql-utils.ts
917
1008
  function convertToEnvironmentVariableName(str) {
@@ -987,8 +1078,9 @@ function createPythonCode(block, executionContext) {
987
1078
  if (isVisualizationBlock(block)) return createPythonCodeForVisualizationBlock(block);
988
1079
  if (isButtonBlock(block)) return createPythonCodeForButtonBlock(block, executionContext);
989
1080
  if (isBigNumberBlock(block)) return createPythonCodeForBigNumberBlock(block);
1081
+ if (isNotebookFunctionBlock(block)) return createPythonCodeForNotebookFunctionBlock(block);
990
1082
  throw new UnsupportedBlockTypeError(`Creating python code from block type ${block.type} is not supported yet.`);
991
1083
  }
992
1084
 
993
1085
  //#endregion
994
- export { createMarkdown, createPythonCode, decodeUtf8NoBom, deepnoteBlockSchema, deepnoteFileSchema, deserializeDeepnoteFile, environmentSchema, executionErrorSchema, executionSchema, executionSummarySchema, stripMarkdown };
1086
+ export { UnsupportedBlockTypeError, createMarkdown, createPythonCode, decodeUtf8NoBom, deepnoteBlockSchema, deepnoteFileSchema, deepnoteSnapshotSchema, deserializeDeepnoteFile, environmentSchema, executionErrorSchema, executionSchema, executionSummarySchema, isExecutableBlock, isExecutableBlockType, parseYaml, stripMarkdown };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@deepnote/blocks",
3
- "version": "3.0.0",
3
+ "version": "3.2.1",
4
4
  "description": "",
5
5
  "keywords": [],
6
6
  "repository": {