Skip to content

Commit

Permalink
🎨 Format Python code with psf/black
Browse files Browse the repository at this point in the history
  • Loading branch information
fedebotu authored Sep 19, 2023
1 parent a550cea commit 423cb4f
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 51 deletions.
125 changes: 78 additions & 47 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import sys
from copy import deepcopy
import gettext

_ = gettext.gettext
import pandas as pd
import streamlit as st
Expand All @@ -13,20 +14,22 @@
from ktrains.srt.srt import SRT
from ktrains.utils import Stations, save_to_log, LINKS

language = st.sidebar.selectbox(_('Select your language'), ['English', '한국어', 'Italiano','Español'])
if language == 'English':
language = 'en'
elif language == '한국어':
language = 'kr'
elif language == 'Español':
language = 'es'
elif language == 'Italiano':
language = 'it'

language = st.sidebar.selectbox(
_("Select your language"), ["English", "한국어", "Italiano", "Español"]
)
if language == "English":
language = "en"
elif language == "한국어":
language = "kr"
elif language == "Español":
language = "es"
elif language == "Italiano":
language = "it"

try:
localizator = gettext.translation('base', localedir='locales', languages=[language])
localizator.install()
_ = localizator.gettext
localizator = gettext.translation("base", localedir="locales", languages=[language])
localizator.install()
_ = localizator.gettext
except:
pass

Expand Down Expand Up @@ -71,6 +74,7 @@ def check_login():
return True
return False


st.title("K-trains 🇰🇷-🚄")
st.markdown(_("Fork me on:") + f" [{_('GitHub')}]({LINKS['app']['github']})")

