Grabbing snapshots of cell output - identifying a cell output dom element

One of the hobby projects I’m dabbling with creates some heavily styled pandas tables than I want to be able to automatically post to social media as images.

At the moment I have a clunky workflow that grabs the table html, then gets the screenshot using selenium.

It strikes me that something like https://github.com/niklasvh/html2canvas, which can generate an image from a DOM element, would be more generally useful, if I could easily identify the DOM element associated with a particular cell output.

So: any ideas how I might do this?

Emit a HTML div with an id and your content, then in the next cell use it.

Ah, that makes sense…

In general though, if I want to capture and arbitrary cell output, is there a way to introspect on cell output ids, eg of a previous cell?

There is %%capture.

Selection_651

I donlt have a problem capturing the html - pandas will let me export that; is the output rendered and then captured as png that I’m interested in.

Current recipe is of the form:

def getTablePNG(tablehtml, basepath='.', path='testpng', fnstub='testhtml', scale_factor=2):
  ''' Save HTML table as file. '''
  if not os.path.exists(path):
      os.makedirs('{}/{}'.format(basepath, path))
  fn='{cwd}/{basepath}/{path}/{fn}.html'.format(cwd=os.getcwd(), basepath=basepath, path=path,fn=fnstub)
  tmpurl='file://{fn}'.format(fn=fn)
  with open(fn, 'w') as out:
      out.write(tablehtml)
  return getTableImage(tmpurl, fnstub, basepath, path, scale_factor=scale_factor)

img = getTablePNG( getHTMLfromDF( df ) )
sociallyAmplify(img, socialSettings)

and the getTableImage() function uses selenium to load the HTML in a browser and then get a screengrab using Selenium, though I realised I may be able to use Firefox more directly, though I haven’t checked this to see how it handles renderings that require x and y scrolling on a default browser window size.

What’d I’d like is a recipe of the form:

previewDisplay( df )
sociallyAmplify(thisCellOutput, socialSettings)

or:

 previewDisplay( df )

in one cell, then:

sociallyAmplify(previousCellOutput, socialSettings)

in the next.

And yes, as I type this, I realise that imposes some use-issues… Like this is an interactive way of working. The Selenium route copes with headless execution of the notebook…

1 Like