cancel
Showing results for 
Search instead for 
Did you mean: 
Community Platform Discussions
Connect with fellow community members to discuss general topics related to the Databricks platform, industry trends, and best practices. Share experiences, ask questions, and foster collaboration within the community.
cancel
Showing results for 
Search instead for 
Did you mean: 

Create new workbooks with code

invalidargument
New Contributor III

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_Fatma
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 III

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

Connect with Databricks Users in Your Area

Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you won’t want to miss the chance to attend and share knowledge.

If there isn’t a group near you, start one and help create a community that brings people together.

Request a New Group