Expand All @@ -79,8 +83,8 @@ def check_login():
# Checkbox with two options and an image on top of each
mode = st.selectbox(_("Select railways company"), [_("Korail"), _("SRT")]).lower()
st.session_state.mode = mode.lower()
st.write(_(
"Get your credentials from {} website: [{}]({})").format(
st.write(
_("Get your credentials from {} website: [{}]({})").format(
LINKS[mode]["name"], LINKS[mode]["link"], LINKS[mode]["link"]
)
)
Expand All @@ -98,10 +102,10 @@ def check_login():
else:
st.error(_("Login failed"))
else:
if language == 'kr':
language_sched = 'kor'
if language == "kr":
language_sched = "kor"
else:
language_sched = 'en'
language_sched = "en"

# What happens when logged in
mode = st.session_state.mode
Expand Down Expand Up @@ -143,26 +147,32 @@ def check_login():
date = date.strftime("%Y%m%d")
time = time.strftime("%H%M%S")

table_stations = Stations(mode,language_sched)
table_stations = Stations(mode, language_sched)
if st.button(_("Search")):
trains = ktrains.search_train(dep, arr, date, time, available_only=False)
if trains == []:
st.error("No trains found")
st.session_state.trains = None
else:
#stations.convert_station_name(trains[0].dep_station_name, lang="en")
# stations.convert_station_name(trains[0].dep_station_name, lang="en")
train_list = []
for train in trains:
if language != 'kr':
if language != "kr":
table_lang = "tc"
else:
table_lang = "kr"
train_list.append(
{
"train_no": train.train_number,
"train_type_name": stations.convert_train_name(train.train_name,lang=table_lang),
"dep_name": stations.convert_station_name(train.dep_station_name,lang=table_lang),
"arr_name": stations.convert_station_name(train.arr_station_name,lang=table_lang),
"train_type_name": stations.convert_train_name(
train.train_name, lang=table_lang
),
"dep_name": stations.convert_station_name(
train.dep_station_name, lang=table_lang
),
"arr_name": stations.convert_station_name(
train.arr_station_name, lang=table_lang
),
"dep_time": train.dep_time,
"arr_time": train.arr_time,
"duration": None,
Expand Down Expand Up @@ -242,7 +252,7 @@ def check_login():
header_checkbox_selection_filtered_only=True,
theme="streamlit",
use_checkbox=True,
width="200%"
width="200%",
)

new_df = pd.DataFrame(grid_return["selected_rows"])
Expand All @@ -253,20 +263,27 @@ def check_login():
# st.write(train_codes)

st.header(_("Runner Settings"))
st.write(_(
"The app will automatically reserve and/or notify you when the train is available."
))
st.write(
_(
"The app will automatically reserve and/or notify you when the train is available."
)
)

col1, col2 = st.columns(2)
with col1:
st.subheader(_("Reserve settings"))
st.markdown(_(
"Reserve the train(s) automatically. You will need to reserve in the app/website within a few minutes.")+ f" ([link]({LINKS[mode]['reserve_link']}))"
)
st.markdown(
_(
"Reserve the train(s) automatically. You will need to reserve in the app/website within a few minutes."
)
+ f" ([link]({LINKS[mode]['reserve_link']}))"
)

st.write(_(
"If you do not process the payment, the reservation will be cancelled automatically."
))
st.write(
_(
"If you do not process the payment, the reservation will be cancelled automatically."
)
)
st.number_input(
_("Number of tickets"),
min_value=1,
Expand All @@ -276,12 +293,20 @@ def check_login():
key="num_tickets",
)

st.write(_("Please select the preferred seat type. If both are selected, the app will try to reserve the first available."))
st.write(
_(
"Please select the preferred seat type. If both are selected, the app will try to reserve the first available."
)
)
sub_col1, sub_col2 = st.columns(2)
with sub_col1:
general_seat = st.checkbox(_("General seat"), key="general_seat", value=True)
general_seat = st.checkbox(
_("General seat"), key="general_seat", value=True
)
with sub_col2:
special_seat = st.checkbox(_("Special seat"), key="special_seat", value=True)
special_seat = st.checkbox(
_("Special seat"), key="special_seat", value=True
)
if general_seat and not special_seat:
seat_type = "R"
elif not general_seat and special_seat:
Expand All @@ -291,13 +316,19 @@ def check_login():
with col2:
st.subheader(_("Email notifications settings"))
st.write(_("Notify you when the train is available."))
st.write(_(
"Receivers are the email addresses that will receive notifications. Use commas to separate multiple addresses."
))
st.write(_(
"Note: sender email is ")+f" {LINKS['app']['email']} ."+_(" Be sure to check your spam folder."
))
email_receivers = st.text_input(_("Receivers"), st.session_state.email_receivers)
st.write(
_(
"Receivers are the email addresses that will receive notifications. Use commas to separate multiple addresses."
)
)
st.write(
_("Note: sender email is ")
+ f" {LINKS['app']['email']} ."
+ _(" Be sure to check your spam folder.")
)
email_receivers = st.text_input(
_("Receivers"), st.session_state.email_receivers
)
st.session_state.email_receivers = email_receivers

col1, col2 = st.columns(2)
Expand Down Expand Up @@ -358,9 +389,9 @@ def check_login():

# If running, show log
if st.session_state.running:
st.write(_(
"Hang tight, I'm running... This may take some time, until we find a train!"
))
st.write(
_("Hang tight, I'm running... This may take some time, until we find a train!")
)
st.markdown("---")
st.write(_("Log:"))
with open("log.txt", "r") as f:
Expand Down
5 changes: 3 additions & 2 deletions ktrains/korail/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,7 @@

station_names = {"kor": station_names_korean, "en": station_names_english}


def convert_station_name(station_name, lang="en"):
if lang == "en":
# get index of station_name in station_names_english
Expand Down Expand Up @@ -571,10 +572,11 @@ def convert_station_name(station_name, lang="en"):
"Nuriro",
"Mugunghwa",
"Commuter",
]
]

train_types = {"kor": train_types_korean, "en": train_types_english}


def convert_train_name(train_type, lang):
if lang == "en":
index = train_types_english.index(train_type)
Expand All @@ -584,4 +586,3 @@ def convert_train_name(train_type, lang):
return train_types_english[index]
else:
return train_type

4 changes: 2 additions & 2 deletions ktrains/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ def convert_station_name(self, station_name, lang=None):
raise ValueError(
f"Invalid mode: {self.mode}. Must be one of korail or srt."
)
def convert_train_name(self,train_name,lang=None):

def convert_train_name(self, train_name, lang=None):
if lang is None:
lang = self.lang
if self.mode == "korail":
Expand Down

0 comments on commit 423cb4f

Please sign in to comment.