Storage & Scaling

Persistent key-value storage and the window-scale utility used for resolution-independent layouts.

pylay.Storagepylay.UI_SCALEpylay.scaled_px

Storage

A simple persistent key-value store backed by a JSON file in the user's home directory (~/.{app_name}/storage.json). All reads and writes are synchronous. Use it for preferences, progress, and any data that must survive application restarts.

class Storage
Persistent JSON key-value store.
Storage(app_name="app")
MethodDescription
get(key, default=None)Read a value by key. Returns default if the key does not exist.
set(key, value)Write a value and immediately flush to disk.
remove(key)Delete a key. No-op if the key does not exist.
clear()Delete all stored keys.
"key" in storeCheck whether a key exists (__contains__).
from pylay import Storage

store = Storage("Pianove")

# Save user preferences
store.set("dark_mode", True)
store.set("volume", 0.8)
store.set("streak", 7)

# Read on next launch
dark = store.get("dark_mode", default=True)
vol  = store.get("volume", default=1.0)

# Check existence
if "streak" in store:
    print(f"Streak: {store.get('streak')}")

# Reset a value
store.remove("streak")
store.clear()   # wipe everything
Values must be JSON-serialisable: str, int, float, bool, None, list, or dict. Passing a pygame.Color or other non-serialisable type will raise a TypeError.

UI Scaling

PyLay scales the UI proportionally to the window size relative to a reference resolution (default 1024 × 768). The global UI_SCALE factor is applied automatically to font sizes and pixel values when you use the scaled_* functions.

Function / VariableDescription
UI_SCALEGlobal float scale factor. 1.0 at the reference resolution (1024 × 768). Read-only; updated by set_window_size().
set_window_size(w, h, ref_w=1024, ref_h=768)Recompute UI_SCALE for a new window size. Call in every resize handler.
scaled_px(base_px)Convert a base pixel value to scaled pixels. Returns an int.
scaled_font_size(base_size)Convert a base font size to scaled size. Returns an int.
from pylay import App, set_window_size, scaled_px, scaled_font_size

app = App("Scalable App", 1024, 768)

def on_resize(w, h):
    set_window_size(w, h)
    # Re-solve all layout constraints for the new scale:
    app.root.layout()

app.on_resize = on_resize

# Use scaled values in your custom drawing:
padding = scaled_px(20)          # 20px at 1024×768, ~30px at 1536×1152
font    = scaled_font_size(16)   # proportional font size
PyLay's built-in widgets already use UI_SCALE internally. You only need scaled_px when doing custom rendering in a PygameCanvas or UIElement.render() override.