Spaces:
Running
Running
App Gradio Refactoring Summary
Overview
Successfully refactored the monolithic app_gradio.py (2337 lines) into a modular architecture with a streamlined main file (672 lines) - a 71% reduction in main file size!
New File Structure
Main Application
app_gradio.py(672 lines) - Streamlined entry point that imports and orchestrates all modular components
Modular Components Created
1. Formatters (formatters/)
character_formatter.py- Character sheet formatting for 3-column displayparty_formatter.py- Party sheet formatting and party member displays
2. Handlers (handlers/)
character_handlers.py- Character loading, creation, deletion logiccombat_handlers.py- Combat initiative tracking and turn managementchat_handlers.py- Chat message handling, RAG lookups, command processingparty_handlers.py- Party mode loading, adding/removing party members
3. UI Components (components/)
play_tab.py- Play Game tab UI definitioncreate_tab.py- Create Character tab UI definitionparty_tab.py- Party Management tab UI definition
Backup
app_gradio_old.py(2337 lines) - Original monolithic file preserved
Architecture
Imports Flow
app_gradio.py
βββ Components (UI structure)
β βββ components.play_tab
β βββ components.create_tab
β βββ components.party_tab
β
βββ Handlers (business logic)
β βββ handlers.character_handlers
β βββ handlers.combat_handlers
β βββ handlers.chat_handlers
β βββ handlers.party_handlers
β
βββ Formatters (display logic)
βββ formatters.character_formatter
βββ formatters.party_formatter
Global State Management
The main app_gradio.py manages global state through wrapper functions:
current_character- Active characterconversation_history- Chat historyparty- Party state objectparty_characters- Party character dictgameplay_mode- "character" or "party"
Event Handler Wiring
All event handlers are properly wired in the main file:
- Play Game Tab: Mode toggle, load character, load party, delete, RAG lookup, chat, combat controls, quick actions
- Create Character Tab: Roll stats, create character
- Party Management Tab: Add to party, remove from party
Key Features Preserved
- β All 23+ event handlers properly wired
- β Global state management maintained
- β Character and party modes supported
- β Debug scenarios for testing
- β RAG lookup functionality
- β Combat management (initiative, turns)
- β Character creation with racial bonuses
- β Party management (add/remove)
- β All UI components functional
Benefits
- Maintainability: Logic separated into focused modules
- Testability: Individual components can be tested in isolation
- Reusability: Formatters and handlers can be reused
- Readability: Main file is clean and easy to understand
- Scalability: Easy to add new features without bloating main file
Testing Checklist
- Load character in single character mode
- Load character with debug scenario
- Delete character
- Create new character
- Load party mode
- Add characters to party
- Remove characters from party
- RAG lookup for spells/items
- Chat interaction
- Combat flow (start, next turn, end)
- Quick action buttons
- Clear history
File Size Comparison
| File | Lines | Reduction |
|---|---|---|
| app_gradio_old.py | 2337 | - |
| app_gradio.py | 672 | 71% |
Notes
- All functionality from the original file is preserved
- The modular structure allows for easier future enhancements
- Each handler module has a clear, single responsibility
- UI components define structure, handlers contain logic
- Formatters handle display formatting