-
Notifications
You must be signed in to change notification settings - Fork 26
/
GetNotebook.ps1
85 lines (75 loc) · 3.9 KB
/
GetNotebook.ps1
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
function Get-Notebook {
<#
.SYNOPSIS
Get-Notebook reads the metadata of Jupyter Notebooks
.Example
Get-Notebook .\samplenotebook\Chapter01code.ipynb
NoteBookName : Chapter01code.ipynb
KernelName : powershell
CodeBlocks : 83
MarkdownBlocks : 23
FullName : C:\Users\Douglas\Documents\GitHub\MyPrivateGit\PowerShellNotebook\samplenotebook\Chapter01code.ipynb
FormatStyle : PowerShell
HasParameterizedCell : False
.Example
Get-Notebook .\samplenotebook\| Format-Table
NoteBookName KernelName CodeBlocks MarkdownBlocks FullName
------------ ---------- ---------- -------------- ----------------
Chapter01code.ipynb powershell 83 23 C:\Users\Douglas\Documents\GitHub\MyPrivateGit\Power...
csharp.ipynb .net-csharp 1 0 C:\Users\Douglas\Documents\GitHub\MyPrivateGit\Power...
fsharp.ipynb .net-fsharp 1 0 C:\Users\Douglas\Documents\GitHub\MyPrivateGit\Power...
powershell.ipynb .net-powershell 1 0 C:\Users\Douglas\Documents\GitHub\MyPrivateGit\Power...
python.ipynb python3 1 0 C:\Users\Douglas\Documents\GitHub\MyPrivateGit\Power...
SingleCodeBlock.ipynb powershell 1 0 C:\Users\Douglas\Documents\GitHub\MyPrivateGit\Power
.Example
Get-Notebook -recurse | group KernelName
Count Name Group
----- ---- -----
98 powershell {@{NoteBookName=Aaron_CopyWorkspace.ipynb; KernelName=powershell; CodeBlocks=14; Mar...
2 not found {@{NoteBookName=BPCheck.ipynb; KernelName=not found; CodeBlocks=0; MarkdownBlocks=0;...
36 SQL {@{NoteBookName=BPCheck.ipynb; KernelName=SQL; C...
29 python3 {@{NoteBookName=python install powershell_kernel.ipynb; KernelName=python3; CodeBloc...
781 .net-powershell {@{NoteBookName=Using_ConvertTo-SQLNoteBook.ipynb; KernelName=.net-powershell; CodeB...
3 pyspark3kernel {@{NoteBookName=load-sample-data-into-bdc.ipynb; KernelName=pyspark3kernel; C...
This command will allow you to serch through a directory & all sub directories to find Jupyter Notebooks & group them by Kernel used in each of those Notebook.
#>
param(
[Parameter(ValueFromPipelineByPropertyName=$true)]
[Alias('Fullname')]
$Path = $pwd,
$NoteBookName = '*',
[switch]$Recurse
)
begin {
$linguistNames = @{ #used by github to the render code in markup ```SQL will render as sql etc
'.net-csharp' = 'C#'
'.net-fsharp' = 'F#'
'.net-powershell' = 'PowerShell'
'not found' = ''
'powershell' = 'PowerShell'
'python3' = 'Python'
'Python [Root]' = 'Python'
'sql' = 'SQL'
}
}
process {
$targetName = "$($NotebookName).ipynb"
foreach ($file in Get-ChildItem $Path $targetName -Recurse:$Recurse) {
$r = Get-Content $file.fullname | ConvertFrom-Json
$kernelspecName = $r.metadata.kernelspec.name
if (!$kernelspecName) { $kernelspecName = "not found" }
$counts = $r.cells | Group-Object cell_type -AsHashTable
[PSCustomObject][Ordered]@{
NoteBookName = $file.Name
KernelName = $kernelspecName
CodeBlocks = $counts.code.Count
MarkdownBlocks = $counts.markdown.Count
FullName = $file.FullName
FormatStyle = $linguistNames[$kernelspecName]
LanguageName = $r.metadata.language_info.name
Lexer = $r.metadata.language_info.pygments_lexer
HasParameterizedCell = $r.cells.metadata.tags -contains "parameters"
}
}
}
}