{ "cells": [ { "cell_type": "code", "execution_count": 2, "id": "f8f72ea7-273c-49da-8af7-09e9fa934b83", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Collecting gradio\n", " Downloading gradio-5.47.0-py3-none-any.whl.metadata (16 kB)\n", "Collecting aiofiles<25.0,>=22.0 (from gradio)\n", " Using cached aiofiles-24.1.0-py3-none-any.whl.metadata (10 kB)\n", "Requirement already satisfied: anyio<5.0,>=3.0 in /opt/conda/lib/python3.11/site-packages (from gradio) (4.2.0)\n", "Requirement already satisfied: brotli>=1.1.0 in /opt/conda/lib/python3.11/site-packages (from gradio) (1.1.0)\n", "Collecting fastapi<1.0,>=0.115.2 (from gradio)\n", " Downloading fastapi-0.117.1-py3-none-any.whl.metadata (28 kB)\n", "Collecting ffmpy (from gradio)\n", " Using cached ffmpy-0.6.1-py3-none-any.whl.metadata (2.9 kB)\n", "Collecting gradio-client==1.13.2 (from gradio)\n", " Downloading gradio_client-1.13.2-py3-none-any.whl.metadata (7.1 kB)\n", "Collecting groovy~=0.1 (from gradio)\n", " Using cached groovy-0.1.2-py3-none-any.whl.metadata (6.1 kB)\n", "Collecting httpx<1.0,>=0.24.1 (from gradio)\n", " Using cached httpx-0.28.1-py3-none-any.whl.metadata (7.1 kB)\n", "Collecting huggingface-hub<1.0,>=0.33.5 (from gradio)\n", " Downloading huggingface_hub-0.35.1-py3-none-any.whl.metadata (14 kB)\n", "Requirement already satisfied: jinja2<4.0 in /opt/conda/lib/python3.11/site-packages (from gradio) (3.1.3)\n", "Requirement already satisfied: markupsafe<4.0,>=2.0 in /opt/conda/lib/python3.11/site-packages (from gradio) (2.1.3)\n", "Requirement already satisfied: numpy<3.0,>=1.0 in /opt/conda/lib/python3.11/site-packages (from gradio) (1.26.3)\n", "Collecting orjson~=3.0 (from gradio)\n", " Using cached orjson-3.11.3-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (41 kB)\n", "Requirement already satisfied: packaging in /opt/conda/lib/python3.11/site-packages (from gradio) (23.2)\n", "Requirement already satisfied: pandas<3.0,>=1.0 in /opt/conda/lib/python3.11/site-packages (from gradio) (2.1.4)\n", "Requirement already satisfied: pillow<12.0,>=8.0 in /opt/conda/lib/python3.11/site-packages (from gradio) (10.2.0)\n", "Collecting pydantic<2.12,>=2.0 (from gradio)\n", " Downloading pydantic-2.11.9-py3-none-any.whl.metadata (68 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m68.4/68.4 kB\u001b[0m \u001b[31m2.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hCollecting pydub (from gradio)\n", " Using cached pydub-0.25.1-py2.py3-none-any.whl.metadata (1.4 kB)\n", "Collecting python-multipart>=0.0.18 (from gradio)\n", " Using cached python_multipart-0.0.20-py3-none-any.whl.metadata (1.8 kB)\n", "Requirement already satisfied: pyyaml<7.0,>=5.0 in /opt/conda/lib/python3.11/site-packages (from gradio) (6.0.1)\n", "Collecting ruff>=0.9.3 (from gradio)\n", " Downloading ruff-0.13.1-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (25 kB)\n", "Collecting safehttpx<0.2.0,>=0.1.6 (from gradio)\n", " Using cached safehttpx-0.1.6-py3-none-any.whl.metadata (4.2 kB)\n", "Collecting semantic-version~=2.0 (from gradio)\n", " Using cached semantic_version-2.10.0-py2.py3-none-any.whl.metadata (9.7 kB)\n", "Collecting starlette<1.0,>=0.40.0 (from gradio)\n", " Downloading starlette-0.48.0-py3-none-any.whl.metadata (6.3 kB)\n", "Collecting tomlkit<0.14.0,>=0.12.0 (from gradio)\n", " Using cached tomlkit-0.13.3-py3-none-any.whl.metadata (2.8 kB)\n", "Collecting typer<1.0,>=0.12 (from gradio)\n", " Downloading typer-0.19.2-py3-none-any.whl.metadata (16 kB)\n", "Requirement already satisfied: typing-extensions~=4.0 in /opt/conda/lib/python3.11/site-packages (from gradio) (4.9.0)\n", "Collecting uvicorn>=0.14.0 (from gradio)\n", " Downloading uvicorn-0.37.0-py3-none-any.whl.metadata (6.6 kB)\n", "Requirement already satisfied: fsspec in /opt/conda/lib/python3.11/site-packages (from gradio-client==1.13.2->gradio) (2023.12.2)\n", "Collecting websockets<16.0,>=13.0 (from gradio-client==1.13.2->gradio)\n", " Downloading websockets-15.0.1-cp311-cp311-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (6.8 kB)\n", "Requirement already satisfied: idna>=2.8 in /opt/conda/lib/python3.11/site-packages (from anyio<5.0,>=3.0->gradio) (3.6)\n", "Requirement already satisfied: sniffio>=1.1 in /opt/conda/lib/python3.11/site-packages (from anyio<5.0,>=3.0->gradio) (1.3.0)\n", "Requirement already satisfied: certifi in /opt/conda/lib/python3.11/site-packages (from httpx<1.0,>=0.24.1->gradio) (2024.2.2)\n", "Collecting httpcore==1.* (from httpx<1.0,>=0.24.1->gradio)\n", " Using cached httpcore-1.0.9-py3-none-any.whl.metadata (21 kB)\n", "Collecting h11>=0.16 (from httpcore==1.*->httpx<1.0,>=0.24.1->gradio)\n", " Using cached h11-0.16.0-py3-none-any.whl.metadata (8.3 kB)\n", "Collecting filelock (from huggingface-hub<1.0,>=0.33.5->gradio)\n", " Using cached filelock-3.19.1-py3-none-any.whl.metadata (2.1 kB)\n", "Requirement already satisfied: requests in /opt/conda/lib/python3.11/site-packages (from huggingface-hub<1.0,>=0.33.5->gradio) (2.31.0)\n", "Requirement already satisfied: tqdm>=4.42.1 in /opt/conda/lib/python3.11/site-packages (from huggingface-hub<1.0,>=0.33.5->gradio) (4.66.1)\n", "Collecting hf-xet<2.0.0,>=1.1.3 (from huggingface-hub<1.0,>=0.33.5->gradio)\n", " Downloading hf_xet-1.1.10-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (4.7 kB)\n", "Requirement already satisfied: python-dateutil>=2.8.2 in /opt/conda/lib/python3.11/site-packages (from pandas<3.0,>=1.0->gradio) (2.8.2)\n", "Requirement already satisfied: pytz>=2020.1 in /opt/conda/lib/python3.11/site-packages (from pandas<3.0,>=1.0->gradio) (2023.3.post1)\n", "Requirement already satisfied: tzdata>=2022.1 in /opt/conda/lib/python3.11/site-packages (from pandas<3.0,>=1.0->gradio) (2023.4)\n", "Collecting annotated-types>=0.6.0 (from pydantic<2.12,>=2.0->gradio)\n", " Using cached annotated_types-0.7.0-py3-none-any.whl.metadata (15 kB)\n", "Collecting pydantic-core==2.33.2 (from pydantic<2.12,>=2.0->gradio)\n", " Using cached pydantic_core-2.33.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (6.8 kB)\n", "Collecting typing-extensions~=4.0 (from gradio)\n", " Using cached typing_extensions-4.15.0-py3-none-any.whl.metadata (3.3 kB)\n", "Collecting typing-inspection>=0.4.0 (from pydantic<2.12,>=2.0->gradio)\n", " Using cached typing_inspection-0.4.1-py3-none-any.whl.metadata (2.6 kB)\n", "Requirement already satisfied: click>=8.0.0 in /opt/conda/lib/python3.11/site-packages (from typer<1.0,>=0.12->gradio) (8.1.7)\n", "Collecting shellingham>=1.3.0 (from typer<1.0,>=0.12->gradio)\n", " Using cached shellingham-1.5.4-py2.py3-none-any.whl.metadata (3.5 kB)\n", "Collecting rich>=10.11.0 (from typer<1.0,>=0.12->gradio)\n", " Using cached rich-14.1.0-py3-none-any.whl.metadata (18 kB)\n", "Requirement already satisfied: six>=1.5 in /opt/conda/lib/python3.11/site-packages (from python-dateutil>=2.8.2->pandas<3.0,>=1.0->gradio) (1.16.0)\n", "Collecting markdown-it-py>=2.2.0 (from rich>=10.11.0->typer<1.0,>=0.12->gradio)\n", " Using cached markdown_it_py-4.0.0-py3-none-any.whl.metadata (7.3 kB)\n", "Requirement already satisfied: pygments<3.0.0,>=2.13.0 in /opt/conda/lib/python3.11/site-packages (from rich>=10.11.0->typer<1.0,>=0.12->gradio) (2.17.2)\n", "Requirement already satisfied: charset-normalizer<4,>=2 in /opt/conda/lib/python3.11/site-packages (from requests->huggingface-hub<1.0,>=0.33.5->gradio) (3.3.2)\n", "Requirement already satisfied: urllib3<3,>=1.21.1 in /opt/conda/lib/python3.11/site-packages (from requests->huggingface-hub<1.0,>=0.33.5->gradio) (2.1.0)\n", "Collecting mdurl~=0.1 (from markdown-it-py>=2.2.0->rich>=10.11.0->typer<1.0,>=0.12->gradio)\n", " Using cached mdurl-0.1.2-py3-none-any.whl.metadata (1.6 kB)\n", "Downloading gradio-5.47.0-py3-none-any.whl (60.4 MB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m60.4/60.4 MB\u001b[0m \u001b[31m51.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m:00:01\u001b[0m00:01\u001b[0m\n", "\u001b[?25hDownloading gradio_client-1.13.2-py3-none-any.whl (325 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m325.3/325.3 kB\u001b[0m \u001b[31m8.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m:00:01\u001b[0m\n", "\u001b[?25hUsing cached aiofiles-24.1.0-py3-none-any.whl (15 kB)\n", "Downloading fastapi-0.117.1-py3-none-any.whl (95 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m96.0/96.0 kB\u001b[0m \u001b[31m3.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hUsing cached groovy-0.1.2-py3-none-any.whl (14 kB)\n", "Using cached httpx-0.28.1-py3-none-any.whl (73 kB)\n", "Using cached httpcore-1.0.9-py3-none-any.whl (78 kB)\n", "Downloading huggingface_hub-0.35.1-py3-none-any.whl (563 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m563.3/563.3 kB\u001b[0m \u001b[31m17.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hUsing cached orjson-3.11.3-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (132 kB)\n", "Downloading pydantic-2.11.9-py3-none-any.whl (444 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m444.9/444.9 kB\u001b[0m \u001b[31m16.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hUsing cached pydantic_core-2.33.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.0 MB)\n", "Using cached python_multipart-0.0.20-py3-none-any.whl (24 kB)\n", "Downloading ruff-0.13.1-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (13.2 MB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m13.2/13.2 MB\u001b[0m \u001b[31m72.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m:00:01\u001b[0m0:01\u001b[0m\n", "\u001b[?25hUsing cached safehttpx-0.1.6-py3-none-any.whl (8.7 kB)\n", "Using cached semantic_version-2.10.0-py2.py3-none-any.whl (15 kB)\n", "Downloading starlette-0.48.0-py3-none-any.whl (73 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m73.7/73.7 kB\u001b[0m \u001b[31m2.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hUsing cached tomlkit-0.13.3-py3-none-any.whl (38 kB)\n", "Downloading typer-0.19.2-py3-none-any.whl (46 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m46.7/46.7 kB\u001b[0m \u001b[31m1.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hUsing cached typing_extensions-4.15.0-py3-none-any.whl (44 kB)\n", "Downloading uvicorn-0.37.0-py3-none-any.whl (67 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m68.0/68.0 kB\u001b[0m \u001b[31m2.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hUsing cached ffmpy-0.6.1-py3-none-any.whl (5.5 kB)\n", "Using cached pydub-0.25.1-py2.py3-none-any.whl (32 kB)\n", "Using cached annotated_types-0.7.0-py3-none-any.whl (13 kB)\n", "Using cached h11-0.16.0-py3-none-any.whl (37 kB)\n", "Downloading hf_xet-1.1.10-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.2 MB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m3.2/3.2 MB\u001b[0m \u001b[31m61.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m:00:01\u001b[0m\n", "\u001b[?25hUsing cached rich-14.1.0-py3-none-any.whl (243 kB)\n", "Using cached shellingham-1.5.4-py2.py3-none-any.whl (9.8 kB)\n", "Using cached typing_inspection-0.4.1-py3-none-any.whl (14 kB)\n", "Downloading websockets-15.0.1-cp311-cp311-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (182 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m182.3/182.3 kB\u001b[0m \u001b[31m6.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hUsing cached filelock-3.19.1-py3-none-any.whl (15 kB)\n", "Using cached markdown_it_py-4.0.0-py3-none-any.whl (87 kB)\n", "Using cached mdurl-0.1.2-py3-none-any.whl (10.0 kB)\n", "Installing collected packages: pydub, websockets, typing-extensions, tomlkit, shellingham, semantic-version, ruff, python-multipart, orjson, mdurl, hf-xet, h11, groovy, filelock, ffmpy, annotated-types, aiofiles, uvicorn, typing-inspection, starlette, pydantic-core, markdown-it-py, huggingface-hub, httpcore, rich, pydantic, httpx, typer, safehttpx, gradio-client, fastapi, gradio\n", " Attempting uninstall: websockets\n", " Found existing installation: websockets 12.0\n", " Uninstalling websockets-12.0:\n", " Successfully uninstalled websockets-12.0\n", " Attempting uninstall: typing-extensions\n", " Found existing installation: typing_extensions 4.9.0\n", " Uninstalling typing_extensions-4.9.0:\n", " Successfully uninstalled typing_extensions-4.9.0\n", "Successfully installed aiofiles-24.1.0 annotated-types-0.7.0 fastapi-0.117.1 ffmpy-0.6.1 filelock-3.19.1 gradio-5.47.0 gradio-client-1.13.2 groovy-0.1.2 h11-0.16.0 hf-xet-1.1.10 httpcore-1.0.9 httpx-0.28.1 huggingface-hub-0.35.1 markdown-it-py-4.0.0 mdurl-0.1.2 orjson-3.11.3 pydantic-2.11.9 pydantic-core-2.33.2 pydub-0.25.1 python-multipart-0.0.20 rich-14.1.0 ruff-0.13.1 safehttpx-0.1.6 semantic-version-2.10.0 shellingham-1.5.4 starlette-0.48.0 tomlkit-0.13.3 typer-0.19.2 typing-extensions-4.15.0 typing-inspection-0.4.1 uvicorn-0.37.0 websockets-15.0.1\n" ] } ], "source": [ "# To install gradio into the notebook environment\n", "!pip install gradio" ] }, { "cell_type": "code", "execution_count": 4, "id": "e15773a1-3e7f-4d09-b777-124247954e35", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "📂 Success: Loaded 0 lighthouses from lighthouses.json.\n", "* Running on local URL: http://127.0.0.1:7861\n", "* Running on public URL: https://f80ff2f7140868d8d0.gradio.live\n", "\n", "This share link expires in 1 week. For free permanent hosting and GPU upgrades, run `gradio deploy` from the terminal in the working directory to deploy to Hugging Face Spaces (https://huggingface.co/spaces)\n" ] }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "\n", "# To run this app:\n", "# 1. Install Gradio: !pip install gradio\n", "# 2. Run from your terminal: python lighthouse.py\n", "\n", "import gradio as gr\n", "import json\n", "from datetime import date\n", "\n", "# ===================================================================================\n", "# PROJECT CODE STARTS HERE\n", "# Students will modify the code below this line.\n", "# ===================================================================================\n", "\n", "# ### WEEK 1: The Blueprint - Modeling a Lighthouse ###\n", "# Focus: Abstraction, Automation, Analysis\n", "# -----------------------------------------------------------------------------------\n", "# TODO: Implement the Lighthouse data model.\n", "# - Use a class to represent a lighthouse.\n", "# - Add validation logic inside its __init__ method.\n", "\n", "class Lighthouse:\n", " \"\"\"Represents a single lighthouse with validation.\"\"\"\n", " def __init__(self, name: str, location: str, is_operational: bool, last_inspected_date: str):\n", " # Validate name: must be a non-empty string\n", " if not isinstance(name, str) or not name.strip():\n", " raise ValueError(\"Name must be a non-empty string.\")\n", " # Validate location: must be a non-empty string\n", " if not isinstance(location, str) or not location.strip():\n", " raise ValueError(\"Location must be a non-empty string.\")\n", " # Validate is_operational: must be a boolean\n", " if not isinstance(is_operational, bool):\n", " raise TypeError(\"Is Operational must be a boolean (True/False).\")\n", " # Validate date: must be in 'YYYY-MM-DD' format\n", " try:\n", " parsed_date = date.fromisoformat(last_inspected_date)\n", " except (ValueError, TypeError):\n", " raise ValueError(\"Last Inspected Date must be in YYYY-MM-DD format.\")\n", "\n", " self.name = name.strip()\n", " self.location = location.strip()\n", " self.is_operational = is_operational\n", " self.last_inspected_date = parsed_date\n", "\n", " def to_dict(self):\n", " \"\"\"Converts the object to a dictionary for JSON serialization.\"\"\"\n", " return {\n", " \"name\": self.name,\n", " \"location\": self.location,\n", " \"is_operational\": self.is_operational,\n", " \"last_inspected_date\": self.last_inspected_date.isoformat()\n", " }\n", "\n", " def __repr__(self):\n", " \"\"\"Provides a developer-friendly string representation of the object.\"\"\"\n", " status = \"Operational\" if self.is_operational else \"Non-Operational\"\n", " return f\"✅ {self.name} ({self.location}) - Status: {status}, Last Inspected: {self.last_inspected_date.isoformat()}\"\n", "\n", "\n", "# ### WEEK 5: Scaling Up - The Lighthouse Authority ###\n", "# Focus: Abstraction, Automation, Analysis\n", "# -----------------------------------------------------------------------------------\n", "# TODO: Refactor all functions into this LighthouseRegistry class.\n", "\n", "class LighthouseRegistry:\n", " \"\"\"Manages the collection of all lighthouses.\"\"\"\n", " def __init__(self, filepath=\"lighthouses.json\"):\n", " self.lighthouses = []\n", " self.filepath = filepath\n", "\n", " # ### WEEK 2: First Steps - The Lighthouse Logbook ###\n", " # TODO: Implement the add_lighthouse method.\n", " def add_lighthouse(self, name: str, location: str, is_operational: bool, last_inspected_date: str):\n", " \"\"\"Creates and adds a new Lighthouse to the registry after validation.\"\"\"\n", " try:\n", " new_lighthouse = Lighthouse(name, location, is_operational, last_inspected_date)\n", " self.lighthouses.append(new_lighthouse)\n", " return f\"Success: Added '{name}' to the registry.\"\n", " except (ValueError, TypeError) as e:\n", " return f\"❌ Error: Could not add lighthouse. {e}\"\n", "\n", " # ### WEEK 3: Making Sense of the Data - Fleet Status Reports ###\n", " # TODO: Implement the generate_status_report method for ANALYSIS.\n", " def generate_status_report(self):\n", " \"\"\"Analyzes the current list of lighthouses and generates a summary report.\"\"\"\n", " if not self.lighthouses:\n", " return \"Cannot generate report: No lighthouses in the registry.\"\n", "\n", " total = len(self.lighthouses)\n", " operational_count = sum(1 for lh in self.lighthouses if lh.is_operational)\n", " non_operational_count = total - operational_count\n", "\n", " most_recent_lighthouse = max(self.lighthouses, key=lambda lh: lh.last_inspected_date)\n", "\n", " report = (\n", " \"📊 ===== Lighthouse Fleet Status Report ===== 📊\\n\"\n", " f\"Total Lighthouses: {total}\\n\"\n", " f\" - Operational: {operational_count}\\n\"\n", " f\" - Non-Operational: {non_operational_count}\\n\\n\"\n", " f\"Most Recently Inspected:\\n\"\n", " f\" - Name: {most_recent_lighthouse.name}\\n\"\n", " f\" - Date: {most_recent_lighthouse.last_inspected_date.isoformat()}\\n\"\n", " \"==========================================\"\n", " )\n", " return report\n", "\n", " def list_lighthouses(self):\n", " \"\"\"Returns a formatted string listing all lighthouses in the registry.\"\"\"\n", " if not self.lighthouses:\n", " return \"No lighthouses in the registry.\"\n", "\n", " header = \"--- List of All Lighthouses ---\"\n", " # Use the __repr__ from the Lighthouse class for a clean format\n", " report_lines = [repr(lh) for lh in self.lighthouses]\n", " return f\"{header}\\n\" + \"\\n\".join(report_lines)\n", "\n", "\n", " # ### WEEK 4: Making It Real - The Official Record ###\n", " # TODO: Implement save_to_file and load_from_file to AUTOMATE persistence.\n", " def save_to_file(self):\n", " \"\"\"Saves the current list of lighthouses to the JSON file.\"\"\"\n", " try:\n", " # Convert each Lighthouse object to its dictionary representation\n", " data_to_save = [lh.to_dict() for lh in self.lighthouses]\n", " with open(self.filepath, 'w') as f:\n", " json.dump(data_to_save, f, indent=4)\n", " return f\"💾 Success: Saved {len(self.lighthouses)} lighthouses to {self.filepath}.\"\n", " except (IOError, TypeError) as e:\n", " return f\"❌ Error: Could not save to file. {e}\"\n", "\n", " def load_from_file(self):\n", " \"\"\"Loads lighthouses from the JSON file, recreating the objects.\"\"\"\n", " try:\n", " with open(self.filepath, 'r') as f:\n", " data = json.load(f)\n", " self.lighthouses = [] # Clear the current list before loading\n", " for item in data:\n", " # The Lighthouse constructor handles validation of the loaded data\n", " lh = Lighthouse(**item)\n", " self.lighthouses.append(lh)\n", " return f\"📂 Success: Loaded {len(self.lighthouses)} lighthouses from {self.filepath}.\"\n", " except FileNotFoundError:\n", " return f\"ℹ️ Info: No save file found at '{self.filepath}'. Starting with an empty registry.\"\n", " except (json.JSONDecodeError, ValueError, TypeError) as e:\n", " # Catches corrupted JSON, or data that fails Lighthouse validation\n", " return f\"❌ Error: Could not load from file. File may be corrupt or malformed. {e}\"\n", "\n", "\n", "# ===================================================================================\n", "# GRADIO UI CODE\n", "# Students should not need to modify below this line initially,\n", "# but can extend it in Week 5.\n", "# ===================================================================================\n", "\n", "def create_ui(registry: LighthouseRegistry):\n", " \"\"\"Creates the Gradio web interface.\"\"\"\n", " # Helper functions to interact with the registry and update the UI\n", " def add_and_update(name, location, is_op, date_str, current_log):\n", " result = registry.add_lighthouse(name, location, is_op, date_str)\n", " new_log = f\"{current_log}\\n> {result}\"\n", " return new_log\n", "\n", " def report_and_update(current_log):\n", " result = registry.generate_status_report()\n", " new_log = f\"{current_log}\\n\\n{result}\\n\"\n", " return new_log\n", "\n", " def list_and_update(current_log):\n", " result = registry.list_lighthouses()\n", " new_log = f\"{current_log}\\n\\n{result}\\n\"\n", " return new_log\n", "\n", " def save_and_update(current_log):\n", " result = registry.save_to_file()\n", " new_log = f\"{current_log}\\n> {result}\"\n", " return new_log\n", "\n", " def load_and_update(current_log):\n", " result = registry.load_from_file()\n", " new_log = f\"{current_log}\\n> {result}\"\n", " # After loading, also list the lighthouses to show what was loaded\n", " list_result = registry.list_lighthouses()\n", " return f\"{new_log}\\n\\n{list_result}\\n\"\n", "\n", " # Define the Gradio interface layout\n", " with gr.Blocks(theme=gr.themes.Monochrome(), title=\"Lighthouse Lookout\") as app:\n", " gr.Markdown(\"# 🚢 Lighthouse Lookout\")\n", " gr.Markdown(\"A tool for monitoring and managing a fleet of lighthouses.\")\n", " log_textbox = gr.Textbox(\n", " label=\"Log & Reports\",\n", " value=\"Welcome! Your command results will appear here.\",\n", " lines=15,\n", " interactive=False,\n", " elem_classes=\"terminal\" # Just for potential styling\n", " )\n", " with gr.Row():\n", " report_btn = gr.Button(\"📊 Generate Status Report\")\n", " list_btn = gr.Button(\"📋 List All Lighthouses\")\n", "\n", " with gr.Accordion(\"➕ Add a New Lighthouse\", open=False):\n", " with gr.Row():\n", " name_input = gr.Textbox(label=\"Name\", placeholder=\"e.g., Trinity Lighthouse\")\n", " location_input = gr.Textbox(label=\"Location\", placeholder=\"e.g., 50.18, -0.16\")\n", " with gr.Row():\n", " is_operational_input = gr.Checkbox(label=\"Is Operational?\", value=True)\n", " # Using a string for the date to align with the Lighthouse class constructor\n", " date_input = gr.Textbox(label=\"Last Inspected Date\", placeholder=\"YYYY-MM-DD\")\n", " add_btn = gr.Button(\"Add Lighthouse\")\n", "\n", " with gr.Row():\n", " save_btn = gr.Button(\"💾 Save Registry\")\n", " load_btn = gr.Button(\"📂 Load Registry\")\n", "\n", " # Connect UI components to the helper functions\n", " add_btn.click(\n", " fn=add_and_update,\n", " inputs=[name_input, location_input, is_operational_input, date_input, log_textbox],\n", " outputs=[log_textbox]\n", " )\n", " report_btn.click(fn=report_and_update, inputs=[log_textbox], outputs=[log_textbox])\n", " list_btn.click(fn=list_and_update, inputs=[log_textbox], outputs=[log_textbox])\n", " save_btn.click(fn=save_and_update, inputs=[log_textbox], outputs=[log_textbox])\n", " load_btn.click(fn=load_and_update, inputs=[log_textbox], outputs=[log_textbox])\n", "\n", " return app\n", "\n", "# --- Main execution block ---\n", "if __name__ == \"__main__\":\n", " # Create a single instance of our registry that the app will use.\n", " lighthouse_registry = LighthouseRegistry()\n", " # Load any existing data on startup\n", " initial_load_message = lighthouse_registry.load_from_file()\n", " print(initial_load_message) # Print to terminal for the developer\n", " # Create and launch the Gradio app\n", " web_app = create_ui(lighthouse_registry)\n", " web_app.launch(share=True)" ] }, { "cell_type": "code", "execution_count": null, "id": "6df3b922-6bd8-49c1-99d7-8bfd8f791337", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.7" } }, "nbformat": 4, "nbformat_minor": 5 }