Skip to content

Commit

Permalink
updated comments to match documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
PointlessUser committed May 9, 2023
1 parent 45b665d commit abb40de
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 50 deletions.
97 changes: 50 additions & 47 deletions mecsimcalc/MecSimCalc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
"""

Expand All @@ -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:
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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,
Expand All @@ -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
Expand All @@ -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()
)
Expand All @@ -144,22 +166,3 @@ def print_img(
downloadLink = f"<a href='{encoded_data}' download='{ImageName}.{img.format}'>{DownloadText}</a>"

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
4 changes: 1 addition & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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",
Expand Down

0 comments on commit abb40de

Please sign in to comment.