How to open an external file from a Colab Jupyter notebook? - r

I have a Jupyter notebook in which I create an HTML file. I then open this HTML file in a new browser tab.
It's an R analysis, so I opened the HTML file using browseURL().
However, when I use a Colab hosted version of the notebook, nothing happens when I try to open the page.
Here's a reproducible example: https://colab.research.google.com/drive/1BfVDsDnXQwEpy4HwPKWkUC9Bpaz6r0Kv
Other things I tried that didn't work:
Using system2() instead of browseURL().
Setting the browser option to the open system tool via options(browser = "/usr/bin/open").
Is there another value for browser I should use? Is there a permission setting I should change?
Otherwise, how can I get the Colab notebook to display the HTML page I created?

As far as I know, Colab stores its files in Google Drive and judging by this link, Google doesn't support serving HTML from there.
You could save them as png files and display them like this if you're OK to have to install a few extra libraries.
install.packages('webshot')
webshot::install_phantomjs()
library(webshot)
webshot('https://www.halfbakery.com/', 'hb.png', delay = 2)
install.packages("png")
library(png)
img <- readPNG('hb.png')
grid::grid.raster(img)
I have tried it in Colab and it works there. I should add that browseURL still doesn't work on the file although double clicking on the file in the Files page does bring up a window with the png correctly displayed.

Related

downloading from a url with redirects

I am trying to download a files (it could be png, csv or pdf) from an internal url which seems to have redirects. I have tried downloading using download.file with extra=-L option, the download function from downloader package and the httr package.
However, in all cases I get a file of 768 B. Saving this file as a .txt shows that there is a another url within that. I have tried using that url, but without success. I see the following message (along with other information) in the downloaded file
Since your browser does not support JavaScript, you must press the Resume button once to proceed.
What works is if I stick that url in the browseURL function, I get a prompt to save the desired file.
I need to run the script in batch mode for reproducibility purposes, is there any way to run the browseURL in batch mode? or is there any other tool that would be useful here? (I have tried read.csv, fread etc. without any success). Unfortunately, I can't share the url as it is internal to my organization.
Thanks

How to upload ipynb with images in it's markdown cells to google colab

I want to upload an .ipynb which has images in it's markdown cells, but when I upload it to google colab it doesn't show the images. I tried uploading the file solely and with it's '.ipynb_checkpoints' folder and in both cases problem was there.
probably the reason is the way colab saves images in it's cell(image and data in the same place something like this)
but jupyter-notebook on windows saves the image on cell like this
so how can I upload an .ipynb which has images in it's markdown cells from pc to google colab? by the way the answer is not probably in this link How to embed image or picture in jupyter notebook, either from a local machine or from a web resource? and also I know when I open the .ipynb in text(notepad) I can access the data (=in the way colab saves the image) but I don't want to copy and paste 100s of images manually.

Google Drive does not recognise .ipynb file as colab notebook

If I create a new Colab notebook in Google Drive, it is recognised as a Colab notebook.
If I upload a .ipynb file into Google Drive, it is not recognised as a Colab notebook.
In the example below, file1.ipynb was created in Google Drive (and is recognised). This file was then downloaded, renamed to file2.ipynb, and then uploaded. The duplicate is not recognised (but you can open and run it in Colab, so the file is OK).
What could be wrong?
The JSON in the .ipynb file that is recognised, and the copy that is downloaded and then uploaded is identical.
As mentioned before, Google Colab usually recognizes the .ipynb files that created within itself. Uploading directly to Google Drive or even inside the folder "Colab Notebooks" doesnt't work.
But there is a way to upload .ipynb files to Colab;
Open https://colab.research.google.com
Go to File > Upload Notebook
Notebooks that uploaded with this method will be shown as Colab files in your drive as well, even if you change the location.
I have created a new empty Colab file. Download it. Then upload it to Google Drive again.
The content of the new file and the uploaded file is the same. But the displayed logo is different. So it's not about the content. Google must be tracking which file it create itself from its system, and which file is uploaded.
So, I think it's impossible to find a way to create an ipynb file that will be recognized as Colab.

Trying to use live server package on text editor atom

I am trying to use the live server package which works pretty much like livereload for editor Atom. The server runs fine, but the files do not get automatically refreshed whenever I make changes + save the files..
Not sure if this is normal, but when I open up the server on the browser, I am forwarded to a directory with all the files on the server. How do I change this so that the server renders HTML only and refreshes every time changes are made?
My php files with html elements dont open, so create the file like .html files, and everythings runs well. Use .atom-live-server.json on the root project to change some options like other browsers.
Edit 1: Have the solution !
1- Install Live Reload from livereload.com.
2- Install Live Reload from browser extension.
3- Copy and paste this before before body:
<script>document.write('<script src="http://' + (location.host || 'localhost').split(':')[0] + ':35729/livereload.js?snipver=1"></' + 'script>')</script>
4- Execute live reload and the browser and still works for php files.
You haven't set the project folder for the live-server to operate from, or you've saved the file you want to view outside of where it is running from.
In the project window on the left, add the project folder that you are using, and remove anything else. Or save your file in that location.
I had the same problem with atom-live-server and Google Chrome browser. Finally made it work using these steps:
Open your Browser
In Project pane open the project folder that you want to test
Start the atom-live-server
It should automatically open a new page in your browser. Right click on it and select Inspect -> it will open Console window.
Your Live reload is enabled. Notice the message in the Console.
After you make changes in your javascript or html in Atom save your changes (Ctrl + S) and the browser page will be updated.
I fixed it by adding the <head> and <body> tags to the html file I was editing. (even though I don't always include them anymore at least one is needed for the "Live reload" server to work.)
Live reload won't work unless there's a tag in the HTML the script can be injected on (body, head, and svg).
see this github issue!

IPython notebook Jupyter cannot download as .py

After I open Jupyter and write down codes, I want to download it as .py file, but it is always downloaded as HTML. How can I fix it?
Try refreshing the web page and download as .py should show up.
You may need to zoom out on your browser in order to see the options below html. With jupyter notebook, if the browser is zoomed in then some options may fall off the bottom of the browser.

Resources