cancel
Showing results for 
Search instead for 
Did you mean: 
Community Discussions
cancel
Showing results for 
Search instead for 
Did you mean: 

Create new workbooks with code

invalidargument
New Contributor II

Is is possible to create new notebooks from a notevbook in databricks?

 

I have tried this code. But all of them are generic files, not notebooks.

notebook_str = """# Databricks notebook source

import pyspark.sql.functions as F
import numpy as np

# COMMAND ----------

# A test notebook
np.__version__
"""
with open("./notebook.ipynb","w") as f:
    f.write(notebook_str)

with open("./notebook.py","w") as f:
    f.write(notebook_str)

with open("./notebook","w") as f:
    f.write(notebook_str)
2 REPLIES 2

Kaniz
Community Manager
Community Manager

Hi @invalidargument, In Databricks, you can manage notebooks using the UI, the CLI, and the Workspace API

 

However, creating a new notebook from another notebook in Databricks is not directly supported in how you’re trying to do it with Python file operations.

The code you’re using is writing a string to a file, which doesn’t create a Databricks Notebook. Databricks Notebooks have a specific format and associated metadata that can’t be replicated by writing a string to a file.

 

However, Databricks does provide a %run command that allows you to include another notebook within a.... This can be used to modularize your code by putting supporting functions in a separate notebook. You can also use it to concatenate notebooks that implement the steps in an analysis.

 

I hope this helps! Let me know if you have any other questions.

invalidargument
New Contributor II

Unfortunaly %run does not help me since I can't %run a .py file. I still need my code in notebooks.

I am transpiling propriatary code to python using jinja templates. I would like to have the output as notebooks since those are most convenient to edit in databricks.

Perhaps calling the workspace API is a possible way forward then? Can i call this from a databrick notebook?
Import a workspace object | Workspace API | REST API reference | Databricks on AWS

Welcome to Databricks Community: Lets learn, network and celebrate together

Join our fast-growing data practitioner and expert community of 80K+ members, ready to discover, help and collaborate together while making meaningful connections. 

Click here to register and join today! 

Engage in exciting technical discussions, join a group with your peers and meet our Featured Members.