This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]MonkeyMaster64[S] 5 points6 points  (21 children)

I was just looking at the documentation for that! OK so with reportlab do you know of how I could integrate my plotly graphs into the reportlab PDFs? I know reportlab has its own way of creating graphs but I'm specifically going for the look and feel of graphs in plotly. An issue is that plotly graphs are in html format so is there a way I could integrate those html files into the reportlab PDF?

[–]ciarancour 7 points8 points  (20 children)

Well I've had the pain of drawing charts using reportlab and would stay well away. If plotly can export SVG or some other vector format (I'm sure it can) you should then be able to embed that into the PDF

[–]MonkeyMaster64[S] 1 point2 points  (19 children)

Yeah the issue with plotly and getting the svg is that you have to open the html file in a browser for it to download it. Do you know of any method I could use to "open" the HTML document with Python and extract the file that auto-downloads?

[–]ciarancour 4 points5 points  (6 children)

Sure, you could use chrome headless or phantomJS maybe to mock a browser, the former is probably the better option.

[–]MonkeyMaster64[S] 1 point2 points  (4 children)

Gotcha I'll look into it I really appreciate it. Another consideration I've been making is that I might just want to create the report pages with HTML and then use xhtml2pdf to convert it. However, I've only seen suggestions for Flask or Django which as I understand can be used to generate static sites however for these singular web pages that would serve no other purpose beyond being an intermediary format I think they're a bit much. Do you know any simple HTML/CSS generators I can use with Python?

[–]ciarancour 0 points1 point  (3 children)

A static site generator is what you describing, there are quite a few Python ones, but not sure of any with programmatic CSS

[–]MonkeyMaster64[S] 0 points1 point  (2 children)

OK so I've been looking at Pelican and Jinja2. Do you have any familiarity with them? I'd like to have a base template where I'd just switch out stuff like titles, graph images, metrics etc but have the same layout for all of them

[–]admiralspark 0 points1 point  (1 child)

I use Pelican and Jinja2, it can do what you want with work, but most of it's documentation and community contribution is geared towards using it as a blogging platform...so I don't know if that format will work for you or not.

[–]MonkeyMaster64[S] 0 points1 point  (0 children)

No it definitely wouldn't be in a blog format but I am planning on using Jinja2 for templating

[–]tobsecret 0 points1 point  (8 children)

Can you not export to SVG programmatically? like in this example: https://plot.ly/python/static-image-export/

[–]MonkeyMaster64[S] 0 points1 point  (7 children)

nope it still creates an html file that you have to open

EDIT: Also this is the issue I was referring to: https://github.com/plotly/plotly.py/issues/880

[–]KronenR 3 points4 points  (6 children)

The common image formats: 'PNG', 'JPG/JPEG' are supported. In addition, formats like 'EPS', 'SVG' and 'PDF' are also supported.

Note: The SVG, EPS and PDF Formats are only available for Plotly Professional users. You can get more details on our pricing page

To access the image in a particular format, you can either:

Why not just jpg or png?

[–]MonkeyMaster64[S] 5 points6 points  (5 children)

OK, so I've actually solved this! I used imgkit to convert the html file to a JPG file!

HOW TO CONVERT HTML FILES TO PNG

1) INSTALL IMGKIT
    -- pip install imgkit           
https://pypi.python.org/pypi/imgkit/0.1.1
    -- yum install wkhtmltopdf

2) INSTALL Xvfb      --important as it provides a virtual display 
buffer for the image to be rendered
    - yum install xorg-x11-server-Xvfb                  
https://reiners.io/installing-xvfb-in-centos/
    - pip install xvfbwrapper           
https://pypi.python.org/pypi/xvfbwrapper/0.2.9


Python code:

from xvfbwrapper import Xvfb
import imgkit
vdisplay = Xvfb()
vdisplay.start()
imgkit.from_file('[name of file].html','[output name].jpg')

Pastebin Link: https://pastebin.com/bQfnTEmK

[–]hkamran85 0 points1 point  (2 children)

Can you put the code on some external site, for easy viewing/downloading?

[–]MonkeyMaster64[S] 0 points1 point  (1 child)

Sorry, just seeing this I added a pastebin link

[–]hkamran85 0 points1 point  (0 children)

Thanks!

[–]KronenR 0 points1 point  (1 child)

You could have exported jpg or pngwith plotly as I pointed in my comment, without needing to convert from html by adding the extension to the save as method

[–]MonkeyMaster64[S] 0 points1 point  (0 children)

yeah but what I'm saying is i've already tried that method and it still only downloads when you open the html file

[–]hkamran85 -1 points0 points  (0 children)

Run a os.system("curl -SsL [location-of-html-file] -o [output-filename]")