Skip to content

Commit 384d0d7

Browse files
author
mnfgul
committed
Updated for v.2
Updated for v2 of the course. As arbitrary files are supported in repos, relative imports and arbitrary file edits works in repos.
1 parent ceefa2a commit 384d0d7

File tree

2 files changed

+43
-4
lines changed

2 files changed

+43
-4
lines changed

my_lib/db-notebook.py

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Databricks notebook source
2+
print("This is a Databricks Notebook")

temperatures.py

+41-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,37 @@
11
# Databricks notebook source
22
# MAGIC %md
3-
# MAGIC **NOTE**: the following cell _**should**_ fail.
3+
# MAGIC # Introduction to Databricks Repos
4+
5+
# COMMAND ----------
6+
7+
# MAGIC %md
8+
# MAGIC
9+
# MAGIC This repo is created to demonstrate Git integration with Databricks Repos. Databricks Repos is a visual Git client in Databricks. **It supports common Git operations such a cloning a repository, committing and pushing, pulling, branch management, and visual comparison of diffs when committing.**
10+
# MAGIC
11+
# MAGIC Within Repos you can develop code in notebooks or other files and follow data science and engineering code development best practices using Git for version control, collaboration, and CI/CD.
12+
# MAGIC
13+
# MAGIC *In Databricks Repos*, you can use Git functionality to:
14+
# MAGIC - Clone, push to, and pull from a remote Git repository.
15+
# MAGIC - Create and manage branches for development work.
16+
# MAGIC - Create notebooks, and edit notebooks and other files.
17+
# MAGIC - Visually compare differences upon commit.
18+
# MAGIC
19+
# MAGIC For following tasks, *work in your Git provider*:
20+
# MAGIC - Create a pull request.
21+
# MAGIC - Resolve merge conflicts.
22+
# MAGIC - Merge or delete branches.
23+
# MAGIC - Rebase a branch.
24+
25+
# COMMAND ----------
26+
27+
# MAGIC %md
28+
# MAGIC ## Importing Notebooks/Modules
29+
# MAGIC
30+
# MAGIC You can import Databricks notebooks as well as python or R modules.
31+
# MAGIC
32+
# MAGIC ### Import Python Modules
433
# MAGIC
5-
# MAGIC Relative imports of Python libraries are currently not supported. ([Custom libraries can be uploaded to the workspace or installed from PyPi](https://docs.databricks.com/libraries/workspace-libraries.html).)
34+
# MAGIC The current working directory of your repo and notebook are automatically added to the Python path. When you work in the repo root, you can import modules from the root directory and all subdirectories.
635

736
# COMMAND ----------
837

@@ -11,6 +40,8 @@
1140
# COMMAND ----------
1241

1342
# MAGIC %md
43+
# MAGIC ### Import Databricks Notebooks
44+
# MAGIC
1445
# MAGIC Using `%run` allows you to execute a Databricks notebook in the current SparkSession, bringing any imported modules, declared variables, or defined functions into the current scope.
1546
# MAGIC
1647
# MAGIC Note that Databricks Python notebooks are stored as normal Python files with the first line
@@ -19,15 +50,21 @@
1950
# MAGIC # Databricks notebook source
2051
# MAGIC ```
2152
# MAGIC
22-
# MAGIC The Databricks web app searches for this line when syncing changes with the remote repository and will render Python scripts as single cell notebooks automatically.
53+
# MAGIC When you import the notebook, Databricks recognizes it and imports it as a notebook, not as a Python module.
54+
# MAGIC
55+
# MAGIC If you want to import the notebook as a Python module, you must edit the notebook in a code editor and remove the line `# Databricks Notebook source`. Removing that line converts the notebook to a regular Python file.
56+
# MAGIC
57+
# MAGIC In the following cell, we are importing an Databricks notebook and run it.
2358

2459
# COMMAND ----------
2560

26-
# MAGIC %run ./my_lib/my_funcs
61+
# MAGIC %run ./my_lib/db-notebook
2762

2863
# COMMAND ----------
2964

3065
# MAGIC %md
66+
# MAGIC ## Using Imported Modules
67+
# MAGIC
3168
# MAGIC The remainder of this notebook demonstrates using the functions declared in the referenced `my_funcs` Python file.
3269

3370
# COMMAND ----------

0 commit comments

Comments
 (0)