Problem with IFrame in Jupyter notebook


#1

Hi,

I have been trying to make use of IFrame to display a PDF file inside a Jupyter notebook. This has worked for me previously (many months ago), but fails now.

Sample code (url instead of PDF):

from IPython.display import IFrame    
IFrame('http://www.stackoverflow.org', width=800, height=650)

This works for me in a local Jupyter notebook, but not on the collab. I am having the same version of IPython on both (5.5.0).


#2

I did find the solution to my original problem of loading a PDF. The problem was when I was providing an absolute path, such as:

report_path = "/home/jovyan/report/HBP_VF_Report_20180216-152201.pdf"
from IPython.display import IFrame    
IFrame(report_path, width=900, height=650)

This can be fixed by converting it into a relative path:

report_path = "/home/jovyan/report/HBP_VF_Report_20180216-152201.pdf"
rel_report_path = os.path.relpath(report_path)
from IPython.display import IFrame    
IFrame(rel_report_path, width=900, height=650)

Not sure why this issue arises. Also the problem posted with the URL (instead of PDF) persists.


#3

This problem still exists in using a URL. Could someone take a look?


#4

Found solutions… courtesy: @lupascu, @andrew_davison
Listing these below for future reference:

Using IFrame inside a block (e.g. if statement)

PROBLEM:

if True:
    from IPython.display import IFrame    
    IFrame("./myfile.pdf", width=900, height=650)

will not show any output.

SOLUTION:
use display():

if True:
    from IPython.display import IFrame    
    display(IFrame("./myfile.pdf", width=900, height=650))

Using IFrame to display URLs

Note that http urls will not be displayed. Only https are allowed inside the Collaboratory.