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_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

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.3.tar.gz (6.2 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.3-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: dfimg-0.1.3.tar.gz
  • Upload date:
  • Size: 6.2 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.3.tar.gz
Algorithm Hash digest
SHA256 2bc4b6e6abc7df0e7ab30bfa0742da598e7bf1868a1de2c9dce2f95906aec02a
MD5 dada94d903f179da19b75a33c0528fa5
BLAKE2b-256 0590488d5e1b45b1009d813fb800899d900a23447b8f9e247fe10aba9023adb0

See more details on using hashes here.

File details

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

File metadata

  • Download URL: dfimg-0.1.3-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.3-py3-none-any.whl
Algorithm Hash digest
SHA256 5f4fdb0985087d38a8292e23220d93232eb9b8a308474dcca77632af439bf966
MD5 c46040206ef18473fbb68ae69f645b51
BLAKE2b-256 3f99617b3bc2d2872e529e044f05a8f2e32cb755077fe2d9a97d36a395c0d5b3

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