Skip to content

Commit

Permalink
feat: allow yaml input
Browse files Browse the repository at this point in the history
  • Loading branch information
siemdejong committed Nov 3, 2024
1 parent 9e76deb commit b78377b
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 16 deletions.
6 changes: 6 additions & 0 deletions examples/example/example.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
- situation: "You don't know how to use this tool"
misery index: 80
image: umbrella.png
- situation: "This is just an example"
misery index: 1
image: simple stick figure.png
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ matplotlib
pandas
openpyxl
tqdm
streamlit
streamlit
pyyaml
18 changes: 15 additions & 3 deletions src/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,17 @@ def zip_cards(target_file: Path, source_dir: Path):

with st.popover("Download example data"):
st.write("Download example data to get started.")
with open(
Path(__file__).parent.parent.parent / "examples" / "example" / "example.yaml",
"rb",
) as yaml_file:
st.download_button(
label="Input yaml",
data=yaml_file,
file_name="example.yaml",
mime="text/yaml",
icon=":material/download:",
)
with open(
Path(__file__).parent.parent.parent / "examples" / "example" / "example.csv",
"rb",
Expand Down Expand Up @@ -139,9 +150,10 @@ def zip_cards(target_file: Path, source_dir: Path):
with open(image_dir / image.name, "wb") as image_file:
image_file.write(image.getbuffer())

expansion_logo_path = image_dir / expansion_logo.name
with open(expansion_logo_path, "wb") as logo_file:
logo_file.write(expansion_logo.getbuffer())
if expansion_logo is not None:
expansion_logo_path = image_dir / expansion_logo.name
else:
expansion_logo_path = None

df = parse_input_file(input_file, image_dir)

Expand Down
45 changes: 33 additions & 12 deletions src/ithappens/create_cards.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from matplotlib.transforms import Bbox
from PIL import Image
from tqdm import tqdm
from yaml import safe_load


from ithappens.card import Card
Expand Down Expand Up @@ -138,24 +139,44 @@ def parse_input_file(
Pandas DataFrame with index, description, and misery index.
"""
usecols = ["misery index", "situation", "image"]

df = None
try:
df = pd.read_excel(input_path)
df = df[usecols]
with open(input_path, 'r') as f:
df = pd.json_normalize(safe_load(f))
df = df[usecols]
except TypeError:
bytes_data = input_path.getvalue()
df = pd.json_normalize(safe_load(bytes_data))
df = df[usecols]
except ValueError:
pass
except KeyError:
print(f"Make sure {input_path} has two columns named {usecols}.")
print(f"yaml: Make sure {input_path} has two columns named {usecols}.")
exit()
except Exception as e:
raise e

try:
df = pd.read_csv(input_path)
df = df[usecols]
except UnicodeDecodeError:
print(f"{input_path} is not a valid .csv or .xlsx file.")
exit()
except KeyError:
print(f"Make sure {input_path} has two columns named {usecols}.")
exit()
if df is None:
try:
df = pd.read_excel(input_path)
df = df[usecols]
except ValueError:
pass
except KeyError:
print(f"excel: Make sure {input_path} has two columns named {usecols}.")
exit()

if df is None:
try:
df = pd.read_csv(input_path)
df = df[usecols]
except UnicodeDecodeError:
print(f"{input_path} is not a valid .csv or .xlsx file.")
exit()
except KeyError:
print(f"csv: Make sure {input_path} has two columns named {usecols}.")
exit()

if image_dir:

Expand Down

0 comments on commit b78377b

Please sign in to comment.