Skip to main content

dfimg

Convert polars / pandas dataframe to image/ binary image.

Installation ::

For intallation use pip :

pip install dfimg

Functions ::

dfimg provides the following functions:

df_to_html_img: This function performs the main task of converting a Polars or Pandas DataFrame to an HTML or PIL image.

Parameters:
    dataframe (required): The Polars or Pandas DataFrame to be converted.
    title (optional): Title string to display on top of the image. Default is an empty string.
    image_title_font (optional): Font to be used for the title. Default is None.
    image_table_font (optional): Font for the table content. Default is None.
    image_title_font_size (optional): Font size for the title. Default is 16.
    image_table_font_size (optional): Font size for the table content. Default is 12.
    image_cells_border_color (optional): Color for cell borders. Default is None.
    style (optional): Styles for the html table. Default             
                        {
                            "bg-color": "white", 
                            "color": "black", 
                            "font-weight": "normal"
                        }
    tbl_style (optional): Table-specific styles, default 
                        {
                            "bg-color": "white", 
                            "color": "black", 
                            "font-weight": "normal"
                        }
    header_style (optional): Style for the table header, Default 
                            {
                                "bg-color": "DarkViolet", 
                                "color": "white", 
                                "font-weight": "bold"
                            }
    highlight_conditions (optional): A dictionary for conditional formatting. 
                                    For example, look below.
                                    
    column_style (optional): Dictionary of styles for columns. This will override content_based_style if both are provided.

    content_based_style (optional): Dictionary of styles based on content. The style will be applied for all column of same datatype 

    Supported keys are "String", "Int", "Float", "Boolean", "Datetime", "Date", "Time" and "Others". For example look below
    table_formatting (optional): Format of the table in the image. Possible values are:
        "ASCII_FULL"
        "ASCII_FULL_CONDENSED"
        "ASCII_NO_BORDERS"
        "ASCII_BORDERS_ONLY"
        "ASCII_BORDERS_ONLY_CONDENSED"
        "ASCII_HORIZONTAL_ONLY"
        "ASCII_MARKDOWN"
        "MARKDOWN"
        "NOTHING" Default is "ASCII_MARKDOWN".

    cell_padding (optional): Padding inside image cells. Default is 10.
    min_cell_width (optional): Minimum width for each image cell. Default is 100.
    float_precision (optional): Decimal precision for floating-point numbers. Default is 2.
    thousands_separator (optional): Character to use as a thousands separator. Default is _.
    return_type (optional): Type of output. Options are:

        "html" -> returns html string of the dataframe
        "pil_image" -> return PIL Image.
        Default is "pil_image".

image_to_bin: This function convert PIL Image to binary.

Parameters:
    img (required): A PIL Image object.
    format (optional): The image format of the resultant binary(e.g., "PNG", "JPEG", etc.). Default is "PNG".

send_img_to_telegram: This is a addon function for sending PIL Image to Telegram chat via the Telegram Bot API.

Parameters:
    img (required): A PIL Image object that you want to send to Telegram.
    chat_id (required): The chat ID of the recipient on Telegram.
    bot_token (required): Your Telegram bot token.
    img_name (optional): The name of the image file (without extension). Default is "sample".
    format (optional): The format of the image to send (e.g., "PNG", "JPEG"). Default is "PNG".

Example ::

import polars as pl
from datetime import datetime, date, time
from dfimg import df_to_html_img, send_img_to_telegram, image_to_bin

data = {
    "Title": ["x", "y", "z"],
    "Datetime": [datetime(2023, 1, 1), datetime(2023, 2, 1), datetime(2023, 3, 1)],
    "Date": [date(2023, 1, 1), date(2023, 2, 1), date(2023, 3, 1)],    
    "Time": [time(12, 0, 0), time(12, 0, 0), time(12, 0, 0)],
    "Int_col": [10, 20, 30],
    "Str_col": ["str1", "str2", "str3"],
    "Float_col": [1.5, 2.5, 3.5],
    "Bool_col": [True, False, True]
}

img = df_to_html_img(
        pl.DataFrame(data),
        title= "TEST",
        content_based_style= {
                        "String": {
                            "bg-color": "LightCoral", 
                            "color": "DodgerBlue", 
                            "font-weight": "bolder"
                            }
                        },
        highlight_conditions= {
                        "Int_col": {
                                "condition": ">= 20", 
                                "bg-color": "yellow", 
                                "color": "green"
                                },
                        "Str_col": {
                                "condition": ".__contains__('str2')", 
                                "bg-color": "yellow", 
                                "color": "green"
                                },
                        "Bool_col": {
                                "condition": True, 
                                "bg-color": "yellow", 
                                "color": "green"
                                }
                            }
    )

# If your want to save the image locally:
img.save("image.png")

#for converting PIL Image to binary.

img_binary = image_to_bin(img)

#For sending image to telegram 

send_img_to_telegram(
                img= img, 
                chat_id= "xxxxxxxxx", 
                bot_token= "xxxxxxx:xxxxxxxxx"
                )

Example Image ::

Example

Release files for dfimg 0.1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for dfimg 0.1.3
File Size Uploaded
dfimg-0.1.3.tar.gz 6.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for dfimg 0.1.3
File Interpreter ABI Platform
dfimg-0.1.3-py3-none-any.whl Python 3 none any Details

Total release size: 13.3 kB

Release files / dfimg-0.1.3.tar.gz

Download URL dfimg-0.1.3.tar.gz
Size 6.2 kB
Tags Source
SHA-256 checksum
How to use checksums
2bc4b6e6abc7df0e7ab30bfa0742da598e7bf1868a1de2c9dce2f95906aec02a
BLAKE2b-256 checksum
How to use checksums
0590488d5e1b45b1009d813fb800899d900a23447b8f9e247fe10aba9023adb0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.12.3

Release files / dfimg-0.1.3-py3-none-any.whl

Download URL dfimg-0.1.3-py3-none-any.whl
Size 7.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5f4fdb0985087d38a8292e23220d93232eb9b8a308474dcca77632af439bf966
BLAKE2b-256 checksum
How to use checksums
3f99617b3bc2d2872e529e044f05a8f2e32cb755077fe2d9a97d36a395c0d5b3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.12.3

Release history Release notifications | RSS feed

This release

0.1.3 This release

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page