From 50516d23969f38730db0988897290d11c0454eb7 Mon Sep 17 00:00:00 2001 From: Yahya Al-Shamali <42281247+PointlessUsername@users.noreply.github.com> Date: Fri, 12 May 2023 03:16:29 -0600 Subject: [PATCH] Update README.md --- README.md | 283 +++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 250 insertions(+), 33 deletions(-) diff --git a/README.md b/README.md index 537fbc5..cd43fb6 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,9 @@ -# Mecsimcalc 0.0.3 documentation +--- +sidebar_label: "mecsimcalc Library" +sidebar_position: 4 +--- + +# 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. @@ -9,11 +14,9 @@ This library is designed to provide a set of functions for handling and converti

decode_file_data

- [Source] + [Source]
- - ```python def decode_file_data(encoded_data, metadata = False) ``` @@ -43,19 +46,64 @@ Converts a base64 encoded file into a file object and metadata >>> file, metadata = decode_file_data(inputFile, metadata = True) >>> print(metadata) data:image/jpeg;base64, ->>> print(file) -<_io.BytesIO object at 0x0000000000000000> +>>> type(file) + +``` + +
+

download_text

+ [Source] +
+ +```python +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: +| Argument | Type | Description | +| ------------------- | ------------------ | ----------------------------------------------------------------------------- | +| **`text`** | **str** | The text to be downloaded | +| **`filename`** | **str** (optional) | The name of the file to be downloaded (Defaults to "myfile") | +| **`extension`** | **str** (optional) | The file extension of the file to be downloaded (Defaults to ".txt") | +| **`download_text`** | **str** (optional) | The text to be displayed on the download button (Defaults to "Download File") | + +#### Returns: +| Return Type | Description | +| ----------- | ------------------------------------- | +| **`str`** | The HTML code for the download button | + +#### Example: + +#### Python + +```python +>>> downloadLink = download_text("Hello World!") +>>> return {"downloadLink": downloadLink} +``` +#### Jinja2 + +```python +# outputs.downloadLink is the html download link generated by the function +{{ outputs.downloadLink }} ``` ## Tables/DataFrames
-

file_data_to_dataframe

- [Source] +

file_to_dataframe

+ [Source]
```python -def file_data_to_dataframe(file_data): +def file_to_dataframe(file_data): ``` #### Description: @@ -84,15 +132,15 @@ Converts a file object into a pandas DataFrame ```python >>> inputFile = inputs['file'] ->>> file, metadata = decode_file_data(inputFile, metadata = True) ->>> df = file_data_to_dataframe(file) +>>> file = decode_file_data(inputFile) +>>> df = file_to_dataframe(file) >>> print(df) A B C ```

input_to_dataframe

- [Source] + [Source]
```python @@ -122,18 +170,22 @@ Converts a base64 encoded file data into a pandas DataFrame >>> df = input_to_dataframe(inputFile) >>> print(df) A B C +0 a b c +1 d e f ```
-

dataframe_to_output

- [Source] +

print_dataframe

+ [Source]
```python -def dataframe_to_output( +def print_dataframe( df, - DownloadText = "Download File", - DownloadFileName = "myfile", + download = False, + DownloadText = "Download Table", + DownloadFileName = "mytable", + FileType = "csv", ): ``` @@ -143,16 +195,20 @@ Creates an HTML table and a download link for a given DataFrame #### Arguments: -| Argument | Type | Description | -| ---------------------- | ------------------ | ----------------------------------------------------------------------- | -| **`df`** | **pd.DataFrame** | 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") | +| Argument | Type | Description | +| ---------------------- | ------------------- | ----------------------------------------------------------------------- | +| **`df`** | **pd.DataFrame** | DataFrame to be converted | +| **`download`** | **bool** (optional) | If True, function returns a download link (Defaults to False) | +| **`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") | +| **`FileType`** | **str** (optional) | File type of download (Defaults to "csv") | #### Returns: -| Return Type | Description | -|**`Tuple[str, str]`**| (HTML table, download link)| +| Return Type | Description | Condition | +| --------------------- | --------------------------- | ----------------- | +| **`str`** | HTML table | download is False | +| **`Tuple[str, str]`** | (HTML table, download link) | download is True | #### Example: @@ -161,10 +217,10 @@ Creates an HTML table and a download link for a given DataFrame ```python >>> inputFile = inputs['file'] >>> df = input_to_dataframe(inputFile) ->>> table, download = dataframe_to_output(df, DownloadFileName = "mytable", DownloadText = "Download Table") +>>> table, download = print_dataframe(df, download = True, DownloadFileName = "FunkyTable", DownloadText = "Download My Funky Table HERE!", FileType = "xlsx") >>> return { "table":table, - "download"download, + "download":download, } ``` @@ -180,12 +236,93 @@ Downloading Table {{ outputs.download }} ``` -## Images +
+

