Skip to main content

Embed pandas DataFrames as images in pdf and markdown files when converting from Jupyter Notebooks

Project description

dataframe_image

PyPI - License

A package to convert Jupyter Notebooks to PDF and/or Markdown embedding pandas DataFrames as images.

Overview

When converting Jupyter Notebooks to pdf using nbconvert, pandas DataFrames appear as either raw text or as simple LaTeX tables. The left side of the image below shows this representation.

png

This package was first created to embed DataFrames into pdf and markdown documents as images so that they appear exactly as they do in Jupyter Notebooks, as seen from the right side of the image above. It has since added much more functionality.

Usage

Upon installation, the option DataFrame as Image (PDF or Markdown) will appear in the menu File -> Download as. Clicking this option will open up a new browser tab with a short form to be completed.

png

Exporting individual DataFrames

dataframe_image has the ability to export both normal and styled DataFrames as images from within a Python script. Pass your normal or styled DataFrame to the export function along with a file location to save it as an image.

>>> import dataframe_image as dfi
>>> dfi.export(df_styled, 'df_styled.png')

You may also export directly from the DataFrame or styled DataFrame using the dfi.export and export_png methods, respectively.

>>> df.dfi.export('df.png')
>>> df_styled.export_png('df_styled.png)

Here, an example of how exporting a DataFrame would look like in a notebook.

png

Installation

pip install dataframe_image

PDF Conversion - LaTeX vs Chrome Browser

By default, conversion to pdf happens via LaTeX, which you must have pre-installed on your machine. If you do not have the correct LaTeX installation, you'll need to select the Chrome Browser option to make the conversion.

Conversion via Chrome browser is much quicker and will look very different than the LaTeX rendition. The chrome browser version will look nearly the same as it does in your browser, while the LaTeX version looking more like a book/article.

Consult nbconvert's documentation to learn how to get latex installed correctly on your machine.

More features

Below, is a description of other features from dataframe_image:

  • Embeds all images from markdown cells (inline, reference, attachments, and <img> tags) into the pdf
  • Saves the new documents anywhere in your filesystem and correctly link the resources
  • Converts gifs to single-frame png files allowing them to be embedded into the pdf

As a Python Library

dataframe_image can also be used outside of the notebook as a normal Python library. In a separate Python script, import the dataframe_image package and pass the file name of your notebook to the convert function.

>>> import dataframe_image as dfi
>>> dfi.convert('path/to/your_notebook.ipynb',
                to='pdf',
                use='latex',
                center_df=True,
                max_rows=30,
                max_cols=10,
                execute=False,
                save_notebook=False,
                limit=None,
                document_name=None,
                table_conversion='chrome'
                chrome_path=None,
                latex_command=None,
                output_dir=None,
                )

By default, the new file(s) will be saved in the same directory where the notebook resides. Do not run this command within the same notebook that is being converted.

From the Command Line

The command line tool dataframe_image will be available upon installation with the same options as the convert function from above.

dataframe_image --to=pdf "my notebook with dataframes.ipynb"

Finding Google Chrome

You must have Google Chrome (or Brave) installed in order for dataframe_image to work. The path to Chrome should automatically be found. If Chrome is not in a standard location, set it with the chrome_path parameter.

Using matplotlib instead of Chrome

If you do not have Chrome installed or cannot get it to work properly, you can alternatively use matplotlib to convert the DataFrames to images. Select this option by setting the table_conversion parameter to 'matplotlib'.

Publish to Medium

Closely related to this package is jupyter_to_medium, which publishes your notebooks directly and quickly as Medium blog posts.

Dependencies

You must have the following Python libraries installed:

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dataframe_image-0.1.0.tar.gz (27.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dataframe_image-0.1.0-py3-none-any.whl (31.4 kB view details)

Uploaded Python 3

File details

Details for the file dataframe_image-0.1.0.tar.gz.

File metadata

  • Download URL: dataframe_image-0.1.0.tar.gz
  • Upload date:
  • Size: 27.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/47.1.1.post20200529 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.8.3

File hashes

Hashes for dataframe_image-0.1.0.tar.gz
Algorithm Hash digest
SHA256 704ac94896a33bf537da5e851b3a0c2cae887612a9f5286f4231a4ed7e96b77d
MD5 9db8fbfc323e482c18880e1af31f918c
BLAKE2b-256 84922728ec5e630af8989325b68e7195c77879195f5162671acbc2b8543dd2e0

See more details on using hashes here.

File details

Details for the file dataframe_image-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: dataframe_image-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 31.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/47.1.1.post20200529 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.8.3

File hashes

Hashes for dataframe_image-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2c97a0a0f66712adb985037474f6329fa467e406f411f8c545fd2b5e6c9e38b3
MD5 b0d464770a6f632939035396d536f8d3
BLAKE2b-256 5a5b70ac946affd06c1c811c80db51c0bef4a927c8fb666410da7d881cbec02a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page