Skip to main content

A library to convert polars/ pandas dataframes to image.

Project description

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"
                            }
    cell_highlight (optional bool): Whether to highlight cells based on conditions.          Default is False.
    highlight_conditions (optional): A dictionary for conditional formatting. 
                                    For example, look below.
    content_based_color (optional bool): Whether to color cells based on content. Default is False.
    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_color= True,
        content_based_style= {
                        "String": {
                            "bg-color": "LightCoral", 
                            "color": "DodgerBlue", 
                            "font-weight": "bolder"
                            }
                        },
        cell_highlight= True,
        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

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

dfimg-0.1.0.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

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

dfimg-0.1.0-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: dfimg-0.1.0.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.3

File hashes

Hashes for dfimg-0.1.0.tar.gz
Algorithm Hash digest
SHA256 7ecb043350a5585a5f96cdd3bbda7f8af268167ff86ae6ac13f09037d60675a5
MD5 075d40185cbe4a6e070f0131a5a22510
BLAKE2b-256 cb005a25b2c8b9a7c75708e2f0b9c0f9837b6120b5a7f395407b811c88ec5655

See more details on using hashes here.

File details

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

File metadata

  • Download URL: dfimg-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 7.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.3

File hashes

Hashes for dfimg-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 558cd8b322c43d0ace023326a0adf45624bd0392ccb45a06ce4fc9ff8e21ee6b
MD5 bcce3189373541adc0c47665171a81eb
BLAKE2b-256 7998db893857678efeb501ed35593a5d3ba53c12d2e31b9612d645b7b59d7faf

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