mljar / mercury

Convert Jupyter Notebooks to Web Apps
https://RunMercury.com
GNU Affero General Public License v3.0
3.87k stars 243 forks source link

PDF widget not function properly #428

Closed tinidoooooooog closed 4 months ago

tinidoooooooog commented 4 months ago

Hi,

I have an example pdf file and the simple code is run as follows:

import mercury as mr app = mr.App(title="show pdf", description="show pdf", allow_download=False) mr.PDF("example.pdf")

but nothing displayed. Can you help please fix this bug?

pplonski commented 4 months ago

Hi @tinidoooooooog,

Could you please double check if file example.pdf exists in your directory and is accessible with provided path. I've just checked and it looks like this on my machine

image

What is your use case? What would you like to build with Mercury?

@apjanusz I think we are missing docs/example/tutorial for displaying PDF in Jupyter notebooks and Mercury.

tinidoooooooog commented 4 months ago

Thanks for quick response.

I am running Mercury using docker +nginx on my remote machine instead of on my local machine (btw, PDF viewer works normally in my local version of mercury as well)

I am doubting if the problem occurs as the IFrame function is called Could you please give me some suggestions? Thanks!

pplonski commented 4 months ago

Do you get error when trying to access file with some other Python code?

content = None
with open(file_path, "rb") as fin:
    content = fin.read()

@apjanusz could you please check if there is a good error message if we try to access not existing pdf file.

tinidoooooooog commented 4 months ago

In my case, error message show up correctly file does not exist and no message pop up if existing pdf file name is given.

tinidoooooooog commented 4 months ago

@pplonski I found the root cause to this issue. The issue comes from the Data URI limitations. In Firefox, the size limitations are looser than in Chromium-based browsers (<1.5 MB) so mr.PDF works as expected in Firefox.

It would be helpful If you can help fix this using approaches not based on data URI. Thank you!

tinidoooooooog commented 4 months ago

I resolved the issue. By saving the file in the media folder, one can access it via from IPython.display import Iframe filepath = '/app/mercury/media/abcde12331/example.pdf' # assuming this

#extract media/abcde12331/example.pdf from {filepath} url=os.environ.get("MERCURY_SERVER_URL"),'/'.join(filepath.split()[3:]) display(Iframe(src=url,width=width, height=height))