|
2 | 2 | A full-stack application to visualize and edit entities and their relations defined in JSON Schema. |
3 | 3 |
|
4 | 4 |
|
| 5 | + |
| 6 | +*** |
| 7 | + |
| 8 | +# JSONED — Full Teaching Guide |
| 9 | + |
| 10 | +> **Goal:** Learn how to build and run a full-stack app to **visualize and edit JSON Schema entities** using **FastAPI** (backend) and **React** (frontend). Includes MongoDB integration as an advanced option. |
| 11 | +
|
| 12 | +*** |
| 13 | + |
| 14 | +## ✅ Quick Start |
| 15 | + |
| 16 | +### 1. Clone the repository |
| 17 | + |
| 18 | +```bash |
| 19 | +jsoned> git clone https://github.com/BAMresearch/jsoned.git |
| 20 | +jsoned> cd jsoned |
| 21 | +jsoned> dir # Windows |
| 22 | +jsoned> ls # Linux/macOS |
| 23 | +``` |
| 24 | + |
| 25 | +*** |
| 26 | + |
| 27 | +### 2. Backend Setup (Terminal 1) |
| 28 | + |
| 29 | +```bash |
| 30 | +jsoned> cd backend |
| 31 | +backend> python -m venv venv |
| 32 | +backend> source venv/bin/activate # Windows: venv\Scripts\activate |
| 33 | + |
| 34 | +backend(venv)> pip install -r requirements.txt |
| 35 | + |
| 36 | +backend(venv)> uvicorn main:app --reload |
| 37 | +# or specify host/port |
| 38 | +backend(venv)> uvicorn main:app --reload --host 127.0.0.1 --port 8000 |
| 39 | +``` |
| 40 | + |
| 41 | +#### If using `pyproject.toml`: |
| 42 | + |
| 43 | +```bash |
| 44 | +# using install -e . |
| 45 | +pip install -e . |
| 46 | +pip install -e .[dev] #when dev is defined |
| 47 | + |
| 48 | +# Check which tool is used (Poetry or Pipenv) |
| 49 | + |
| 50 | +# If Poetry: |
| 51 | +backend> pip install poetry |
| 52 | +backend> poetry install |
| 53 | +backend> poetry shell |
| 54 | + |
| 55 | +# If Pipenv: |
| 56 | +backend> pip install pipenv |
| 57 | +backend> pipenv install |
| 58 | +backend> pipenv shell |
| 59 | + |
| 60 | +# If standard PEP 621: |
| 61 | +backend> pip install . |
| 62 | +``` |
| 63 | + |
| 64 | +**Check:** Open `http://127.0.0.1:8000/docs` → FastAPI docs should appear. |
| 65 | + |
| 66 | +*** |
| 67 | + |
| 68 | +### 3. Frontend Setup (Terminal 2) |
| 69 | + |
| 70 | +**Create React app:** |
| 71 | + |
| 72 | +```bash |
| 73 | +jsoned> npx create-react-app gui |
| 74 | +jsoned> cd gui |
| 75 | +gui> dir # Windows |
| 76 | +gui> ls # Linux/macOS |
| 77 | +``` |
| 78 | + |
| 79 | +**Install Axios for API calls:** |
| 80 | + |
| 81 | +```bash |
| 82 | +gui> npm install axios |
| 83 | +gui> npm audit #Run audit details |
| 84 | +gui> npm audit fix #Fix automatically |
| 85 | + |
| 86 | +``` |
| 87 | + |
| 88 | +**Run frontend:** |
| 89 | + |
| 90 | +```bash |
| 91 | +gui> npm start # Standard start command |
| 92 | + |
| 93 | +#OR |
| 94 | +gui> npm run dev # Alias for developer mode |
| 95 | + |
| 96 | +INFO:To npm run dev DO THE FOLLOWING: |
| 97 | + in package.json add ->> "dev": "react-scripts start": |
| 98 | + |
| 99 | + "scripts": { |
| 100 | + "start": "react-scripts start", |
| 101 | + "build": "react-scripts build", |
| 102 | + "test": "react-scripts test", |
| 103 | + "eject": "react-scripts eject", |
| 104 | + "dev": "react-scripts start" |
| 105 | + } |
| 106 | + |
| 107 | +``` |
| 108 | + |
| 109 | +✔ **Difference?** |
| 110 | + |
| 111 | +* `npm start` → Runs the default development server. |
| 112 | +* `npm run dev` → Custom alias (same behavior here). |
| 113 | + |
| 114 | +📂 **Where are App.js and App.css?** |
| 115 | + |
| 116 | +* Located in `gui/src/`: |
| 117 | + * `App.js` → Main React component |
| 118 | + * `App.css` → Styling for App.js |
| 119 | + |
| 120 | +✅ **Check:** |
| 121 | + |
| 122 | +* `http://localhost:3000` |
| 123 | + |
| 124 | +*** |
| 125 | + |
| 126 | +## ✅ Architecture & Diagrams |
| 127 | + |
| 128 | +### **High-Level Architecture** |
| 129 | + |
| 130 | + +-------------------+ HTTP API +-------------------+ MongoDB |
| 131 | + | React Frontend | <--------------------> | FastAPI | <----> | Database | |
| 132 | + | (Components/UI) | | (Routes/Models) | | Schemas | |
| 133 | + +-------------------+ +-------------------+ +----------+ |
| 134 | + |
| 135 | +*** |
| 136 | + |
| 137 | +### **Detailed Component Flow** |
| 138 | + |
| 139 | + [User Action] --> [React Component] --> [Axios API Call] --> [FastAPI Endpoint] |
| 140 | + | | |
| 141 | + | v |
| 142 | + | [MongoDB Query] |
| 143 | + | | |
| 144 | + v v |
| 145 | + [Updated State] <-- [JSON Response] <-- [FastAPI Response] <-- [Database Result] |
| 146 | + |
| 147 | +*** |
| 148 | + |
| 149 | +### **Backend Layer Diagram** |
| 150 | + |
| 151 | + +-------------------+ |
| 152 | + | main.py | -> Routes & Controllers |
| 153 | + +-------------------+ |
| 154 | + | models.py | -> Pydantic Schemas |
| 155 | + +-------------------+ |
| 156 | + | database.py | -> MongoDB Connection |
| 157 | + +-------------------+ |
| 158 | + |
| 159 | +*** |
| 160 | + |
| 161 | +### **Frontend Folder Structure** |
| 162 | + |
| 163 | + gui/ |
| 164 | + ├── public/ |
| 165 | + ├── src/ |
| 166 | + │ ├── App.js # Main React Component |
| 167 | + │ ├── api.js # Axios API Calls |
| 168 | + │ ├── components/ # UI Components |
| 169 | + │ └── App.css # Styling |
| 170 | + |
| 171 | +*** |
| 172 | + |
| 173 | +### **Request Lifecycle** |
| 174 | + |
| 175 | + User Click -> React -> Axios -> FastAPI -> MongoDB -> Response -> React State Update |
| 176 | + |
| 177 | +*** |
| 178 | + |
| 179 | +### **MVC Mapping** |
| 180 | + |
| 181 | + Model -> Pydantic schemas (datamodel.py, model.py) |
| 182 | + View -> React components (App.jsx) |
| 183 | + Controller -> FastAPI routes (main.py) |
| 184 | + |
| 185 | +*** |
| 186 | + |
| 187 | +### **Sequence Flow (Add Version)** |
| 188 | + |
| 189 | + User -> React -> FastAPI -> In-memory DB |
| 190 | + | | | | |
| 191 | + | Click Add | | |
| 192 | + |---------------> | | |
| 193 | + | POST /version | |
| 194 | + | -> Append SchemaDefinition |
| 195 | + |
| 196 | +*** |
| 197 | + |
| 198 | +### **UML Class Diagram** |
| 199 | + |
| 200 | + +-------------------------+ |
| 201 | + | SchemaDefinition | |
| 202 | + +-------------------------+ |
| 203 | + | id: str | |
| 204 | + | name: str | |
| 205 | + | version: str | |
| 206 | + | content: dict | |
| 207 | + | updated_at: datetime | |
| 208 | + +-------------------------+ |
| 209 | + |
| 210 | + +-------------------------+ |
| 211 | + | UpdateSchema | |
| 212 | + +-------------------------+ |
| 213 | + | name: str | None | |
| 214 | + | version: str | None | |
| 215 | + +-------------------------+ |
| 216 | + |
| 217 | +*** |
| 218 | + |
| 219 | +### **MongoDB Integration Flow** |
| 220 | + |
| 221 | + +-----------+ Axios HTTP +-----------+ PyMongo +-----------+ |
| 222 | + | React | ---> POST /version -> | FastAPI | ---> Insert Doc -> | MongoDB | |
| 223 | + | Frontend | GET /version | Backend | Query Docs | Database | |
| 224 | + +-----------+ PATCH /version +-----------+ Return JSON +-----------+ |
| 225 | + |
| 226 | +*** |
| 227 | + |
| 228 | +## ✅ MongoDB Atlas Setup (Optional Advanced) |
| 229 | + |
| 230 | +1. Create account at <https://www.mongodb.com/atlas> |
| 231 | +2. Create cluster and get connection string |
| 232 | +3. Add `.env` in `backend`: |
| 233 | + |
| 234 | +<!----> |
| 235 | + |
| 236 | + MONGO_URI="your-atlas-uri" |
| 237 | + |
| 238 | +4. Use `pymongo` in `database.py`: |
| 239 | + |
| 240 | +```python |
| 241 | +from pymongo import MongoClient |
| 242 | +from dotenv import load_dotenv |
| 243 | +import os |
| 244 | + |
| 245 | +load_dotenv() |
| 246 | +client = MongoClient(os.getenv("MONGO_URI")) |
| 247 | +db = client.jsoned_db |
| 248 | +``` |
| 249 | + |
| 250 | +*** |
| 251 | + |
| 252 | +## ✅ API Endpoints |
| 253 | + |
| 254 | + GET /version |
| 255 | + POST /version |
| 256 | + PUT /version/{id} |
| 257 | + PATCH /version/{id} |
| 258 | + DELETE /version/{id} |
| 259 | + |
| 260 | +*** |
| 261 | + |
| 262 | +## ✅ Best Practices |
| 263 | + |
| 264 | +* Use `.env` for secrets |
| 265 | +* Enable CORS for frontend |
| 266 | +* Keep code modular |
| 267 | + |
| 268 | +*** |
| 269 | + |
| 270 | +## ✅ Troubleshooting |
| 271 | + |
| 272 | +* **CORS errors**: Add `CORSMiddleware` |
| 273 | +* **npm issues**: Delete `node_modules` → `npm install` |
| 274 | +* **Port conflicts**: Change port in `uvicorn` or `npm run dev` |
| 275 | + |
| 276 | +*** |
| 277 | + |
| 278 | +## ✅ Learning Tasks |
| 279 | + |
| 280 | +1. Add a form in React to create a new schema |
| 281 | +2. Add delete button |
| 282 | +3. Persist data in MongoDB |
| 283 | + |
| 284 | +*** |
| 285 | + |
| 286 | +## ✅ Resources |
| 287 | + |
| 288 | +* <https://fastapi.tiangolo.com/> |
| 289 | +* <https://docs.pydantic.dev/> |
| 290 | +* <https://react.dev/> |
| 291 | +* <https://www.mongodb.com/docs/> |
| 292 | + |
| 293 | +*** |
0 commit comments