Select Version: Latest Release (v0.2.1) v0.1.9 v0.1.6 v0.1.5 v0.1.4 v0.1.3 v0.1.2 v0.0.5 v0.0.4
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
[Source]
input_to_file ( input_file , metadata = False )
Description:
Converts a base64 encoded string into a file object and metadata
Arguments:
Argument Type Description input_filestr Base64 encoded string, prefixed with metadata metadatabool (optional)Flag to return metadata with the file. (Defaults to False)
Raises:
Exception Description ValueErrorIf the input string doesn't contain ';base64,' to separate metadata and file data.
Returns:
Return Type Description Condition io.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 metadata metadata is True
Example:
import io import mecsimcalc as msc def main ( inputs ) : input_file = inputs [ 'file' ] file , metadata = msc . input_to_file ( input_file , metadata = True ) return { "file_type" : type ( file ) . __name__ , "metadata" : metadata }
[Source]
metadata_to_filetype ( metadata ) :
Description:
Extracts the file type from the metadata
Arguments:
Argument Type Description metadatastr The metadata string in the form "Data:(MIME type);base64,"(returned from input_to_file )
Returns:
Return Type Description strThe file type (e.g. "jpeg")
Example:
import mecsimcalc as msc def main ( inputs ) : input_file = inputs [ 'file' ] file , metadata = msc . input_to_file ( input_file , metadata = True ) download_file_type = msc . metadata_to_filetype ( metadata ) return { "file_type" : download_file_type }
Text
string_to_file
[Source]
string_to_file ( text filename = "myfile" , download_text = "Download File" , )
Description:
Generates a downloadable text file containing the given text
Arguments:
Argument Type Description textstr Text to be downloaded filenamestr (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:
Exception Description TypeErrorIf the input text is not a string.
Returns:
Return Type Description strHTML download link
Example:
Python
import mecsimcalc as msc def main ( inputs ) : download_link = msc . string_to_file ( "Hello World!" ) return { "download" : download_link }
Jinja2
Spreadsheets
file_to_dataframe
[Source]
file_to_dataframe ( file_data ) :
Description:
Converts a base64 encoded file data into a pandas DataFrame
Arguments:
Argument Type Description file_dataio.BytesIO Decoded file data (e.g. from input_to_file )
Raises:
Exception Description 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 Type Description pd.DataFrameDataFrame created from file data
Example:
import mecsimcalc as msc def main ( inputs ) : input_file = inputs [ 'file' ] decoded_file = msc . input_to_file ( input_file ) df = msc . file_to_dataframe ( decoded_file ) return { "dataframe" : df . to_dict ( ) }
[Source]
input_to_dataframe ( file ) :
Description:
Converts a base64 encoded file data into a pandas DataFrame
Arguments:
Argument Type Description input_filestr Base64 encoded file data get_file_typebool If True, the function also returns the file type (Defaults to False)
Returns:
Return Type Description Condition pd.DataFrameDataFrame created from file data get_file_type is False (pd.DataFrame, str)Tuple containing the DataFrame and the file type get_file_type is True
Example:
import mecsimcalc as msc def main ( inputs ) : input_file = inputs [ 'file' ] df , file_type = msc . input_to_dataframe ( input_file , get_file_type = True ) return { "dataframe" : df . to_dict ( ) , "file_type" : file_type }
print_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 DataFrame
Arguments:
Argument Type Description dfpd.DataFrame DataFrame to be converted downloadbool (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 Type Description Condition strHTML table download is False Tuple[str, str](HTML table, HTML download link) download is True
Example:
Python Code:
import mecsimcalc as msc def main ( inputs ) : input_file = inputs [ 'file' ] df = msc . input_to_dataframe ( input_file ) table , download = msc . print_dataframe ( df , download = True , download_file_name = "Table" , download_text = "Download My Table HERE!" , download_file_type = "xlsx" ) return { "table" : table , "download" : download }
Output using Jinja2 Template:
Displaying Table { { outputs . table } } Downloading Table { { outputs . download } }
Tables
table_to_dataframe
[Source]
table_to_dataframe ( column_headers , rows ) :
Description:
Create a DataFrame from given rows and column headers
Arguments:
Argument Type Description column_headersList[str] List of column headers rowsList[List[str]] List of rows to be converted into a DataFrame. Each column is a list of strings
Returns:
Return Type Description pd.DataFrameDataFrame created from headers and rows
Example:
import mecsimcalc as msc def main ( inputs ) : column_headers = [ "A" , "B" , "C" ] rows = [ [ "a" , "b" , "c" ] , [ "d" , "e" , "f" ] ] df = msc . table_to_dataframe ( column_headers , rows ) return { "dataframe" : df . to_dict ( ) }
print_table
[Source]
print_table ( column_headers , rows ) :
Description:
Creates an HTML table from given rows and column headers
Arguments:
Argument Type Description column_headersList[str] List of column headers rowsList[List[str]] List of rows to be converted into a table. Each column is a list of strings indexbool (optional)Whether to use the first column as the DataFrame's index. (Defaults to True)
Returns:
Return Type Description strHTML table created from rows and headers
Example:
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:
Displaying Table { { outputs . table } }
Images
file_to_PIL
[Source]
Description:
Transforms a file into a Pillow Image object
Arguments:
Argument Type Description filestr Decoded file data (returned from input_to_file)
Raises:
Exception Type Description ValueErrorIf the file does not contain image data
Returns:
Return Type Description ImagePillow Image object
Example:
Python Code:
import mecsimcalc as msc def main ( inputs ) : input_file = inputs [ 'file' ] decoded_file = msc . input_to_file ( input_file ) image = msc . file_to_PIL ( decoded_file ) return { "image" : image }
Output using Jinja2 Template:
Displaying Image { { outputs . image } }
[Source]
input_to_PIL ( input_file , get_file_type = False ) :
Description:
Converts a base64 encoded file data into a pillow image
Arguments:
Argument Type Description input_filestr Base64 encoded file data get_file_typebool If True, the function also returns the file type (Defaults to False)
Returns:
Return Type Description Condition PIL.Image.ImagePillow Image object get_file_type is False Tuple[PIL.Image.Image, str](pillow image, metadata) get_file_type is True
Example:
import mecsimcalc as msc def main ( inputs ) : input_file = inputs [ 'file' ] image , file_type = msc . input_to_PIL ( input_file , get_file_type = True ) return { "image" : image , "file_type" : file_type }
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 link
Arguments:
Argument Type Description imagePIL.Image.Image Pillow image widthint (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 Type Description Condition strHTML image download is False Tuple[str, str](HTML image, download link) download is True
Example:
Python Code:
import mecsimcalc as msc def main ( inputs ) : input_file = inputs [ 'file' ] image , metadata = msc . input_to_PIL ( input_file ) html_image , download = msc . 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:
Displaying Image { { outputs . image } } Downloading Image { { outputs . download } }
Plots
print_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 image
Arguments:
Argument Type Description plot_objaxes or figure Matplotlib 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) 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 Type Description Condition strHTML image download is False Tuple[str, str](HTML image, HTML download link) download is True
Example:
Python Code:
import matplotlib . pyplot as plt import numpy as np import mecsimcalc as msc def main ( inputs ) : 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 = msc . 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:
Displaying Image { { outputs . image } } Downloading Image { { outputs . download } }
print_animation
[Source]
print_animation ( ani , fps = 30 , save_dir = "/tmp/temp_animation.gif" ) :
Description:
Converts a matplotlib animation into an animated GIF. Returns an HTML image tag to display it in your app.
Arguments:
Argument Type Description aniFuncAnimation The matplotlib animation to be converted. fpsint (optional)Frames per second for the animation. (Defaults to 30) save_dirstr (optional)The directory to temporarily save files. You can only write to the tmp directory in mecsimcalc. Defaults to "/tmp/temp_animation.gif"
Returns:
Return Type Description strThe HTML image tag as a string.
Example:
import matplotlib . pyplot as plt from matplotlib . animation import FuncAnimation import numpy as np import mecsimcalc as msc def main ( inputs ) : fig , ax = plt . subplots ( ) x = np . linspace ( 0 , 2 * np . pi , 100 ) y = np . sin ( x ) line , = ax . plot ( x , y ) def update ( frame ) : line . set_ydata ( np . sin ( x + frame / 10 ) ) return line , ani = FuncAnimation ( fig , update , frames = 100 ) animation = msc . print_animation ( ani ) return { "animation" : animation }
animate_plot
[Source]
animate_plot ( x , y , duration = 3 , fps = 15 , x_label = "x" , y_label = "y" , title = "y = f(x)" , show_axes = True , follow_tip = False , save_dir = "/tmp/temp_animation.gif" , follow_tip = False , hold_last_frame = 1.0 , )
Description:
Creates an animated plot from given x and y data and returns it as an HTML image tag.
Arguments:
Argument Type Description xnp.ndarray The x-coordinates of the data points. ynp.ndarray The y-coordinates of the data points. durationfloat (optional)The duration of the animation in seconds. Defaults to 3. fpsfloat (optional)Frames per second for the animation. Defaults to 15. x_labelstr (optional)The label for the x-axis. Defaults to "x". y_labelstr (optional)The label for the y-axis. Defaults to "y". titlestr (optional)Title of the plot. Defaults to "y = f(x)". show_axesbool (optional)Whether to show the x and y axes. Defaults to True. follow_tipbool (optional)Whether to follow the tip of the line as it moves along the x-axis. Defaults to False. hold_last_framefloat (optional)The duration to hold the last frame in seconds. Defaults to 1.0. save_dirstr (optional)The directory to temporarily save files. You can only write to the tmp directory in mecsimcalc. Defaults to "/tmp/temp_animation.gif"
Returns:
Return Type Description strThe HTML image tag containing the animated plot.
Example:
import numpy as np import mecsimcalc as msc def main ( inputs ) : x = np . linspace ( 0 , 10 , 100 ) y = np . sin ( x ) animation_html = msc . animate_plot ( x , y , duration = 4 , title = "Sine Wave" , show_axes = True ) return { "animation" : animation_html }
plot_slider
[Source]
plot_slider ( f_x , x_range , y_range = None , title = "" , x_label = "x" , y_label = "y" , num_points = 250 , initial_value = 1 , step_size = 0.1 , slider_range = ( - 10 , 10 ) , ) :
Description:
Creates an interactive plot with a slider using Plotly, allowing the user to dynamically update the plot based on a parameter.
Arguments:
Argument Type Description f_xCallable[[float, np.ndarray], np.ndarray] A function that takes a float and an array of x-values, and returns an array of y-values. x_rangeTuple[float, float] A tuple defining the range of x-values (start, end) for the plot. y_rangeTuple[float, float] (optional)A tuple defining the range of y-values (start, end) for the plot. Defaults to None. titlestr (optional)Title of the plot. Defaults to "". x_labelstr (optional)Label for the x-axis. Defaults to "x". y_labelstr (optional)Label for the y-axis. Defaults to "y". num_pointsint (optional)Number of points to plot (line resolution). Defaults to 250. initial_valuefloat (optional)Initial value of the slider. Defaults to 1. step_sizefloat (optional)Step size for the slider. Defaults to 0.1. slider_rangeTuple[float, float] (optional)Range for the slider values (start, end). Defaults to (-10, 10).
Returns:
Return Type Description strThe HTML string containing the Plotly interactive plot.
Example:
import mecsimcalc as msc def parabola ( a , x ) : return a * x ** 2 def main ( inputs ) : plot_html = msc . plot_slider ( parabola , x_range = ( - 10 , 10 ) , y_range = ( - 100 , 100 ) ) return { "plot" : plot_html }
append_to_google_sheet
[Source]
append_to_google_sheet ( service_account_info = { . . . } , spreadsheet_id = "123abc..." , values = [ [ "name" , 12837 , . . . ] ] , range_name = "Sheet1!A1" , include_timestamp = True )
Description:
This function appends given values to a specified Google Sheet and optionally includes a current timestamp with each entry. It transforms data into a Google Sheets document, facilitating dynamic data entry directly from your application.
Arguments:
Argument Type Description service_account_infodict The service account credentials used for Google Sheets API authentication. spreadsheet_idstr The unique identifier of the target Google Spreadsheet. valueslist of lists The data to append. Each list element represents a row of data. range_namestr (optional)The A1 notation of the range to start appending data. Defaults to "Sheet1!A1". include_timestampbool (optional)If True, appends the current timestamp to each row of data. Defaults to True.
Returns:
Return Type Description dictThe response from the Google Sheets API, containing details of the append operation.
Example:
Code step:
import mecsimcalc as msc def main ( inputs ) : service_account_info = { } spreadsheet_id = 'your_spreadsheet_id_here' values = [ [ inputs [ 'input_1' ] , inputs [ 'input_2' ] , inputs [ 'input_3' ] ] , ] result = msc . append_to_google_sheet ( service_account_info , spreadsheet_id , values ) return { "result" : result }
send_gmail
[Source]
send_gmail ( sender_email = 'sender@example.com' , receiver_email = 'receiver@example.com' , subject = "Quiz" , app_password = "xxxx xxxx xxxx xxxx" , values = [ [ "name" , "grade" ] ] )
Description:
This function sends an email with specified values formatted in the message body, utilizing a service account for authentication.
Arguments:
Argument Type Description sender_emailstr The email address of the sender. receiver_emailstr The email address of the receiver. subjectstr The subject line of the email. app_passwordstr The app-specific password for the sender's email account. valueslist A list of lists. Each list contains data to be included in the email body.
Returns:
Return Type Description boolReturns True if the email was sent successfully, otherwise False.
Example Usage:
import mecsimcalc as msc def main ( inputs ) : sender_email = 'sender@example.com' receiver_email = 'receiver@example.com' subject = 'Test Email' app_password = 'your_app_password_here' name = inputs [ 'name' ] grade = inputs [ 'grade' ] values = [ [ name , grade ] ] result = msc . send_gmail ( sender_email , receiver_email , subject , app_password , values ) return { "result" : result }