-
Notifications
You must be signed in to change notification settings - Fork 0
/
00_first_notebook_script.py
277 lines (163 loc) · 5.03 KB
/
00_first_notebook_script.py
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#!/usr/bin/env python
# coding: utf-8
# # Jupyter Notebook Document
# ## *Literate analysis* in Jupyter Notebook
#
# In *literate analysis*, **documentation**, **specification**, **explanation**, **interpretation,** and **code** co-exist in a single document that presents the analysis process in a narrative format.
#
# In contrast to scripts, where code is the default kind of content and everything else must be shoe-horned into comments, notebooks are based on **cells**.
#
# **Cells can be:**
#
# - *Markdown* formatted text cells
# - *Code* cells (python code is default in python notebooks but others are possible)
# - *Raw*, unformatted text
#
# ### Code Cells
#
# Code and results appear in specially designated content blocks.
# Execute a cell with <kbd>shift</kbd> + <kbd>return</kbd> or use the <kbd>▶</kbd> button in the toolbar.
# In[2]:
2 + 2
# ### Markdown Cells
# Markdown cells support a range of formatting options via [Markdown](https://daringfireball.net/projects/markdown/) and html5
#
# - *italics*
# - **bold**
# - `code`
# - [links](https://jupyterlab.readthedocs.io/)
# - Inline LaTeX equations: $E = mc^{2}$
#
#
# \begin{equation}
# e^x=\sum_{i=0}^\infty \frac{1}{i!}x^i
# \end{equation}
# ### Raw Cells
#
# Raw cells show their contents. For example, the raw cell below shows the raw markdown from the cell above.
- *italics*
- **bold**
- `code`
- [links](https://jupyterlab.readthedocs.io/)
- Inline LaTeX equations: $E = mc^{2}$
\begin{equation}
e^x=\sum_{i=0}^\infty \frac{1}{i!}x^i
\end{equation}
# ## Hierarchical Organization
#
# Markdown supports six levels of headers to organize your document. Headers are text preceded by one to six hash symbols. The more hash symbols, the lower the level.
#
# The outline pane on the left side shows the document outline and permits quick navigation.
#
```
# H1 heading (top level header)
## H2
### H3
#### H4
##### H5
###### H6 (deepest level header)
```
# In[ ]:
# In[ ]:
# In[12]:
# scroll down #
# In[ ]:
# In[ ]:
# In[ ]:
# ### Heading Anchors
# [Code Cells](#Code-Cells)
#
# `[Code Cells](#Code-Cells)`
# ---
# # Jupyter Interface
#
# ## Panels
#
# 1. File browser
# 2. Active kernel list
# 3. Outline view
# 4. Extensions
# 5. Inspector
# 6. Debug
# ## Menus
#
# - View > Activate Command Palette
# - Edit > Split / Merge Cells
#
# ## Documents
#
# 1. Notebooks
# 2. Consoles
# 3. Terminals (to underlying OS)
# ## Editing Notebooks
# ### Re-order Cells
#
# #### <span style="color:red"> Move me! </span>
# ### Multiple Cursors
# Sometimes, you need to edit several places at once. Press command (macOS) or alt (Windows) and click to place additional cursors.
# In[13]:
# Change the 4 "dd"s to "gg"
+ gg.theme_bw()
+ gg.theme(figure_size=(10,6))
+ gg.theme(axis_text_x=dd.element_text(angle=30, hjust=1))
# ---
# # Features
# ## Execute Terminal Commands in host system
# In[ ]:
#!pip install
# In[3]:
get_ipython().system(' ls')
# In[4]:
get_ipython().run_cell_magic('bash', '', '#Unix hosts only\nls\n')
# ### Jupytext Extension
#
# Pair the notebook with a "lightscript" file. Lightscript is can be executed like a python script file (.py) but also contains all the info needed to be reconstituted into a jupyter notebook. It strips out non-content metadata in the process, so lightscript files are a good choice to use with github (the file differences will only be actual differences in content).
#
# With lightscript, you can also tag individual cells to be active/inactive only in certain formats. This is useful when you are writing a notebook that uses special notebook-only features that should also be runnable as a script.
#
# Use the *Pair Notebook with Lightscript* command in *View > Activate Command Palette*.
#
# Refer to the Jupytext Manual [https://jupytext.readthedocs.io/en/latest/install.html](https://jupytext.readthedocs.io/en/latest/install.html) for more details.
# In[ ]:
# In[ ]:
# ### TQDM
#
# A popular progress bar tool integrated with many data science packages.
# In[5]:
# has active-ipynb tag
from tqdm.notebook import tqdm
# has active-py tag
from tqdm import tqdm
# In[6]:
import time
for i in tqdm(range(20)):
time.sleep(.5)
# ### Time and Timeit cell magics
# In[7]:
get_ipython().run_cell_magic('time', '', 'for i in tqdm(range(20)):\n time.sleep(.5)\n')
# In[8]:
get_ipython().run_cell_magic('timeit', '', 'sum(x**2 for x in range(1,100000,10))\n')
# ### Jupyter Widgets
#
# Widgets are a collection of simple interactive elements that can be used to create a GUI in Jupyter.
#
# Uses:
#
# - enhance teaching and communication
# - support specialized interactive data analysis
# - create simple interfaces for human coding tasks like tagging, sorting, ranking or classifying.
#
#
# https://ipywidgets.readthedocs.io/en/stable/lite/lab/
# In[9]:
import ipywidgets as widgets
from IPython.display import display
# In[10]:
date_w = widgets.DatetimePicker(
description='Pick a Time',
disabled=False
)
display(date_w)
# In[11]:
print(date_w.value)
# In[ ]: