File size: 5,058 Bytes
b787616
 
d9b4271
14f90e3
59718b5
ea3872d
24d3971
59718b5
b787616
dda161c
 
b6d4315
 
dda161c
 
 
 
 
 
 
 
 
 
0b21467
c751fd4
 
0b21467
 
6085806
 
 
b6d4315
b787616
b6d4315
 
c751fd4
 
 
 
59718b5
b787616
 
8ceea03
59718b5
b787616
24d3971
 
 
dbc1992
 
 
 
24d3971
 
 
50e3646
24d3971
dc2bac2
 
 
 
 
 
 
 
 
 
dbc1992
c955f0a
dc2bac2
c955f0a
dc2bac2
c955f0a
dc2bac2
c955f0a
dc2bac2
c955f0a
dc2bac2
c955f0a
dc2bac2
21f99c2
b7f5781
 
 
 
 
 
 
 
 
 
 
21f99c2
b7f5781
14ce0f4
21f99c2
dbc1992
14ce0f4
dbc1992
21f99c2
 
14ce0f4
 
 
 
21f99c2
 
dbc1992
 
 
 
24d3971
 
 
dbc1992
 
 
 
24d3971
 
 
0ff4e79
 
 
 
ea3872d
59718b5
ea3872d
59718b5
dda161c
59718b5
dda161c
14f90e3
dda161c
 
 
 
 
 
 
 
 
 
 
d9b4271
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import streamlit as st
import pandas as pd
import streamlit_common.footer
import streamlit_common.lib as lib
import streamlit_common.locale

mslist_path = "output/middleschool_extra_fields.csv"
_ = streamlit_common.locale.get_locale()

if "number_shown_results" not in st.session_state:
    st.session_state["number_shown_results"] = 20
if "lang" not in st.session_state:
    st.session_state["lang"] = "en"


def add_more_results():
    st.session_state["number_shown_results"] += 20


def reset_more_results():
    st.session_state["number_shown_results"] = 20


st.set_page_config(
    page_title="Middle School Tutor | Card Search",
    page_icon="favicon.ico",
    layout="wide",
)
lang = st.sidebar.radio(
    label="Language / 言語",
    options=["English", "日本語"],
    index=1 if st.session_state["lang"] == "ja" else 0,
)
st.session_state["lang"] = "ja" if lang == "日本語" else "en"
l = st.session_state["lang"]
headcol1, headcol2 = st.columns([1, 7])
headcol1.image("favicon.ico", width=80)
headcol2.write(f"# Middle School Tutor")
st.write(f'## {_["search"]["title"][l]}')
st.write(_["search"]["instructions"][l])

mslist_df = pd.read_csv(mslist_path)
mslist_df.fillna("", inplace=True)
st.write(f'**{mslist_df.shape[0]}**{_["search"]["cards_are_legal"][l]}')

results_df = mslist_df

# Filter by card name
input_name = st.text_input(
    f'**{_["search"]["search_by_card_name"][l]}**',
    placeholder=_["search"]["search_by_card_name_placeholder"][l],
).strip()
exact_match = lib.get_legal_cardnames(input_name, mslist_df)
results_en_df = results_df[results_df["name"].str.contains(input_name, case=False)]
results_ja_df = results_df[results_df["name_ja"].str.contains(input_name, case=False)]
results_df = results_en_df.merge(results_ja_df, how="outer")

# Filter by color
(
    colorcol0,
    colorcol1,
    colorcol2,
    colorcol3,
    colorcol4,
    colorcol5,
    colorcol6,
) = st.columns(7)
colorcol0.write(f'**{_["search"]["search_by_color"][l]}**')
if colorcol1.checkbox(_["basic"]["color_w"][l]):
    results_df = results_df[results_df["w"] == True]
if colorcol2.checkbox(_["basic"]["color_u"][l]):
    results_df = results_df[results_df["u"] == True]
if colorcol3.checkbox(_["basic"]["color_b"][l]):
    results_df = results_df[results_df["b"] == True]
if colorcol4.checkbox(_["basic"]["color_r"][l]):
    results_df = results_df[results_df["r"] == True]
if colorcol5.checkbox(_["basic"]["color_g"][l]):
    results_df = results_df[results_df["g"] == True]
if colorcol6.checkbox(_["basic"]["color_c"][l]):
    results_df = results_df[results_df["c"] == True]

# Filter by mana value
min_mv = mslist_df["mv"].min()
max_mv = mslist_df["mv"].max()
mv_options = [mv for mv in range(min_mv, max_mv + 1)]
start_mv, end_mv = st.select_slider(
    f'**{_["search"]["search_by_mv"][l]}**', options=mv_options, value=(min_mv, max_mv)
)
cond1 = results_df["mv"] >= start_mv
cond2 = results_df["mv"] <= end_mv
results_df = results_df[cond1 & cond2]

# Filter by type (select)
col1, col2 = st.columns(2)
type_list = streamlit_common.locale.get_type_options()
select_types = col1.multiselect(
    f'**{_["search"]["select_type"][l]}**',
    type_list[l],
    placeholder=_["search"]["select_type_placeholder"][l],
)
for cardtype in select_types:
    type_to_search = cardtype
    if l == "ja":
        type_to_search = type_list["en"][type_list["ja"].index(cardtype)]
    results_df = results_df[results_df["type"].str.contains(type_to_search, case=False)]

# Filter by type (text input)
input_type = col2.text_input(
    f'**{_["search"]["search_by_type"][l]}**',
    placeholder=_["search"]["search_by_type_placeholder"][l],
).strip()
results_df = results_df[results_df["type"].str.contains(input_type, case=False)]

# Filter by text
input_text = st.text_input(
    f'**{_["search"]["search_by_text"][l]}**',
    placeholder=_["search"]["search_by_text_placeholder"][l],
).strip()
results_df = results_df[results_df["text"].str.contains(input_text, case=False)]

if results_df.shape[0] < mslist_df.shape[0]:
    if exact_match[0]:
        cardname = exact_match[1]
        if exact_match[2] is not None:
            cardname = f"{cardname} / {exact_match[2]}"
        st.write(
            f'✅ [{cardname}]({lib.compose_scryfall_url(exact_match[1])}) {_["search"]["exact_match"][l]}'
        )
    st.write(f'**{results_df.shape[0]}**{_["search"]["cards_found"][l]}')
    if results_df.shape[0] > st.session_state["number_shown_results"]:
        st.write(_["search"]["top_results"][l])

    results_df["link"] = results_df["name"].apply(lib.compose_scryfall_url)
    results_df[: st.session_state["number_shown_results"]].transpose().apply(
        lib.row_to_link
    )

    if results_df.shape[0] > st.session_state["number_shown_results"]:
        st.button(label=_["search"]["see_more"][l], on_click=add_more_results)
    if st.session_state["number_shown_results"] > 20:
        st.button(
            label=_["search"]["see_20"][l],
            on_click=reset_more_results,
        )

streamlit_common.footer.write_footer()