Skip to content

Commit db99bc6

Browse files
author
Aditya Dakur Rudraiah
committed
adding SQL notebook initial changes
1 parent c7bd743 commit db99bc6

3 files changed

Lines changed: 166 additions & 0 deletions

File tree

‎ConvertToSQLNoteBook.ps1‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
function ConvertTo-SQLNoteBook {
2+
<#
3+
.Example
4+
ConvertTo-PowerShellNoteBook -InputFileName c:\Temp\demo.txt -OutputNotebookName c:\Temp\demo.ipynb
5+
#>
6+
param(
7+
$InputFileName,
8+
$OutputNotebookName
9+
)
10+
11+
New-SQLNotebook -NoteBookName $OutputNotebookName {
12+
$content = Get-Content $InputFileName | Out-String
13+
$insideBlockComment = $false
14+
$lines = [regex]::split($content,'\r\n')
15+
16+
foreach ($line in $lines) {
17+
if($line.Length -eq 0) { continue }
18+
if ($insideBlockComment) {
19+
$blockComment += $line
20+
if ($line -match '\*/') {
21+
$insideBlockComment = $false
22+
Add-NotebookMarkdown $blockComment
23+
Write-Verbose "block comment - $blockComment" -Verbose
24+
$blockComment = ''
25+
continue
26+
}
27+
28+
29+
Write-Verbose "block comment - $blockComment" -Verbose
30+
}
31+
elseif ($line -match '/\*') {
32+
$insideBlockComment = $true;
33+
$blockComment += $line
34+
#Write-Verbose "comment - $line" -Verbose
35+
36+
}else{
37+
if ($line -match '--') {
38+
# Write-Verbose "comment - $line" -Verbose
39+
Add-NotebookMarkdown $line
40+
}
41+
else{
42+
Add-NotebookCode $line
43+
#Write-Verbose "code - $line" -Verbose
44+
}
45+
}
46+
}
47+
}
48+
}

‎PowerShellNotebookDSL.ps1‎

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,3 +255,119 @@ function New-PSNotebook {
255255
}
256256

257257
}
258+
function New-SQLNotebook {
259+
<#
260+
.SYNOPSIS
261+
Creates a new PowerShell Notebook that can be returned as text or saves as a `ipynb` file.
262+
263+
.Description
264+
New-PSNotebook takes a script block in which these two functions can be be use to contstruct a PowerShell Notebook `Add-NotebookMarkdown`, `Add-NotebookCode`.
265+
Additionally, you can use the `-IncludeCodeResults` switch to execute the PowerSHell code and include the results in the notebook.
266+
267+
.Example
268+
# creates a new notebook, and saves it as TestNotebook.ipynb
269+
270+
New-PSNotebook -NoteBookName .\TestNotebook {
271+
Add-NotebookMarkdown -markdown "# This is a H1 tag"
272+
Add-NotebookCode -code 'Hello World'
273+
}
274+
275+
.Example
276+
# creates a new notebook, executes the PowerShell then includes it the block, and saves it as TestNotebook.ipynb
277+
278+
New-PSNotebook -NoteBookName .\TestNotebook -IncludeCodeResults {
279+
Add-NotebookMarkdown -markdown "# This is a H1 tag"
280+
Add-NotebookCode -code 'Hello World'
281+
}
282+
283+
.Example
284+
# creates a new notebook, and returns it as text
285+
286+
New-PSNotebook -AsText {
287+
Add-NotebookMarkdown -markdown "# This is a H1 tag"
288+
Add-NotebookCode -code 'Hello World'
289+
}
290+
291+
{
292+
"metadata": {
293+
"kernelspec": {
294+
"name": "powershell",
295+
"display_name": "PowerShell"
296+
},
297+
"language_info": {
298+
"name": "powershell",
299+
"codemirror_mode": "shell",
300+
"mimetype": "text/x-sh",
301+
"file_extension": ".ps1"
302+
}
303+
},
304+
"nbformat_minor": 2,
305+
"nbformat": 4,
306+
"cells": [{
307+
"cell_type": "markdown",
308+
"source": "# This is a H1 tag"
309+
}, {
310+
"cell_type": "code",
311+
"source": "Hello World",
312+
"metadata": {
313+
"azdata_cell_guid": "a7b91b6c-f57f-4d57-8cc4-7773d7f22756"
314+
},
315+
"outputs": [{
316+
"name": "stdout",
317+
"output_type": "stream",
318+
"text": ""
319+
}]
320+
}]
321+
}
322+
#>
323+
param(
324+
[Scriptblock]$sb,
325+
$NoteBookName,
326+
[Switch]$AsText,
327+
[Switch]$IncludeCodeResults
328+
)
329+
330+
$script:codeBlocks = @()
331+
if ($IncludeCodeResults) {
332+
$Script:IncludeCodeResults = $IncludeCodeResults
333+
$Script:PSNotebookRunspace = New-PSNotebookRunspace
334+
}
335+
336+
&$sb
337+
338+
$result = @"
339+
{
340+
"metadata": {
341+
"kernelspec": {
342+
"name": "sql",
343+
"display_name": "SQL"
344+
},
345+
"language_info": {
346+
"name": "sql",
347+
"codemirror_mode": "shell",
348+
"mimetype": "text/x-sh",
349+
"file_extension": ".sql"
350+
}
351+
},
352+
"nbformat_minor": 2,
353+
"nbformat": 4,
354+
"cells": [
355+
$($script:codeBlocks -join ',')
356+
]
357+
}
358+
"@
359+
360+
$Script:IncludeCodeResults = $false
361+
if ($Script:PSNotebookRunspace) {
362+
$Script:PSNotebookRunspace.Close()
363+
$Script:PSNotebookRunspace = $null
364+
}
365+
366+
if ($AsText) {
367+
return $result
368+
}
369+
else {
370+
$result | Set-Content -Encoding UTF8 -Path $NoteBookName
371+
}
372+
373+
}

‎SQLNotebook.psm1‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
. $PSScriptRoot\ConvertToSQLNoteBook.ps1
2+
. $PSScriptRoot\PowerShellNotebookDSL.ps1

0 commit comments

Comments
 (0)