VSCode Jupyter Notebook, how to change presentation/add mimetypes (changing the notebook output renderer options) - visual-studio-code

I've referenced this question: Change mimetype for VSC Jupyter Notebook ouput
And thoroughly googled, but I still can't seem to change the mime type of my Jupyter Notebook. I have 2 .jpnyb files opened, one has the option of mimetypes of text/html, text/plain and the other gives a weird one of application/vnd.somemorewords
Here is a picture of what the difference between the two look like within the command palette when attempting to change the presentation. First file: https://ibb.co/yBkmVdz Second file: https://ibb.co/vhcz866
Where is the first file grabbing the mimetypes from? I can't find any information on a location of a file (settings.json has been edited) that just sets the standard across the board for the entire VS Code app. Please help if you can!
I've done a search of package.json files on my computer and looked at over 50+ files for some sort of information of the notebook display order and renderer.
I also tried changing the User settings for the work space referencing the question that I linked above.

Related

WOPI corrupting files on edit

I have a WOPI host running in a Blazor server application with all of the .wopitest tests passing for the desired functionality (others skipped).
When I upload a word document, I am able to view the document with no issues. I am also able to edit the document, however when I try and edit the document a second time, I get an error.
The error doesn't appear to be handled and seems to originate in the Office online javascript file.
Error on attempting second edit
Following the error, I am still able to open the document for viewing. It is the same behaviour if I use the 'Editing' button in the Office Online page or directly navigate to the editing page using an edit action url.
Supplementary information:
Using ngrok to debug locally
.NET 6
Using SQLite database for holding file information (including path to file)
Using local folders for storing file contents (e.g. 'data' folder containing all files)
Similar issues with .xslx files beign corrupted upon editing and requiring a 'repair' when opened with Excel. This repair removes cells containing text and indicates that it removes the theme.
Viewing a word document gives the following console errors View document error
The first editing of a word document gives the following console errors Edit document error
I was expecting to be able to repeatedly edit the document.
I tried opening the file in the Desktop version of Word and got the following error Desktop Word recover
Following a recover, the document appears to work as expected in Word (desktop) but still won't open for editing through WOPI.
Turns out it was the way the POST http request body was being saved.
Still not certain what was going wrong but somewhere along the way of writing the stream into a buffer and then saving that to a file corrupted the file.
I suspect the file stream was either truncating or adding a few bytes.
The interesting part being that Office Online was still able to view the file.
This indicates there is some tolerance for malformed files still being served.

What is the vscode command to open a file in preview

I wrote an extension for vscode. After installation the extension folder contains documentation in a markdown file. I want to provide a command that loads this file into the preview pane so it displays rendered with images and hyperlinks etc.
You can do this sort of thing interactively:
and I have the full path to the markdown file, so now all I need is details of the command that implements this context menu item.
Web search does not produce complete or usable results.
After cloning the VS Code repo and trawling through the source I discovered the markdown.showPreview and associated commands.
To give credit where due, Lex Li reported the corresponding package.json entry in a comment while I was looking.
Without parameters this previews the content of the active editor, but as I said in a comment, it supports an optional Uri parameter and the code looks like this:
let pathToManual = path.join(context.extensionPath, "manual.md");
let uriManual: vscode.Uri = vscode.Uri.file(pathToManual);
vscode.commands.executeCommand('markdown.showPreview', uriManual);
For information on constructing workspace relative paths see the answer from Mark. The joinPath method he uses requires a base path as a Uri which is conveniently available for the workspace but not for the extension path.
If you need information on things like showing preview to one side then given the dearth of documentation I recommend cloning the repo and searching it for "markdown.showPreview", then exploring nearby code. If you fold the methods it gets easier to survey your options.
Try:
vscode.commands.executeCommand("markdown.showPreview", vscode.Uri.joinPath(vscode.workspace.workspaceFolders[0].uri,'test.md'));
Your fileName there at the end of course. And that assumed you are in the first or only root of a workspace. You might be able to simplify it using:
vscode.Uri.path(<path to file)
instead of the joinPath that I used.

Convert HTML to a Word-document which can be edited in Word Online