table_to_dataframe

+ [Source] +
+ +```python +def table_to_dataframe(columns: List[List[str]], column_headers: List[str]) -> pd.DataFrame: +``` + +#### Description: +Creates a DataFrame from given columns and headers + +#### Arguments: + +| Argument | Type | Description | +| -------------------- | ------------------- | ---------------------------------------------------------------------------------- | +| **`columns`** | **List[List[str]]** | List of columns to be converted into a DataFrame. Each column is a list of strings | +| **`column_headers`** | **List[str]** | List of column headers | + +#### Returns: +| Return Type | Description | +| ------------------ | ------------------------------------------ | +| **`pd.DataFrame`** | DataFrame created from columns and headers | +#### Example: + +```python +>>> 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] +
+ +```python +print_table(columns: List[List[str]], column_headers: List[str]): +``` + +#### Description: +Creates an HTML table from given columns and headers + +#### Arguments: + +| Argument | Type | Description | +| -------------------- | ------------------- | ------------------------------------------------------------------------------ | +| **`columns`** | **List[List[str]]** | List of columns to be converted into a table. Each column is a list of strings | +| **`column_headers`** | **List[str]** | List of column headers | + +#### Returns: + +| Return Type | Description | +| ----------- | ------------------------------------------- | +| **`str`** | HTML table created from columns and headers | + +#### Example: + +#### Python Code: + +```python +>>> 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: + +```python +# outputs.table is the HTML table +Displaying Table +{{ outputs.table }} +``` + +## Images

input_to_PIL

- [Source] + [Source]
```python @@ -208,10 +345,21 @@ Converts a base64 encoded file data into a pillow image | --------------------------------- | ------------------------ | | **`Tuple[PIL.Image.Image, str]`** | (pillow image, metadata) | +#### Example: + +```python +>>> inputFile = inputs['file'] +>>> img, metadata = input_to_PIL(inputFile) +>>> print(metadata) +data:image/jpeg;base64, +>>> type(file) + +``` +

print_img

- [Source] + [Source]
```python @@ -221,6 +369,7 @@ def print_img( WIDTH = 200, HEIGHT = 200, OriginalSize = False, + download = False, DownloadText = "Download Image", ImageName= "myimg", ): @@ -239,14 +388,16 @@ Converts a pillow image into an HTML image and a download link | **`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) | +| **`download`** | **bool** (optional) | If True, function returns a download link (Defaults to False) | | **`DownloadText`** | **str** (optional) | The text to be displayed on the download link (Defaults to "Download Image") | | **`ImageName`** | **str** (optional) | The name of the image file when downloaded (Defaults to "myimg") | #### Returns: -| Return Type | Description | -| --------------------- | --------------------------- | -| **`Tuple[str, str]`** | (HTML image, download link) | +| Return Type | Description | Condition | +| --------------------- | --------------------------- | ----------------- | +| **`str`** | HTML image | download is False | +| **`Tuple[str, str]`** | (HTML image, download link) | download is True | #### Example: @@ -255,7 +406,73 @@ Converts a pillow image into an HTML image and a download link ```python >>> inputFile = inputs['file'] >>> img, metadata = input_to_PIL(inputFile) ->>> image, download = print_img(img, metadata, OriginalSize = True, DownloadText = "Download Image Here", ImageName = "myimage") +>>> 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: + +```python +# outputs.image is the HTML image +Displaying Image +{{ outputs.image }} + +# outputs.download is the download link +Downloading Image +{{ outputs.download }} +``` + +
+

print_plt

+ [Source] +
+ +```python +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: +| Argument | Type | Description | +| ---------------------- | ------------------- | --------------------------------------------------------------------------- | +| **`plt`** | **plt or figure** | Matplotlib figure | +| **`width`** | **int** (optional) | Output width of the image in pixels (Defaults to 500) | +| **`dpi`** | **int** (optional) | Output dpi of the image in pixels (Defaults to 100) | +| **`download`** | **bool** (optional) | If True, function returns a download link (Defaults to False) | +| **`DownloadText`** | **str** (optional) | The text to be displayed on the download link (Defaults to "Download Plot") | +| **`DownloadFileName`** | **str** (optional) | The name of the image file when downloaded (Defaults to "myplot") | + +#### Returns: +| Return Type | Description | Condition | +| --------------------- | --------------------------- | ----------------- | +| **`str`** | HTML image | download is False | +| **`Tuple[str, str]`** | (HTML image, download link) | download is True | + +#### Example: + +#### Python Code: + +```python +>>> 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,