Skip to main content

Mecsimcalc 0.0.4 documentation

This library is designed to provide a set of functions for handling and converting various types of data, such as base64 encoded data, Pandas DataFrames, and Pillow images.

General

decode_file_data

[Source]
def decode_file_data(encoded_data, metadata = False)

Description:

Converts a base64 encoded file into a file object and metadata

Arguments:

ArgumentTypeDescription
encoded_datastrBase64 encoded file data
metadatabool (optional)If True, function returns file and metadata (Defaults to False)

Returns:

Return TypeDescriptionCondition
io.BytesIOThe decoded file datametadata is False
(io.BytesIO, str)The decoded file and metadatametadata is True

Example:

>>> inputFile = inputs['file']
>>> file, metadata = decode_file_data(inputFile, metadata = True)
>>> print(metadata)
data:image/jpeg;base64,
>>> type(file)
<class '_io.BytesIO'>

download_text

[Source]
def download_text(
text: str,
filename: str = "myfile",
extension: str = ".txt",
download_text: str = "Download File",
) -> str:

Description:

Generates a downloadable text file containing the given text

Arguments:

ArgumentTypeDescription
textstrThe text to be downloaded
filenamestr (optional)The name of the file to be downloaded (Defaults to "myfile")
extensionstr (optional)The file extension of the file to be downloaded (Defaults to ".txt")
download_textstr (optional)The text to be displayed on the download button (Defaults to "Download File")

Returns:

Return TypeDescription
strThe HTML code for the download button

Example:

Python

>>> downloadLink = download_text("Hello World!")
>>> return {"downloadLink": downloadLink}

Jinja2

# outputs.downloadLink is the html download link generated by the function
{{ outputs.downloadLink }}

Tables/DataFrames

file_to_dataframe

[Source]
def file_to_dataframe(file_data):

Description:

Converts a file object into a pandas DataFrame

Arguments:

ArgumentTypeDescription
file_dataio.BytesIODecoded file data (e.g. from decode_file_data)

Raises:

ExceptionDescription
pd.errors.ParserErrorIf the file data cannot be converted to a DataFrame (i.e. file is not an Excel or CSV file or is corrupted)

Returns:

Return TypeDescription
pd.DataFrameDataFrame created from file data

Example:

>>> inputFile = inputs['file']
>>> file = decode_file_data(inputFile)
>>> df = file_to_dataframe(file)
>>> print(df)
A B C
0 a b c
1 d e f

input_to_dataframe

[Source]
def input_to_dataframe(file):

Description:

Converts a base64 encoded file data into a pandas DataFrame

Arguments:

ArgumentTypeDescription
filestrBase64 encoded file data

Returns:

Return TypeDescription
pd.DataFrameDataFrame created from file data

Example:

>>> inputFile = inputs['file']
>>> df = input_to_dataframe(inputFile)
>>> print(df)
A B C
0 a b c
1 d e f

print_dataframe

[Source]
def print_dataframe(
df,
download = False,
DownloadText = "Download Table",
DownloadFileName = "mytable",
FileType = "csv",
):

Description:

Creates an HTML table and a download link for a given DataFrame

Arguments:

ArgumentTypeDescription
dfpd.DataFrameDataFrame to be converted
downloadbool (optional)If True, function returns a download link (Defaults to False)
DownloadTextstr (optional)Text to be displayed as the download link (Defaults to "Download File")
DownloadFileNamestr (optional)Name of file when downloaded (Defaults to "myfile")
FileTypestr (optional)File type of download (Defaults to "csv")

Returns:

Return TypeDescriptionCondition
strHTML tabledownload is False
Tuple[str, str](HTML table, download link)download is True

Example:

Python Code:

>>> inputFile = inputs['file']
>>> df = input_to_dataframe(inputFile)
>>> table, download = print_dataframe(df, download = True, DownloadFileName = "FunkyTable", DownloadText = "Download My Funky Table HERE!", FileType = "xlsx")
>>> return {
"table":table,
"download":download,
}

Output using Jinja2 Template:

# outputs.table is the HTML table
Displaying Table
{{ outputs.table }}

# outputs.download is the download link
Downloading Table
{{ outputs.download }}

table_to_dataframe

[Source]
def table_to_dataframe(columns: List[List[str]], column_headers: List[str]) -> pd.DataFrame:

Description:

Creates a DataFrame from given columns and headers

Arguments:

ArgumentTypeDescription
columnsList[List[str]]List of columns to be converted into a DataFrame. Each column is a list of strings
column_headersList[str]List of column headers

Returns:

Return TypeDescription
pd.DataFrameDataFrame created from columns and headers

Example:

>>> columns = [["a", "b", "c"], ["d", "e", "f"]]
>>> column_headers = ["A", "B", "C"]
>>> df = table_to_dataframe(columns, column_headers)
>>> print(df)
A B C
0 a b c
1 d e f

print_table

[Source]
print_table(columns: List[List[str]], column_headers: List[str]):

Description:

Creates an HTML table from given columns and headers

Arguments:

ArgumentTypeDescription
columnsList[List[str]]List of columns to be converted into a table. Each column is a list of strings
column_headersList[str]List of column headers

Returns:

Return TypeDescription
strHTML table created from columns and headers

Example:

Python Code:

>>> columns = [["a", "b", "c"], ["d", "e", "f"]]
>>> column_headers = ["A", "B", "C"]
>>> table = print_table(columns, column_headers)
>>> return {
"table":table,
}

Output using Jinja2 Template:

# outputs.table is the HTML table
Displaying Table
{{ outputs.table }}

Images

input_to_PIL

[Source]
def input_to_PIL(file):

Description:

Converts a base64 encoded file data into a pillow image

Arguments:

ArgumentTypeDescription
filestrBase64 encoded file data

Returns:

Return TypeDescription
Tuple[PIL.Image.Image, str](pillow image, metadata)

Example:

>>> inputFile = inputs['file']
>>> img, metadata = input_to_PIL(inputFile)
>>> print(metadata)
data:image/jpeg;base64,
>>> type(file)
<class 'PIL.JpegImagePlugin.JpegImageFile'>

print_img

[Source]
def print_img(
img,
metadata,
WIDTH = 200,
HEIGHT = 200,
OriginalSize = False,
download = False,
DownloadText = "Download Image",
ImageName= "myimg",
):

Description:

Converts a pillow image into an HTML image and a download link

Arguments:

ArgumentTypeDescription
imgPIL.Image.ImagePillow image
metadatastrImage metadata
WIDTHint (optional)Output width of the image in pixels (Defaults to 200)
HEIGHTint (optional)Output height of the image in pixels (Defaults to 200)
OriginalSizebool (optional)If True, the HTML image will be displayed in its original size (Defaults to False)
downloadbool (optional)If True, function returns a download link (Defaults to False)
DownloadTextstr (optional)The text to be displayed on the download link (Defaults to "Download Image")
ImageNamestr (optional)The name of the image file when downloaded (Defaults to "myimg")

Returns:

Return TypeDescriptionCondition
strHTML imagedownload is False
Tuple[str, str](HTML image, download link)download is True

Example:

Python Code:

>>> inputFile = inputs['file']
>>> img, metadata = input_to_PIL(inputFile)
>>> image, download = print_img(img, metadata, OriginalSize = True, download = True, DownloadText = "Download Image Here", ImageName = "myimage")
>>> return {
"image":image,
"download":download,
}

Output using Jinja2 Template:

# outputs.image is the HTML image
Displaying Image
{{ outputs.image }}

# outputs.download is the download link
Downloading Image
{{ outputs.download }}

print_plt

[Source]
def print_plt(
plt:,
width = 500,
dpi= 100,
download= False,
DownloadText = "Download Plot",
DownloadFileName = "myplot",
)

Description:

Converts a matplotlib.pyplot or matplotlib.figure into an HTML image tag and optionally provides a download link for the image

Arguments:

ArgumentTypeDescription
pltplt or figureMatplotlib figure
widthint (optional)Output width of the image in pixels (Defaults to 500)
dpiint (optional)Output dpi of the image in pixels (Defaults to 100)
downloadbool (optional)If True, function returns a download link (Defaults to False)
DownloadTextstr (optional)The text to be displayed on the download link (Defaults to "Download Plot")
DownloadFileNamestr (optional)The name of the image file when downloaded (Defaults to "myplot")

Returns:

Return TypeDescriptionCondition
strHTML imagedownload is False
Tuple[str, str](HTML image, download link)download is True

Example:

Python Code:

>>> import matplotlib.pyplot as plt
>>> import numpy as np
>>> x = np.linspace(0, 2 * np.pi, 400)
>>> y = np.sin(x)
>>> fig, ax = plt.subplots()
>>> ax.plot(x, y)
>>> ax.set_title('A single plot')
>>> image, download = print_plt(fig, width = 500, dpi = 100, download = True, DownloadText = "Download Sin Function Plot", DownloadFileName = "sin(x)")
>>> return {
"image":image,
"download":download,
}

Output using Jinja2 Template:

# outputs.image is the HTML image
Displaying Image
{{ outputs.image }}

# outputs.download is the download link
Downloading Image
{{ outputs.download }}