Our users write in a rich-text field, pretty much like this one, and we would like for them to be able to export this as a Word Document in their OneDrive, preserving the formatting, and being able to open the file in Word Online.
I have no trouble creating new files using the https://graph.microsoft.com/v1.0/ api. The problem is the conversion to docx. The google api provides this conversion automatically, but I did not find that for Microsoft. I tried using html-docx-js and it almost works perfectly.
The file is created:
However when opening the file the following dialog pops up:
Opening in read-only mode works, the file shows with the correct formatting.
Downloading the file and opening in desktop word works perfectly (i.e. editing as well).
The HTML-content i use is a simple div with a few p-tags, so the "Objects that Word Online doesn't support" probably comes from html-docx-js.
Here's an example word-file that is created. This file can be opened as normal in desktop Word but only opened in read-only in Word Online
https://1drv.ms/w/s!AqpUGtnMiyurgwE543OscH7PdLnY
Any ideas?

Share rCharts via IPython Notebook

I have been able to embed this map in an IPython Notebook (which is sweet), but I am not clear on how I can share this with folks not using the Notebook. I am familiar with the bl.ocks.org viewer. It's great for standalone examples, but I am looking to share the rest of the analysis in the Notebook along with interactive charts. Neither the HTML conversion of the Notebook nor the nbviewer rendering can locate the map (I get a 404 message).
After the first 404 (with this gist), I changed the viewer function to capture the github location of the map file (V2). I am not yet clear why, but that change stopped nbviewer from even rendering the surrounding materials. Any thoughts on a better way to go about this?
The trouble is that the map is saved as a local HTML file (rChart_map.html) and is hence not accessible to nbviewer when you are trying to view it online.
Even if you upload rChart_map.html to the gist, it won't show up due to path issues. Locally, you need to refer to it as /files/rChart_map.html in your IPython notebook, whereas online, it has a different path. I had posted this issue earlier on twitter using the #IPython tag, but got no responses on how to debug.
So where does that leave us. Well, fortunately, most modern browsers allow an iframe to contain inline HTML using the srcdoc tag. This allows the generated .ipynb file to be standalone, as seen here, at the end of the file.
The key is to use the following code. The first line creates an iframe with inline html of the map and stores it in the python variable map2. The second line imports the necessary python modules and the third line displays the HTML. Note that we use h2[0], since map2 is an array, due to conversion from R, which is vectorized.
map2 = %R paste(capture.output(map$show('iframesrc', cdn = TRUE)), collapse = '\n')
from IPython.display import display, HTML
HTML(map2[0])
For this to work, you will need to have rCharts version > 0.4.1.
I am interested in making it easier for rCharts to be used in IPython notebooks. So any suggestions/feedback is welcome.

Hosting ipython notebooks on Github

I maintain a Github repository with currently two ipython notebook files.
My repository is here:
https://github.com/tschm/MosekRegression
When I try to open those files with the nbviewer, e.g. using
nbviewer.ipython.org
and inserting
http://nbviewer.ipython.org/urls/github.com/tschm/MosekRegression/blob/master/Data.ipynb
i get the 400 Bad Request Error. The file is good as it works when I make it available as a Gist. First research seems to indicate that this is a permission problem? I bet I do something stupid here...
Many thanks
Thomas
Expanding on #chuwy's answer, here's a breakdown of steps. The trick is to head to your file's page on github and then click on the "Raw" button:
then copy the url (minus the protocol string "http://"), and prepend it with "http://nbviewer.ipython.org/urls/"
So for example, if I have my iPython notebook:
https://github.com/watsonix/prediction_for_fun_and_profit/blob/master/statsmodel_outliers.ipynb
and I want to see it in the nbviewer, I click on "Raw" to get the URL:
https://raw.githubusercontent.com/watsonix/prediction_for_fun_and_profit/master/statsmodel_outliers.ipynb
which I use to form the URL:
http://nbviewer.ipython.org/urls/raw.githubusercontent.com/watsonix/prediction_for_fun_and_profit/master/statsmodel_outliers.ipynb
voila!
Valid link is http://nbviewer.ipython.org/urls/raw.github.com/tschm/MosekRegression/master/Minimum%20Variance.ipynb
Your link points to the github's html page with your file. You should give the raw file instead.
It now appears that gist automatically renders iPython notebooks! I have been using nbviewer, but I just noticed that GitHub renders the ipynb file perfectly.