holoviz / lumen

Illuminate your data.
https://lumen.holoviz.org
BSD 3-Clause "New" or "Revised" License
177 stars 20 forks source link

Support downloading pdf for YdataProfiler #720

Closed ahuang11 closed 1 month ago

ahuang11 commented 1 month ago

Requires weasyprint and PyPDF2 because weasyprint alone had empty pages in between and I couldnt figure out which CSS element was triggering that.

https://github.com/user-attachments/assets/8fe3e749-21b6-4262-b973-e3a96ce7e841

Requires (preferably under conda install section)

I think this method is preferable over selenium because this includes data/images from HTML tabs

example pdf profile_report (7).pdf

And it's also tested on deployment

codecov[bot] commented 1 month ago

Codecov Report

Attention: Patch coverage is 8.69565% with 21 lines in your changes missing coverage. Please review.

Project coverage is 55.72%. Comparing base (cc86118) to head (bf50af8). Report is 9 commits behind head on main.

Files with missing lines Patch % Lines
lumen/views/base.py 8.69% 21 Missing :warning:
Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #720 +/- ## ========================================== - Coverage 56.02% 55.72% -0.31% ========================================== Files 98 98 Lines 11828 11901 +73 ========================================== + Hits 6627 6632 +5 - Misses 5201 5269 +68 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

philippjfr commented 1 month ago

Was really rather hoping this could happen on the frontend.

ahuang11 commented 1 month ago

I'm not sure it's possible to do with frontend.

Did some testing with https://docs.profiling.ydata.ai/latest/examples/census/census_report.html

  1. using right click -> print, I only see a subset of the dataset profiler

image

  1. using HTML focus/print, I run into cross origin issue:
    Uncaught SecurityError: Failed to read a named property 'print' from 'Window': Blocked a frame with origin "null" from accessing a cross-origin frame.
    at openAndPrintIframe (testdl.html:17:38)
    at HTMLButtonElement.onclick (testdl.html:10:44)

    (Same issue if I save the census_report.html locally)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Iframe Print Example</title>
</head>
<body>
    <iframe id="myIframe" src="https://docs.profiling.ydata.ai/latest/examples/census/census_report.html" width="600" height="400"></iframe>
    <button onclick="openAndPrintIframe()">Open and Print Iframe</button>
    <script>
        function openAndPrintIframe() {
            var iframe = document.getElementById('myIframe');
            console.log(iframe);
            if (iframe) {
                iframe.contentWindow.focus();
                iframe.contentWindow.print();
            } else {
                console.error("Iframe not found");
            }
        }
    </script>
</body>
</html>
  1. I also try opening the iframe source, and if so, print prompt shows the document correctly

image

However, I can't automate the two step process

var newWindow = window.open(iframe.src);
newWindow.onload = function() {
  newWindow.print();
};
  1. If I use Panel, I also run into shadow root issues.
    
    import panel as pn

pn.extension()

iframe = pn.pane.HTML( '' )

button = pn.widgets.Button(name="Print Iframe", button_type="primary")

js_callback = """ var iframe = document.getElementById("myIframe"); if (iframe) { var newWindow = window.open(iframe.src); } else { console.error("Iframe not found"); } """

button.js_on_click(args={}, code=js_callback)

layout = pn.Column(iframe, button)

layout.show()



Would appreciate ideas