惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

雷峰网
雷峰网
博客园 - 聂微东
酷 壳 – CoolShell
酷 壳 – CoolShell
宝玉的分享
宝玉的分享
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
罗磊的独立博客
Hugging Face - Blog
Hugging Face - Blog
T
Tailwind CSS Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
IT之家
IT之家
博客园_首页
博客园 - 三生石上(FineUI控件)
博客园 - 叶小钗
Apple Machine Learning Research
Apple Machine Learning Research
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
量子位
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
人人都是产品经理
人人都是产品经理
美团技术团队
小众软件
小众软件
Jina AI
Jina AI
S
SegmentFault 最新的问题
博客园 - Franky
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com

jdhao's digital space

Conversion between base64 and OpenCV or PIL Image 腾讯云对象存储博客图床开启 CDN 加速(不需要购买额外域名) Search and Replace in Multiple Files in Vim/Neovim Change Table Column Width in LaTeX Image or Table Side by Side in LaTeX LaTeX 并排显示图像或表格 Firenvim: Neovim inside Your Browser Content inside HTML tags missing in Latest Hugo? Creating Markdown Front Matter with Ultisnips Labelme JSON 标注格式转 voc XML 格式 Nifty Nvim Techniques That Make My Life Easier -- Series 6 macOS 下如何为视频制作字幕 Running Command Asynchronously inside Neovim Resolving Merge Conflict after Git Stash Pop Pylint: command not found? A Hands-on Experience with Neovim's Built-in LSP Support How to Convert PDF to Images with Imagemagick 互联网上常用缩略语集锦 File Backup in Neovim Converting PDF Pages to Images with Poppler Nifty Nvim Techniques That Make My Life Easier -- Series 5 Neovim Configuration for System-wide Use How to sort a list of tuple or list in Python -- lambda or itemgetter? Building A Vim Statusline from Scratch 人类第一颗原子弹爆炸始末 Distributed Training in PyTorch with Horovod Learning Expect Programming Essential Knowledge about SSH Nifty LaTeX Techniques -- Series 1 更改 Adsense 邮寄地址,重新寄送 PIN
Running/importing Python code/module in Databricks
2023-06-07 · via jdhao's digital space

Databricks is centered around notebooks for doing a lot of works. As a result, it is often not obvious how to run or re-use code from another notebook or Python module. In this post, I want to share how to do this in the Databricks.

Run/import code from another notebook#

Suppose that we have the following notebook1:

foo = dbutils.widgets.get("foo")
bar = dbutils.widgets.get("bar")

result = foo + '&' + bar


def show():
    print(result)

dbutils.notebook.exit(result)

In another notebook, we have two ways to “run” the code in notebook1.

%run command#

We can use the %run magic to run the code in notebook1:

%run ./path/to/notebook1 $foo=“hello” $bar=“world”

The path of notebook1 is its relative path to the current notebook. We can give parameters to notebook1 via the ‘$’ assignment. In notebook1, you can then retrieve the value via dbutils.notebook.get() method.

It will “import” the functions and variables from notebook1 to the current notebooks. So you can use those functions in your current notebook. In this sense, it it like from some_module import * in Python.

using dbutils.notebook.run()#

We can also run notebook1 like this:

dbutils.notebook.run(path/to/notebook1, 30, {foo: fooVal, bar: barVal})

The parameters for notebook1 is provided in the 3rd argument.

This will run notebook1 in a ephemeral job. No functions or variable from that notebook will be exposed to your current notebook.

To return results from called notebook, we can use dbutils.notebook.exit(“result_str”). This is rather limited, but it seems currently only string result is supported. You can be creative in how to interpret the returned string though, e.g., as the name of table.

Import a Python module#

Since Databricks runtime 11.3, we can import Python module in the workspace. Create a normal Python module like you often do, and you can then import that module in a notebook. It conforms better to the Python ideology of “explicit is better than implicit”.

I create a demo directory test the feature, which has the following structure.

.
|____hello.py
|____my-notebook
|____utility
| |____math_ops.py

Content of math_ops.py:

def power(x, y):
    return x**y

Content of hello.py:

In the notebook my-notebook, we can run the following code without error:

import hello

import utility.math_ops as m_op

m_op.power(2, 3)

autoreload#

Since Databricks runtime 11.0, for the notebooks, it is using the Ipython kernel under the hood (source here). Note that like typical Ipython notebook, when you import a Python module, the module is not reloaded when there are changes to the module code by default. For example, if you add a new method or change method code for the module, it will not work.

Instead, we need to load the autoreload extension for the notebook (also mentioned in the official doc):

%load_ext autoreload
%autoreload 2

To check the documentation of autoreload, you can run %autoreload?.

If you don’t want to the autoreload magic, to make your module update work, you have to manually detach and attach to the cluster.

ref:

References#