Spaces:
Running
Running
Merge pull request #36 from alecrem/chore/no-ref/refactor-common-functions
Browse files- Middle_School_Card_Search.py +5 -36
- pages/1_Check_Card_List.py +6 -20
- streamlit_common/lib.py +57 -0
Middle_School_Card_Search.py
CHANGED
@@ -1,38 +1,7 @@
|
|
1 |
import streamlit as st
|
2 |
import pandas as pd
|
3 |
-
import urllib.parse
|
4 |
import streamlit_common.footer
|
5 |
-
|
6 |
-
|
7 |
-
def compose_scryfall_url(x: str) -> str:
|
8 |
-
return f"https://scryfall.com/search?q=prefer%3Aoldest%20!%22{urllib.parse.quote_plus(x)}%22"
|
9 |
-
|
10 |
-
|
11 |
-
def row_to_link(x: pd.DataFrame) -> None:
|
12 |
-
cardname = x["name"]
|
13 |
-
if x.name_ja is not "":
|
14 |
-
cardname = f"{cardname} / {x.name_ja}"
|
15 |
-
st.markdown(f"- [{cardname}]({x.link})")
|
16 |
-
|
17 |
-
|
18 |
-
def is_legal(cardname: str) -> list:
|
19 |
-
english_match = mslist_df[mslist_df["name"].str.lower() == cardname.lower()]
|
20 |
-
cardname_en_list = None
|
21 |
-
if english_match.shape[0] > 0:
|
22 |
-
cardname_en_list = english_match["name"].to_list()
|
23 |
-
cardname_ja_list = english_match["name_ja"].to_list()
|
24 |
-
japanese_match = mslist_df[mslist_df["name_ja"] == cardname]
|
25 |
-
if japanese_match.shape[0] > 0:
|
26 |
-
cardname_en_list = japanese_match["name"].to_list()
|
27 |
-
cardname_ja_list = japanese_match["name_ja"].to_list()
|
28 |
-
if cardname_en_list is not None and len(cardname_en_list) > 0:
|
29 |
-
return [
|
30 |
-
cardname_en_list[0] or None,
|
31 |
-
cardname_ja_list[0] or None,
|
32 |
-
]
|
33 |
-
|
34 |
-
return None
|
35 |
-
|
36 |
|
37 |
mslist_path = "output/middleschool.csv"
|
38 |
number_shown_results = 20
|
@@ -55,7 +24,7 @@ mslist_df.fillna("", inplace=True)
|
|
55 |
st.write(mslist_df.shape[0], "cards are legal")
|
56 |
|
57 |
name_input = st.text_input(f"Search by card name").strip()
|
58 |
-
exact_match =
|
59 |
results_en_df = mslist_df[
|
60 |
mslist_df["name"].str.contains(name_input.lower(), case=False)
|
61 |
]
|
@@ -69,12 +38,12 @@ if name_input:
|
|
69 |
if exact_match[1] is not None:
|
70 |
cardname = f"{cardname} / {exact_match[1]}"
|
71 |
st.write(
|
72 |
-
f"✅ [{cardname}]({compose_scryfall_url(exact_match[0])}) is an exact match"
|
73 |
)
|
74 |
st.write(results_df.shape[0], f'cards found by "{name_input}"')
|
75 |
if results_df.shape[0] > number_shown_results:
|
76 |
st.write(f"Top {number_shown_results} results:")
|
77 |
-
results_df["link"] = results_df["name"].apply(compose_scryfall_url)
|
78 |
-
results_df[:number_shown_results].transpose().apply(row_to_link)
|
79 |
|
80 |
streamlit_common.footer.write_footer()
|
|
|
1 |
import streamlit as st
|
2 |
import pandas as pd
|
|
|
3 |
import streamlit_common.footer
|
4 |
+
import streamlit_common.lib as lib
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
mslist_path = "output/middleschool.csv"
|
7 |
number_shown_results = 20
|
|
|
24 |
st.write(mslist_df.shape[0], "cards are legal")
|
25 |
|
26 |
name_input = st.text_input(f"Search by card name").strip()
|
27 |
+
exact_match = lib.get_legal_cardnames(name_input, mslist_df)
|
28 |
results_en_df = mslist_df[
|
29 |
mslist_df["name"].str.contains(name_input.lower(), case=False)
|
30 |
]
|
|
|
38 |
if exact_match[1] is not None:
|
39 |
cardname = f"{cardname} / {exact_match[1]}"
|
40 |
st.write(
|
41 |
+
f"✅ [{cardname}]({lib.compose_scryfall_url(exact_match[0])}) is an exact match"
|
42 |
)
|
43 |
st.write(results_df.shape[0], f'cards found by "{name_input}"')
|
44 |
if results_df.shape[0] > number_shown_results:
|
45 |
st.write(f"Top {number_shown_results} results:")
|
46 |
+
results_df["link"] = results_df["name"].apply(lib.compose_scryfall_url)
|
47 |
+
results_df[:number_shown_results].transpose().apply(lib.row_to_link)
|
48 |
|
49 |
streamlit_common.footer.write_footer()
|
pages/1_Check_Card_List.py
CHANGED
@@ -1,23 +1,7 @@
|
|
1 |
import streamlit as st
|
2 |
import pandas as pd
|
3 |
-
import re
|
4 |
import streamlit_common.footer
|
5 |
-
|
6 |
-
|
7 |
-
def remove_number_of_copies(line: str) -> str:
|
8 |
-
if len(line.strip()) < 1:
|
9 |
-
return None
|
10 |
-
pattern = re.compile("^([0-9]+) +")
|
11 |
-
return pattern.sub("", line)
|
12 |
-
|
13 |
-
|
14 |
-
def is_legal(cardname: str) -> bool:
|
15 |
-
if mslist_df[mslist_df["name"].str.lower() == cardname.lower()].shape[0] > 0:
|
16 |
-
return True
|
17 |
-
if mslist_df[mslist_df["name_ja"] == cardname].shape[0] > 0:
|
18 |
-
return True
|
19 |
-
return False
|
20 |
-
|
21 |
|
22 |
mslist_path = "output/middleschool.csv"
|
23 |
|
@@ -46,12 +30,14 @@ input_list = col1.text_area(
|
|
46 |
cardnames = []
|
47 |
|
48 |
for line in input_list.split("\n"):
|
49 |
-
cardname = remove_number_of_copies(line)
|
50 |
if cardname is not None:
|
51 |
-
cardnames.append(remove_number_of_copies(cardname))
|
52 |
|
53 |
input_cards = pd.DataFrame(cardnames, columns=["cardname"])
|
54 |
-
input_cards["legal"] = input_cards["cardname"].apply(
|
|
|
|
|
55 |
|
56 |
col2.write("##### Middle School legality")
|
57 |
col2.dataframe(input_cards[["legal", "cardname"]], use_container_width=True)
|
|
|
1 |
import streamlit as st
|
2 |
import pandas as pd
|
|
|
3 |
import streamlit_common.footer
|
4 |
+
import streamlit_common.lib as lib
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
mslist_path = "output/middleschool.csv"
|
7 |
|
|
|
30 |
cardnames = []
|
31 |
|
32 |
for line in input_list.split("\n"):
|
33 |
+
cardname = lib.remove_number_of_copies(line)
|
34 |
if cardname is not None:
|
35 |
+
cardnames.append(lib.remove_number_of_copies(cardname))
|
36 |
|
37 |
input_cards = pd.DataFrame(cardnames, columns=["cardname"])
|
38 |
+
input_cards["legal"] = input_cards["cardname"].apply(
|
39 |
+
lib.is_cardname_legal, args=[mslist_df]
|
40 |
+
)
|
41 |
|
42 |
col2.write("##### Middle School legality")
|
43 |
col2.dataframe(input_cards[["legal", "cardname"]], use_container_width=True)
|
streamlit_common/lib.py
ADDED
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
import re
|
4 |
+
import urllib.parse
|
5 |
+
|
6 |
+
|
7 |
+
def compose_scryfall_url(cardname: str) -> str:
|
8 |
+
"""Compose a Scryfall URL from the passed card name"""
|
9 |
+
return f"https://scryfall.com/search?q=prefer%3Aoldest%20!%22{urllib.parse.quote_plus(cardname)}%22"
|
10 |
+
|
11 |
+
|
12 |
+
def row_to_link(row: pd.DataFrame) -> None:
|
13 |
+
"""Prints a list item with a Scryfall link for the card in the row passed"""
|
14 |
+
cardname = row["name"]
|
15 |
+
if row.name_ja is not "":
|
16 |
+
cardname = f"{cardname} / {row.name_ja}"
|
17 |
+
st.markdown(f"- [{cardname}]({row.link})")
|
18 |
+
|
19 |
+
|
20 |
+
def get_legal_cardnames(cardname: str, mslist_df: pd.DataFrame) -> list:
|
21 |
+
"""Returns a list with the English and Japanese names for the card
|
22 |
+
if there is an exact match, or `None` if there is not.
|
23 |
+
"""
|
24 |
+
english_match = mslist_df[mslist_df["name"].str.lower() == cardname.lower()]
|
25 |
+
cardname_en_list = None
|
26 |
+
if english_match.shape[0] > 0:
|
27 |
+
cardname_en_list = english_match["name"].to_list()
|
28 |
+
cardname_ja_list = english_match["name_ja"].to_list()
|
29 |
+
japanese_match = mslist_df[mslist_df["name_ja"] == cardname]
|
30 |
+
if japanese_match.shape[0] > 0:
|
31 |
+
cardname_en_list = japanese_match["name"].to_list()
|
32 |
+
cardname_ja_list = japanese_match["name_ja"].to_list()
|
33 |
+
if cardname_en_list is not None and len(cardname_en_list) > 0:
|
34 |
+
return [
|
35 |
+
cardname_en_list[0] or None,
|
36 |
+
cardname_ja_list[0] or None,
|
37 |
+
]
|
38 |
+
return None
|
39 |
+
|
40 |
+
|
41 |
+
def remove_number_of_copies(line: str) -> str:
|
42 |
+
"""Remove the number of copies in front of the card name
|
43 |
+
from a line in a card list
|
44 |
+
"""
|
45 |
+
if len(line.strip()) < 1:
|
46 |
+
return None
|
47 |
+
pattern = re.compile("^([0-9]+) +")
|
48 |
+
return pattern.sub("", line)
|
49 |
+
|
50 |
+
|
51 |
+
def is_cardname_legal(cardname: str, mslist_df: pd.DataFrame) -> bool:
|
52 |
+
"""Returns wether a card with exactly the passed name is legal in the format"""
|
53 |
+
if mslist_df[mslist_df["name"].str.lower() == cardname.lower()].shape[0] > 0:
|
54 |
+
return True
|
55 |
+
if mslist_df[mslist_df["name_ja"] == cardname].shape[0] > 0:
|
56 |
+
return True
|
57 |
+
return False
|