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"
                            }
    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_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.1.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.1-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: dfimg-0.1.1.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.1.tar.gz
Algorithm Hash digest
SHA256 82c66888c76c0b3326e9a33dbaffb6749789bcd58ee02a6a24b342f1ad9553b2
MD5 4efa731e27eafee976e581cab5ef170d
BLAKE2b-256 b38203544e14e293ce777f99dd81a13903f7e685000831bcf67d1c5075bb9e54

See more details on using hashes here.

File details

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

File metadata

  • Download URL: dfimg-0.1.1-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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 dfd7244355191aeb5eca6256e651a647d032de81f91c6fd56b6d770c49840f5a
MD5 52768b2dbdda69d26671f0b7c6ae3b7a
BLAKE2b-256 a4b72a81af0babbfd3a7ae9b46c94e7a7dec31d3335112949d3984e6bc17b468

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