Package: mecsimcalc
Mecsimcalc v0.1.6 documentationThis 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.GitHub RepositoryPyPi PageGeneralinput_to_file[Source]input_to_file(input_file,metadata=False)Description:Converts a base64 encoded string into a file object and metadataArguments:ArgumentTypeDescriptioninput_filestrBase64 encoded string, prefixed with metadatametadatabool(optional)Flag to return metadata with the file. (Defaults to False)Raises:ExceptionDescriptionValueErrorIf the input string doesn't contain ';base64,' to separate metadata and file data.Returns:Return TypeDescriptionConditionio.BytesIOThe decoded file data (The thing you get when you open a file in Python)metadata is False(io.BytesIO, str)The decoded file data and its metadatametadata is TrueExample:>>>input_file=inputs['file']>>>file,metadata=input_to_file(input_file,metadata=True)>>>print(metadata)data:image/jpeg;base64,>>>type(file)<class'_io.BytesIO'>metadata_to_filetype[Source]metadata_to_filetype(metadata):Description:Extracts the file type from the metadataArguments:ArgumentTypeDescriptionmetadatastrThe metadata string in the form "Data:(MIME type);base64,"(returned frominput_to_file)Returns:Return TypeDescriptionstrThe file type (e.g. "jpeg")Example:>>>input_file=inputs['file']>>>file,metadata=input_to_file(input_file,metadata=True)>>>print(metadata)data:image/jpeg;base64,>>>download_file_type=metadata_to_filetype(metadata)>>>print(download_file_type)jpegTextstring_to_file[Source]string_to_file(textfilename="myfile",download_text="Download File",)Description:Generates a downloadable text file containing the given textArguments:ArgumentTypeDescriptiontextstrText to be downloadedfilenamestr(optional)Name of the download file. (Defaults to "myfile")download_textstr(optional)Text to be displayed as the download link. (Defaults to "Download File")Raises:ExceptionDescriptionTypeErrorIf the input text is not a string.Returns:Return TypeDescriptionstrHTML download linkExample:Python>>>download_link=string_to_file("Hello World!")>>>return{"download":download_link}Jinja2# outputs.downloadLink is the html download link generated by the function{{outputs.download}}Spreadsheetsfile_to_dataframe[Source]file_to_dataframe(file_data):Description:Converts a base64 encoded file data into a pandas DataFrameArguments:ArgumentTypeDescriptionfile_dataio.BytesIODecoded file data (e.g. frominput_to_file)Raises:ExceptionDescriptionpd.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 TypeDescriptionpd.DataFrameDataFrame created from file dataExample:>>>input_file=inputs['file']>>>decoded_file=input_to_file(input_file)>>>df=file_to_dataframe(decoded_file)>>>print(df)ABC0abc1definput_to_dataframe[Source]input_to_dataframe(file):Description:Converts a base64 encoded file data into a pandas DataFrameArguments:ArgumentTypeDescriptioninput_filestrBase64 encoded file dataget_file_typeboolIf True, the function also returns the file type (Defaults to False)Returns:Return TypeDescriptionConditionpd.DataFrameDataFrame created from file dataget_file_type is False(pd.DataFrame, str)Tuple containing the DataFrame and the file typeget_file_type is TrueExample:>>>input_file=inputs['file']>>>df,file_type=input_to_dataframe(input_file,get_file_type=True)>>>print(df)ABC0abc1def>>>print(file_type)csvprint_dataframe[Source]print_dataframe(df,download=False,download_text="Download Table",download_file_name="mytable",download_file_type="csv",):Description:Creates an HTML table and a download link for a given DataFrameArguments:ArgumentTypeDescriptiondfpd.DataFrameDataFrame to be converteddownloadbool(optional)If True, function returns a download link (Defaults to False)download_textstr(optional)Text to be displayed as the download link (Defaults to "Download Table")download_file_namestr(optional)Name of file when downloaded (Defaults to "mytable")download_file_typestr(optional)File type of downloaded file (Defaults to "csv")Returns:Return TypeDescriptionConditionstrHTML tabledownload is FalseTuple[str, str](HTML table, HTML download link)download is TrueExample:Python Code:>>>input_file=inputs['file']>>>df=input_to_dataframe(input_file)>>>table,download=print_dataframe(df,download=True,download_file_name="FunkyTable",download_text="Download My Funky Table HERE!",download_file_type="xlsx")>>>return{"table":table,"download":download,}Output using Jinja2 Template:# outputs.table is the HTML tableDisplayingTable{{outputs.table}}# outputs.download is the download linkDownloadingTable{{outputs.download}}Tablestable_to_dataframe[Source]table_to_dataframe(column_headers,rows)->pd.DataFrame:Description:Create a DataFrame from given rows and column headersArguments:ArgumentTypeDescriptioncolumn_headersList[str]List of column headersrowsList[List[str]]List of rows to be converted into a DataFrame. Each column is a list of stringsReturns:Return TypeDescriptionpd.DataFrameDataFrame created from headers and rowsExample:>>>column_headers=["A","B","C"]>>>rows=[["a","b","c"],["d","e","f"]]>>>df=table_to_dataframe(column_headers,rows)>>>print(df)ABC0abc1defprint_table[Source]print_table(column_headers,rows):Description:Creates an HTML table from given rows and column headersArguments:ArgumentTypeDescriptioncolumn_headersList[str]List of column headersrowsList[List[str]]List of rows to be converted into a table. Each column is a list of stringsindexbool(optional)Whether to use the first column as the DataFrame's index. (Defaults to True)Returns:Return TypeDescriptionstrHTML table created from rows and headersExample:Python Code:>>>column_headers=["A","B","C"]>>>rows=[["a","b","c"],["d","e","f"]]>>>table=print_table(column_headers,rows)>>>return{"table":table,}Output using Jinja2 Template:# outputs.table is the HTML tableDisplayingTable{{outputs.table}}Imagesfile_to_PIL[Source]file_to_PIL(file):Description:Transforms a file into a Pillow Image objectArguments:ArgumentTypeDescriptionfilestrDecoded file data (returned from input_to_file)Raises:Exception TypeDescriptionValueErrorIf the file does not contain image dataReturns:Return TypeDescriptionImagePillow Image objectExample:Python Code:>>>input_file=inputs['file']>>>decoded_file=input_to_file(input_file)>>>image=file_to_PIL(decoded_file)>>>return{"image":image,}Output using Jinja2 Template:# outputs.image is the Pillow Image objectDisplayingImage{{outputs.image}}input_to_PIL[Source]input_to_PIL(input_file,get_file_type=False):Description:Converts a base64 encoded file data into a pillow imageArguments:ArgumentTypeDescriptioninput_filestrBase64 encoded file dataget_file_typeboolIf True, the function also returns the file type (Defaults to False)Returns:Return TypeDescriptionConditionPIL.Image.ImagePillow Image objectget_file_type is FalseTuple[PIL.Image.Image, str](pillow image, metadata)get_file_type is TrueExample:>>>input_file=inputs['file']>>>image,file_type=input_to_PIL(input_file,get_file_type=True)>>>print(file_type)jpeg>>>type(image)<class'PIL.JpegImagePlugin.JpegImageFile'>print_image[Source]print_image(image,width=200,height=200,original_size=False,download=False,download_text="Download Image",download_file_name="myimg",download_file_type="png",):Description:Transforms a Pillow image into an HTML image, with an optional download linkArguments:ArgumentTypeDescriptionimagePIL.Image.ImagePillow imagewidthint(optional)Output width of the image in pixels (Defaults to 200)heightint(optional)Output height of the image in pixels (Defaults to 200)original_sizebool(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)download_textstr(optional)The text to be displayed on the download link (Defaults to "Download Image")download_file_namestr(optional)The name of the image file when downloaded (Defaults to "myimg")download_file_typestr(optional)The file type of the image when downloaded (Defaults to "png")Returns:Return TypeDescriptionConditionstrHTML imagedownload is FalseTuple[str, str](HTML image, download link)download is TrueExample:Python Code:>>>input_file=inputs['file']>>>image,metadata=input_to_PIL(input_file)>>>html_image,download=print_image(image,original_size=True,download=True,download_text="Download Image Here",download_file_name="myimage",download_file_type="jpeg")>>>return{"image":html_image,"download":download,}Output using Jinja2 Template:# outputs.image is the HTML imageDisplayingImage{{outputs.image}}# outputs.download is the download linkDownloadingImage{{outputs.download}}Plotsprint_plot[Source]print_plot(plot_obj,width=500,dpi=100,download=False,download_text="Download Plot",download_file_name="myplot",)Description:Converts a matplotlib.pyplot.axis or matplotlib.figure into an HTML image tag and optionally provides a download link for the imageArguments:ArgumentTypeDescriptionplot_objaxes or figureMatplotlib figurewidthint(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)download_textstr(optional)The text to be displayed on the download link (Defaults to "Download Plot")download_file_namestr(optional)The name of the image file when downloaded (Defaults to "myplot")Returns:Return TypeDescriptionConditionstrHTML imagedownload is FalseTuple[str, str](HTML image, HTML download link)download is TrueExample:Python Code:>>>importmatplotlib.pyplotasplt>>>importnumpyasnp>>>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_plot(fig,width=500,dpi=100,download=True,download_text="Download Sin Function Plot",download_file_name="sin(x)")>>>return{"image":image,"download":download,}Output using Jinja2 Template:# outputs.image is the HTML imageDisplayingImage{{outputs.image}}# outputs.download is the download linkDownloadingImage{{outputs.download}}