Skip to content

Commit

Permalink
added table_to_dataframe
Browse files Browse the repository at this point in the history
  • Loading branch information
PointlessUser committed May 12, 2023
1 parent e6c221f commit 7d5f77b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
2 changes: 2 additions & 0 deletions mecsimcalc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
file_to_dataframe,
input_to_dataframe,
input_to_PIL,
table_to_dataframe,
)

from .output_utils import (
Expand All @@ -18,6 +19,7 @@
"file_to_dataframe",
"decode_file_data",
"input_to_PIL",
"table_to_dataframe",
"print_dataframe",
"print_img",
"download_text",
Expand Down
21 changes: 20 additions & 1 deletion mecsimcalc/input_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import base64
import io
import pandas as pd
from typing import Tuple, Union
from typing import Tuple, Union, List


def decode_file_data(
Expand Down Expand Up @@ -84,3 +84,22 @@ def input_to_PIL(file) -> Tuple[Image.Image, str]:
img = Image.open(fileData)

return img, metaData


def table_to_dataframe(
columns: List[List[str]], column_headers: List[str]
) -> pd.DataFrame:
"""
Creates a DataFrame from given columns and column headers.
Args:
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:
pd.DataFrame: DataFrame constructed from columns and headers
"""

# Create a dictionary mapping column headers to column values
data_dict = dict(zip(column_headers, columns))

return pd.DataFrame(data_dict)
12 changes: 6 additions & 6 deletions mecsimcalc/output_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,23 @@
def print_dataframe(
df: pd.DataFrame,
download: bool = False,
DownloadText: str = "Download File",
DownloadText: str = "Download Table",
DownloadFileName: str = "myfile",
FileType: str = "csv",
) -> Union[str, str]:
) -> Union(str, Tuple[str, str]):
"""
Creates an HTML table and a download link for a given DataFrame
Args:
df (pandas.DataFrame): DataFrame to be converted
download (bool, optional): If True, a download link is created (Defaults to False)
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): Type of file when downloaded (Defaults to "csv")
FileType (str, optional): File type of download (Defaults to "csv")
Returns:
str: HTML table (if download is False)
Union[str, str]: HTML table, and download link (if download is True)
Tuple[str, str]: HTML table, and download link (if download is True)
"""

if not download:
Expand Down Expand Up @@ -184,7 +184,7 @@ def download_text(
filename: str = "myfile",
extension: str = ".txt",
download_text: str = "Download File",
):
) -> str:
"""
Generates a downloadable text file containing the given text
Expand Down

0 comments on commit 7d5f77b

Please sign in to comment.