3

How could one read a markdown table from a text block and import in into a variable in a code block in the same PowerShell notebook?

Attempted to import with PowerShell

$b=Import-Csv -Path <> -Delimiter '|'

enter image description here

Couldn't figure out how to point the -Path parameter to the text block in the same notebook. Using a .ipynb file in Azure Data Studio.

Ayan Mullick
  • 67
  • 2
  • 11
  • 38
  • This might seem like an annoying question, but why do you want to do this? Depending on your use case, it would be simpler to store the markdown table as an external text file and manipulate from there. If you then want the contents of the same external file to be displayed as markdown in a cell, that's easy. – Matt L. Feb 20 '20 at 17:34
  • No, that's a fair question. I want the deployment script and parameters in one file for reviewing. For eg. have the markdown table in the text block with all VM parameters, size, location, Resource group, tier etc; then the script in the code block read the table from the same file to deploy the environment in Azure. So one single file for documentation and parameters [in the table] and the script in the code block. – Ayan Mullick Feb 20 '20 at 17:45
  • I am a little confused with the actual question as question itself suggests that you aren't able to reach the file from PowerShell (it might help to add some details on how your reach the file manually). The notes and answer are more on *how* to read the file (if that is the case, a example of the contents of the file might help). – iRon Feb 25 '20 at 13:49
  • 1
    I found some in formation that `.ipynb` are actually json files, suggesting that you might want to use [`ConvertFrom-Json`](https://learn.microsoft.com/powershell/module/microsoft.powershell.utility/convertfrom-json?view=powershell-7) otherwise, if it realy concerns a markdown table, this cmdlet [ConvertFrom-SourceTable](https://www.powershellgallery.com/packages/ConvertFrom-SourceTable) might help – iRon Feb 25 '20 at 13:49
  • @iRon, Exactly. As soon as I figure out the value for the `-Path` parameter.. – Ayan Mullick Feb 26 '20 at 01:24

2 Answers2

1

I believe the functionality you are looking for is not possible. As a workaround, I would suggest storing the cell markdown as a variable in Python first and using that variable to populate the printed markdown cell. Here is an example. I believe it will work in any notebook built on top of iPython:

#running this cell in your notebook will print the variable as Markdown
mymd = "# Some markdown"
from IPython.display import display, Markdown
display(Markdown(mymd))

Update: If you are worried that representing multi-line markdown is too complicated, you have two good options. First, use triples quotes to read the line breaks as part of the string:

mymd = """
| First Header  | Second Header |
| ------------- | ------------- |
| Content Cell  | Content Cell  |
| Content Cell  | Content Cell  |
"""

Option 2: Put your markdown in a file and read it to a string:

with open("somefile.md") as f:
    mymd = f.read()

Either option would benefit from a well documented and carefully followed workflow but would work well for this use case.

Matt L.
  • 3,431
  • 1
  • 15
  • 28
  • It'd get more complicated to create the table variable in Python and then consume it in PowerShell. https://imgur.com/TSzGNeO I'd like to keep the notebook to PowerShell and Markdown. – Ayan Mullick Feb 21 '20 at 00:33
  • 1
    Updated to address your concern. It's not a perfect solution, but it would work, which is better than the alternative. – Matt L. Feb 21 '20 at 15:19
  • This still results in 2 separate files. I can easily `Import-CSV` from a separate file thru PowerShell too. I want a single notebook file for the parameters' documentation in a markdown table format and the script that would deploy the the resources. I feel Notebooks should enable a way to reference content in a text block from script in a code block. – Ayan Mullick Feb 21 '20 at 17:01
  • 1
    They don't. Not that I know of. – Matt L. Feb 21 '20 at 17:10
0

As per comment on the question, the .ipynb appears to contain JSON formatted text.
Quote about JSON from WikiPedia:

JSON is an open-standard file format or data interchange format that uses human-readable text to transmit data objects consisting of attribute–value pairs and array data types (or any other serializable value). It is a very common data format, with a diverse range of applications, such as serving as replacement for XML in AJAX systems.

JSON is a language-independent data format. It was derived from JavaScript, but many modern programming languages include code to generate and parse JSON-format data. The official Internet media type for JSON is application/json. JSON filenames use the extension .json.

Also PowerShell has its own "cmdlet" commands for managing JSON files: ConvertTo-Json and ConvertFrom-Json.
The ConvertFrom-Json (and the ConvertTo-Json) cmdlet don't have a -Path parameter, instead it will convert from a -InputObject variable or stream, if the information comes from a file, you can use the Get-Content cmdlet to retrieve your data from a file:

$Data = Get-Content -Path .\YourFile.ipynb | ConvertFrom-Json

If your file is actually not provided through a file system but from a web page on the internet (which I suspect) you need rely on the Invoke-WebRequest cmdlet or if it concerns a web API on the Invoke-RestMethod cmdlet. For these cmdlets you need to figure out and supply more details like the URL you need to address.

iRon
  • 20,463
  • 10
  • 53
  • 79
  • Yes, the .ipynb file is on a file system. However, pointing the `-Path` parameter to a file will get the content of the entire file, including the code block. Then one needs 2 separate files; one for the code and one for the text. The question is how can one point the `-Path` to a specific text block in the same file so that the code and the markdown table can be in one single file. – Ayan Mullick Feb 26 '20 at 16:24
  • You can't with `-Path`, but there are other ways. Can you provide an example of the concerned file? – iRon Feb 26 '20 at 17:22
  • https://github.com/Ayanmullick/test/blob/master/testfolder/test.ipynb. How could one populate the `$Data` variable with the contents of the table in the text block without using another file? – Ayan Mullick Feb 26 '20 at 19:02
  • 1
    Thank for the file, it makes it a lot clearer what you actually trying to do. Unfortunately, I don't have an answer for that. Maybe this helps: [ipynb import another ipynb file](https://stackoverflow.com/questions/20186344/ipynb-import-another-ipynb-file) – iRon Feb 26 '20 at 19:53