use options, global vars and give everyone their own page

This commit is contained in:
flukx 2024-08-18 08:11:32 +02:00
parent 4247595977
commit 17b37f0837

View file

@ -13,6 +13,45 @@ import location
from part import Part
import part_list_io
gl_options: argparse.Namespace
gl_parts: list[Part]
def get_options() -> argparse.Namespace:
"""Abuse argparse for collecting file names."""
parser = argparse.ArgumentParser()
part_list_io.add_file_args(parser)
parser.add_argument("--port", "-p",
help="Port on which to serve the website.",
type=int,
default=11111)
parser.add_argument("--host", "-h",
help="Host on which to run. "
"For some cases 0.0.0.0 is necessary for accessability from outside.",
default="0.0.0.0")
return parser.parse_args()
gl_options = get_options()
"""Global (module-wide) variables."""
def load_data():
"""Load data from text files and save to global part list.
Could implement that data is only loaded when necessary.
Then an argument force would be useful to reload.
"""
gl_parts[:] = part_list_io.get_parts(gl_options)
ui.notify("Data loaded", position="top", type="success")
load_data()
def save_data() -> None:
"""Save parts to files."""
part_list_io.save_parts(gl_parts, gl_options.input_file, gl_options.locations_file)
ui.notify(
message="Data saved",
position="top",
type="success"
)
def antilen(string: str):
"""Return a bigger number for shorter strings, except 0 for ""."""
@ -77,11 +116,6 @@ async def find_parts(parts: list[Part], search_string: str, max_number: int=10)
return []
def get_file_names() -> argparse.Namespace:
"""Abuse argparse for collecting file names."""
parser = argparse.ArgumentParser()
part_list_io.add_file_args(parser)
return parser.parse_args([])
async def list_parts(ui_element: nicegui.ui.element, parts: Iterable[Part]) -> None:
@ -89,6 +123,7 @@ async def list_parts(ui_element: nicegui.ui.element, parts: Iterable[Part]) -> N
Args:
ui_element: Some UI element that can be changed.
parts: parts to be displayed
"""
# gives other searches 10 ms time to abort this display which might take long
await asyncio.sleep(0.01)
@ -129,29 +164,14 @@ async def list_parts(ui_element: nicegui.ui.element, parts: Iterable[Part]) -> N
"height=100px").props("fit='scale-down'")
def load_data() -> list[Part]:
"""Load data from text files.
Could implement that data is only loaded when necessary.
Then an argument force would be useful to reload.
Returns:
list of all things listed in the files
"""
options = get_file_names()
return part_list_io.get_parts(options)
def save_data(parts) -> None:
"""Save parts to files."""
options = get_file_names()
part_list_io.save_parts(parts, options.input_file, options.locations_file)
def search_page(parts: list[Part]) -> None:
@ui.page("/")
def search_page() -> None:
"""Create a NiceGUI page with a search input field and search results.
Args:
parts: list of parts to search in
Uses global gl_parts list.
"""
print("(Re)build search page.")
# UI container for the search results.
@ -160,8 +180,8 @@ def search_page(parts: list[Part]) -> None:
# Search queries (max. 1) running. Here to be cancellable by different search coroutines.
running_queries: list[asyncio.Task] = []
# should use the parts as they are when clicked
ui.button("Save").on_click(lambda click_event_arguments, parts=parts: save_data(parts))
# should use the parts as they are when clicked: global variable
ui.button("Save").on_click(lambda click_event_arguments: save_data())
async def search(event: nicegui.events.ValueChangeEventArguments) -> None:
"""Search for cocktails as you type.
@ -361,19 +381,10 @@ def show_part_changer(ui_element: nicegui.ui.element, part: Part) -> None:
if __name__ in {"__main__", "__mp_main__"}:
nicegui.app.add_static_files('/images_landscape', 'images_landscape')
search_page(load_data())
options = {}
try:
option_file = open('flinventory-config.json')
except FileNotFoundError:
pass
else:
with option_file:
options = json.load(option_file)
ui.run(title="Fahrradteile",
favicon="website_resources/favicon.ico",
language="de",
host=options.get("host", "0.0.0.0"),
port=options.get("port", 11111))
# if __name__ in {"__main__", "__mp_main__"}:
nicegui.app.add_static_files('/images_landscape', 'images_landscape')
ui.run(title="Fahrradteile",
favicon="website_resources/favicon.ico",
language="de",
host=gl_options.host,
port=gl_options.port)