generated from hjess/PythonTemplateProject
Loads and loads of data
This commit is contained in:
@@ -6,18 +6,13 @@ from fastapi.templating import Jinja2Templates
|
||||
|
||||
|
||||
class CategoryController:
|
||||
def __init__(self):
|
||||
def __init__(self,data_file="generated_data.json"):
|
||||
"""Initialize the controller."""
|
||||
self.router = APIRouter()
|
||||
self.templates = Jinja2Templates(directory="templates")
|
||||
self.data = self._load_mock_data()
|
||||
self.data = self._load_data( data_file )
|
||||
self._add_routes()
|
||||
|
||||
def _load_mock_data(self):
|
||||
"""Load mock data from a JSON file."""
|
||||
with open("mock_data.json") as file:
|
||||
return json.load(file)
|
||||
|
||||
def _add_routes(self):
|
||||
"""Add routes to the router."""
|
||||
self.router.add_api_route("/", self.get_index, methods=["GET"], response_class=HTMLResponse)
|
||||
@@ -27,6 +22,10 @@ class CategoryController:
|
||||
methods=["GET"],
|
||||
response_class=HTMLResponse,
|
||||
)
|
||||
def _load_data(self, data_file):
|
||||
"""Load JSON data from a file."""
|
||||
with open(data_file, "r", encoding="utf-8") as file:
|
||||
return json.load(file)
|
||||
|
||||
async def get_index(self, request: Request):
|
||||
"""Index route."""
|
||||
|
||||
@@ -15,7 +15,7 @@ class DynamicController:
|
||||
|
||||
def _load_mock_data(self):
|
||||
"""Load mock data from a JSON file."""
|
||||
with open("mock_data.json") as file:
|
||||
with open("generated_data.json") as file:
|
||||
return json.load(file)
|
||||
|
||||
def _add_dynamic_routes(self):
|
||||
|
||||
10
app/main.py
10
app/main.py
@@ -18,16 +18,18 @@ class Application:
|
||||
async def _lifespan_event(self, app: FastAPI):
|
||||
"""Lifespan event for startup and shutdown logic."""
|
||||
print("App startup: Processing Markdown files...")
|
||||
# Process Markdown files into HTML
|
||||
processor = MarkdownProcessor(input_dir="./data", templates_dir="./templates")
|
||||
processor.run()
|
||||
|
||||
# Generate dynamic JSON data
|
||||
metadata_processor = MetadataProcessor(input_dir="./data", output_file="generated_data.json")
|
||||
metadata_processor.generate_json()
|
||||
print("Generated dynamic data file.")
|
||||
print("Markdown processing complete!")
|
||||
|
||||
# Process Markdown files into HTML
|
||||
processor = MarkdownProcessor(input_dir="./data", templates_dir="./templates")
|
||||
processor.run()
|
||||
|
||||
|
||||
|
||||
yield
|
||||
print("App shutdown: Cleanup complete.")
|
||||
|
||||
|
||||
@@ -72,12 +72,31 @@ class MetadataProcessor:
|
||||
|
||||
def generate_json(self):
|
||||
"""
|
||||
Generate the JSON structure and save it to the output file.
|
||||
Generate the JSON structure, deduplicate and sort categories by 'path',
|
||||
then save it to the output file.
|
||||
"""
|
||||
self._process_directory()
|
||||
self._process_directory() # Extract all markdown data into self.data
|
||||
|
||||
# Save JSON to file
|
||||
with open(self.output_file, "w", encoding="utf-8") as json_file:
|
||||
json.dump(self.data, json_file, indent=4, ensure_ascii=False)
|
||||
# Ensure 'categories' exists and is a list
|
||||
if "categories" not in self.data:
|
||||
self.data["categories"] = []
|
||||
|
||||
print(f"Generated JSON saved to {self.output_file}")
|
||||
# Deduplicate 'categories' using 'path' as the unique key
|
||||
unique_categories = { }
|
||||
for category in self.data["categories"]:
|
||||
if isinstance( category, dict ): # Ensure valid category structure
|
||||
path = category.get( "path", "unknown" ) # Use 'path' as the unique key
|
||||
if path not in unique_categories:
|
||||
unique_categories[path] = category
|
||||
|
||||
# Replace the 'categories' list with a sorted version by 'path'
|
||||
self.data["categories"] = sorted(
|
||||
unique_categories.values(),
|
||||
key = lambda x: x.get( "path", "unknown" )
|
||||
)
|
||||
|
||||
# Save the updated JSON to file
|
||||
with open( self.output_file, "w", encoding = "utf-8" ) as json_file:
|
||||
json.dump( self.data, json_file, indent = 4, ensure_ascii = False )
|
||||
|
||||
print( f"Generated JSON saved to {self.output_file}" )
|
||||
|
||||
Reference in New Issue
Block a user