Skip to content

Instantly share code, notes, and snippets.

@colinjroberts
Created February 28, 2022 00:41
Show Gist options
  • Save colinjroberts/172af25e535cb05825c5dcee54fb037d to your computer and use it in GitHub Desktop.
Save colinjroberts/172af25e535cb05825c5dcee54fb037d to your computer and use it in GitHub Desktop.
Part 3 of development of a Python urwid app for tracking information during a job search
import urwid
class App():
def __init__(self):
"""Main process that splits the main window into tabs and a main body
using a Pile"""
# Builds primary two subdivisions: tab_menu and body_container
self.tab_menu = self.build_tab_menu(['Todo', 'Jobs', 'Companies', 'People'])
self.body_container = urwid.Columns(self.get_body_container_columns())
# Arrange primary items into a Pile
self.main_pile = urwid.Pile(
[('pack', self.tab_menu), self.body_container])
self.mainloop = urwid.MainLoop(self.main_pile,
palette=[('reversed', 'standout', '')],
unhandled_input=self.q_for_exit)
self.mainloop.run()
def companies(self):
"""Returns a text list of company names, but in the future could contain
an Urwid object like a ListBox of Selectable company names, each of which
could have a callback function that would look up the related company
information and display it in the main body window."""
company_names = ["Albacore",
"BuyNLarge",
"Caltech",
"Dennys",
"Enron",
"Facebook",
"Google",
]
return urwid.Text("\n".join(company_names))
def people(self):
"""Returns a text list of people names, but in the future could contain
an Urwid object like a ListBox of Selectable people names, each of which
could have a callback function that would look up the related person
information and display it in the main body window."""
contact_names = ["Alice Baker",
"Cooper Douglas",
"Eugene Fernando",
"Gretchen Hyacinth",
"Jeannie Kidseth",
"Liz Maroney",
"Norbert Ort",
"Penny Quinn",
]
return urwid.Text("\n".join(contact_names))
def jobs(self):
"""Returns a text list of job names, but in the future could contain
an Urwid object like a ListBox of Selectable job names, each of which
could have a callback function that would look up the related person
information and display it in the main body window."""
job_names = ["Software Engineer",
"Engineer in Test",
"Program Manager",
"Product Designer",
"Engineering Manager",
"Junior Software Engineer",
"SDE I",
"Software Engineer - Backend, Finance",
"Manager; Software Engineering",
"Software Engineering and Product Design Specialist",
]
return urwid.Text("\n".join(job_names))
def build_body_main_window(self, choice):
"""Builds the main window of the body depending on which tab is
currently active. The calling method is a Filler, and this will return
different Text objects to it
"""
main_window_text = urwid.Text(
f"This will be the main window in which {choice} data will appear",
'center', 'clip')
return urwid.LineBox(urwid.Filler(main_window_text, "top"),
title="Todo Details", title_align="left")
def get_body_container_columns(self, choice="Todo"):
"""Builds default main body when app first runs"""
column_1 = urwid.LineBox(
urwid.Filler(urwid.Text("Side bar", 'center', 'clip'), "top"),
title="Todo Details", title_align="left")
column_2 = self.build_body_main_window(choice)
return [("weight", 1, column_1), ("weight", 3, column_2)]
def build_body_container(self, choice="Todo"):
"""Builds the body container"""
return urwid.Columns(self.get_body_container_columns())
def body_picker(self, button, choice):
"""Function for directly changing the content in body_container"""
if choice == "Todo":
side_bar = urwid.LineBox(
urwid.Filler(urwid.Text("todo items", 'center', 'clip'), "top"),
"Todo Title", "left")
main_body = self.build_body_main_window(choice)
list_of_widgets_to_return = [(side_bar, ("weight", 1, False)),
(main_body, ("weight", 3, False))]
elif choice == "Companies":
side_bar = urwid.LineBox(urwid.Filler(self.companies(), "top"),
title="Company Name", title_align="left")
main_body_top = urwid.LineBox(
urwid.Filler(urwid.Text("Open Jobs", 'center', 'clip'), "top"),
title="Open Jobs", title_align="left")
main_body_mid = urwid.LineBox(
urwid.Filler(urwid.Text("Notes", 'center', 'clip'), "top"),
title="Notes", title_align="left")
main_body_bottom = urwid.LineBox(
urwid.Filler(urwid.Text("People", 'center', 'clip'), "top"),
title="People", title_align="left")
main_body = urwid.Pile(
[main_body_top, main_body_mid, main_body_bottom])
list_of_widgets_to_return = [(side_bar, ("weight", 1, False)),
(main_body, ("weight", 3, False))]
elif choice == "People":
side_bar = urwid.LineBox(urwid.Filler(self.people(), "top"),
title="Person Name", title_align="left")
main_body_top = urwid.LineBox(
urwid.Filler(urwid.Text("Details", 'center', 'clip'), "top"),
title="Details", title_align="left")
main_body_bottom = urwid.LineBox(
urwid.Filler(urwid.Text("Notes", 'center', 'clip'), "top"),
title="Notes", title_align="left")
main_body = urwid.Pile([main_body_top, main_body_bottom])
list_of_widgets_to_return = [(side_bar, ("weight", 1, False)),
(main_body, ("weight", 3, False))]
elif choice == "Jobs":
side_bar = urwid.LineBox(urwid.Filler(self.jobs(), "top"),
title="Job Title", title_align="left")
main_body_top = urwid.LineBox(urwid.Filler(urwid.Text(
"Here will be a bunch of options about the job's status",
'center', 'clip'), "top"),
title="Status", title_align="left")
main_body_mid = urwid.LineBox(urwid.Filler(urwid.Text(
"Here will be a bunch of options about the job's posting details",
'center', 'clip'), "top"),
title="Posting Details",
title_align="left")
main_body_bottom = urwid.LineBox(urwid.Filler(urwid.Text(
"Here will be a bunch of options about the job's notes",
'center', 'clip'), "top"),
title="Notes", title_align="left")
main_body = urwid.Pile(
[main_body_top, main_body_mid, main_body_bottom])
list_of_widgets_to_return = [(side_bar, ("weight", 1, False)),
(main_body, ("weight", 3, False))]
else:
list_of_widgets_to_return = [("pack", urwid.Text(
"There must have been a mistake.", 'left', 'clip'))]
self.body_container.contents = urwid.MonitoredFocusList(
list_of_widgets_to_return)
def build_tab_menu(self, choices):
"""Defines and builds tab_menu using list of choices"""
cells = []
for item in choices:
button = urwid.Button(item)
urwid.connect_signal(button, 'click', self.body_picker, item)
cells.append(urwid.AttrMap(button, None, focus_map='reversed'))
return urwid.GridFlow(cells, 20, 2, 1, "left")
def exit_program(self):
"""Exit the main loop and return to the terminal"""
raise urwid.ExitMainLoop()
def q_for_exit(self, key):
"""Check for key presses for q and quits"""
if key in ('q', 'Q'):
self.exit_program()
if __name__ == "__main__":
app = App()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment