diff --git a/mecsimcalc/MecSimCalc.py b/mecsimcalc/MecSimCalc.py index ef6c9b0..8930c4d 100644 --- a/mecsimcalc/MecSimCalc.py +++ b/mecsimcalc/MecSimCalc.py @@ -7,17 +7,17 @@ def decode_file_data( encoded_data, metadata: bool = False -) -> Union[io.BytesIO, Tuple[str, io.BytesIO]]: +) -> Union[io.BytesIO, Tuple[io.BytesIO, str]]: """ - Converts a base64 encoded file data into a file object. + Converts a base64 encoded file data into a file object and metadata Args: encoded_data (str): Base64 encoded file data - metadata (bool, optional): If True, returns a tuple of (fileData, metadata). Defaults to False. + metadata (bool, optional): If True, function returns file and metadata (Defaults to False) Returns: - io.BytesIO: fileData - (io.BytesIO, str): fileData, metadata + io.BytesIO: The decoded file data (if metadata is False) + (io.BytesIO, str): The decoded file and metadata (if metadata is True) """ @@ -31,16 +31,16 @@ def decode_file_data( def file_data_to_dataframe(file_data) -> pd.DataFrame: """ - Converts a file object into a pandas dataframe + Converts a file object into a pandas DataFrame Args: - file_data (io.BytesIO): Decoded file data from decode_file_data() + file_data (io.BytesIO): Decoded file data (e.g. from decode_file_data) Raises: - pd.errors.ParserError: If the file is not a CSV or Excel file (or if the file is corrupt) + pd.errors.ParserError: If the file data cannot be converted to a DataFrame (i.e. file is not an Excel or CSV file or is corrupted) Returns: - pd.DataFrame: a dataframe of the file data + pd.DataFrame: DataFrame created from file data """ try: @@ -54,13 +54,13 @@ def file_data_to_dataframe(file_data) -> pd.DataFrame: def input_to_dataframe(file) -> pd.DataFrame: """ - Converts a base64 encoded file data into a pandas dataframe + Converts a base64 encoded file data into a pandas DataFrame Args: - file (str): base64 encoded file data + file (str): Base64 encoded file data Returns: - pd.DataFrame: a dataframe of the file data + pd.DataFrame: DataFrame created from file data """ fileData = decode_file_data(file) @@ -70,13 +70,14 @@ def input_to_dataframe(file) -> pd.DataFrame: def dataframe_to_output( df, DownloadText: str = "Download File", DownloadFileName: str = "myfile" ) -> Tuple[str, str]: + # TODO: Add selection of CSV or Excel for download """ - Converts a pandas dataframe into an HTML table and a download link + Creates an HTML table and a download link for a given DataFrame Args: - df (df): pandas dataframe - DownloadText (str, optional): download link text. Defaults to 'Download File'. - DownloadFileName (str, optional): download file name. Defaults to 'myfile.csv'. + df (pandas.df): DataFrame to be converted + DownloadText (str, optional): Text to be displayed as the download link (Defaults to "Download File") + DownloadFileName (str, optional): Name of file when downloaded (Defaults to "myfile") Returns: Tuple[str, str]: HTML table, download link @@ -93,9 +94,28 @@ def dataframe_to_output( ) +def input_to_PIL(file) -> Tuple[Image.Image, str]: + """ + converts a Base64 encoded file data into a pillow image + + Args: + file (str): Base64 encoded file data + + Returns: + Tuple[Image.Image, str]: pillow image, metadata + """ + + [fileData, metaData] = decode_file_data(file, metadata=True) + + # Convert the file data into a Pillow's Image + img = Image.open(fileData) + + return img, metaData + + def print_img( - img, - metadata, + img: Image.Image, + metadata: str, WIDTH: int = 200, HEIGHT: int = 200, OriginalSize: bool = False, @@ -106,13 +126,13 @@ def print_img( Converts a pillow image into an HTML image and a download link Args: - img (PIL.Image): pillow image - metadata (str): image metadata - WIDTH (int, optional): width of the image. Defaults to 200. - HEIGHT (int, optional): height of the image. Defaults to 200. - OriginalSize (bool, optional): If True, the image will not be resized. Defaults to False. - DownloadText (str, optional): download link text. Defaults to 'Download Image'. - ImageName (str, optional): download file name. Defaults to 'myimg'. + img (PIL.Image.Image): Pillow image + metadata (str): Image metadata + WIDTH (int, optional): Output width of the image in pixels (Defaults to 200) + HEIGHT (int, optional): Output height of the image in pixels (Defaults to 200) + OriginalSize (bool, optional): If True, the HTML image will be displayed in its original size (Defaults to False) + DownloadText (str, optional): The text to be displayed on the download link (Defaults to "Download Image") + ImageName (str, optional): download file name (Defaults to 'myimg') Returns: Tuple[str, str]: HTML image, download link @@ -130,9 +150,11 @@ def print_img( # Get displayable data (Custom Resolution) displayBuffer = io.BytesIO() - displayImg.save( - displayBuffer, format=img.format - ) # It seems tempting to use displayImg.format here, but it doesn't work for some reason + + # It seems tempting to use displayImg.format here, but it doesn't work for some reason + displayImg.save(displayBuffer, format=img.format) + + # Get the encoded data encoded_display_data = ( metadata + base64.b64encode(displayBuffer.getvalue()).decode() ) @@ -144,22 +166,3 @@ def print_img( downloadLink = f"{DownloadText}" return image, downloadLink - - -def input_to_PIL(file) -> Tuple[Image.Image, str]: - """ - converts a base64 encoded file data into a pillow image - - Args: - file (str): base64 encoded file data - - Returns: - Tuple[Image.Image, str]: pillow image, image metadata - """ - - [fileData, metaData] = decode_file_data(file, metadata=True) - - # Convert the file data into a Pillow's Image - img = Image.open(fileData) - - return img, metaData diff --git a/setup.py b/setup.py index 1997dba..afa1d09 100644 --- a/setup.py +++ b/setup.py @@ -7,7 +7,7 @@ with codecs.open(os.path.join(here, "README.md"), encoding="utf-8") as fh: long_description = "\n" + fh.read() -VERSION = "0.0.2" +VERSION = "0.0.3" DESCRIPTION = "Useful functions for MecSimCalc.com" LONG_DESCRIPTION = "Useful functions for MecSimCalc.com" @@ -26,8 +26,6 @@ classifiers=[ "Development Status :: 3 - Alpha", "Intended Audience :: Developers", - "Programming Language :: Python :: 3.6", - "Programming Language :: Python :: 3.7", "Programming Language :: Python :: 3.8", "Programming Language :: Python :: 3.9", "Programming Language :: Python :: 3.10",