forked from guidooswald/DatabricksGitIntegration
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRunning Code.py
More file actions
136 lines (80 loc) · 3.66 KB
/
Running Code.py
File metadata and controls
136 lines (80 loc) · 3.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# Databricks notebook source
# MAGIC %md # Running Code
# COMMAND ----------
# MAGIC %md First and foremost, the Jupyter Notebook is an interactive environment for writing and running code. The notebook is capable of running code in a wide range of languages. However, each notebook is associated with a single kernel. This notebook is associated with the IPython kernel, therefore runs Python code.
# COMMAND ----------
# MAGIC %md ## Code cells allow you to enter and run code
# COMMAND ----------
# MAGIC %md Run a code cell using `Shift-Enter` or pressing the <button class='btn btn-default btn-xs'><i class="icon-step-forward fa fa-play"></i></button> button in the toolbar above:
# COMMAND ----------
a = 10
# COMMAND ----------
print(a)
# COMMAND ----------
# MAGIC %md There are two other keyboard shortcuts for running code:
# MAGIC
# MAGIC * `Alt-Enter` runs the current cell and inserts a new one below.
# MAGIC * `Ctrl-Enter` run the current cell and enters command mode.
# COMMAND ----------
# MAGIC %md ## Managing the Kernel
# COMMAND ----------
# MAGIC %md Code is run in a separate process called the Kernel. The Kernel can be interrupted or restarted. Try running the following cell and then hit the <button class='btn btn-default btn-xs'><i class='icon-stop fa fa-stop'></i></button> button in the toolbar above.
# COMMAND ----------
import time
time.sleep(10)
# COMMAND ----------
# MAGIC %md If the Kernel dies you will be prompted to restart it. Here we call the low-level system libc.time routine with the wrong argument via
# MAGIC ctypes to segfault the Python interpreter:
# COMMAND ----------
import sys
from ctypes import CDLL
# This will crash a Linux or Mac system
# equivalent calls can be made on Windows
# Uncomment these lines if you would like to see the segfault
# dll = 'dylib' if sys.platform == 'darwin' else 'so.6'
# libc = CDLL("libc.%s" % dll)
# libc.time(-1) # BOOM!!
# COMMAND ----------
# MAGIC %md ## Cell menu
# COMMAND ----------
# MAGIC %md The "Cell" menu has a number of menu items for running code in different ways. These includes:
# MAGIC
# MAGIC * Run and Select Below
# MAGIC * Run and Insert Below
# MAGIC * Run All
# MAGIC * Run All Above
# MAGIC * Run All Below
# COMMAND ----------
# MAGIC %md ## Restarting the kernels
# COMMAND ----------
# MAGIC %md The kernel maintains the state of a notebook's computations. You can reset this state by restarting the kernel. This is done by clicking on the <button class='btn btn-default btn-xs'><i class='fa fa-repeat icon-repeat'></i></button> in the toolbar above.
# COMMAND ----------
# MAGIC %md ## sys.stdout and sys.stderr
# COMMAND ----------
# MAGIC %md The stdout and stderr streams are displayed as text in the output area.
# COMMAND ----------
print("hi, stdout")
# COMMAND ----------
from __future__ import print_function
print('hi, stderr', file=sys.stderr)
# COMMAND ----------
# MAGIC %md ## Output is asynchronous
# COMMAND ----------
# MAGIC %md All output is displayed asynchronously as it is generated in the Kernel. If you execute the next cell, you will see the output one piece at a time, not all at the end.
# COMMAND ----------
import time, sys
for i in range(8):
print(i)
time.sleep(0.5)
# COMMAND ----------
# MAGIC %md ## Large outputs
# COMMAND ----------
# MAGIC %md To better handle large outputs, the output area can be collapsed. Run the following cell and then single- or double- click on the active area to the left of the output:
# COMMAND ----------
for i in range(50):
print(i)
# COMMAND ----------
# MAGIC %md Beyond a certain point, output will scroll automatically:
# COMMAND ----------
for i in range(500):
print(2**i - 1)