Skip to content

Commit e721fe4

Browse files
authored
refactor: frontend
refactor: frontend
2 parents 34fef72 + 247d72c commit e721fe4

File tree

7 files changed

+57
-40
lines changed

7 files changed

+57
-40
lines changed

src/moin_moin/backend/_db.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
from __future__ import annotations
22

3+
from datetime import UTC
4+
from datetime import datetime
5+
36
from sqlmodel import Field
47
from sqlmodel import SQLModel
58

@@ -10,7 +13,7 @@ class UserUploadData(SQLModel, table=True):
1013
latitude: float
1114
longitude: float
1215
notes: str
13-
tags: str
16+
timestamp: datetime = Field(default=datetime.now(tz=UTC))
1417

1518

1619
class Prediction(SQLModel, table=True):
@@ -23,5 +26,5 @@ class PublicRecord(SQLModel):
2326
latitude: float
2427
longitude: float
2528
notes: str
26-
tags: str
2729
prediction: str
30+
timestamp: datetime

src/moin_moin/backend/api.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,11 @@ def root() -> dict[str, str]:
7575

7676

7777
@app.post("/save")
78-
async def save( # noqa: PLR0913
78+
async def save(
7979
session: SessionDep,
8080
latitude: Annotated[float, Form()],
8181
longitude: Annotated[float, Form()],
8282
notes: Annotated[str, Form()],
83-
tags: Annotated[str, Form()],
8483
image_bytes: Annotated[UploadFile, File()],
8584
) -> dict[str, int | None]:
8685
"""Save the input data into database records into user upload data table."""
@@ -90,7 +89,6 @@ async def save( # noqa: PLR0913
9089
latitude=latitude,
9190
longitude=longitude,
9291
notes=notes,
93-
tags=tags,
9492
)
9593

9694
session.add(record)
@@ -128,7 +126,7 @@ async def load_records(session: SessionDep) -> list[PublicRecord]:
128126
UserUploadData.latitude,
129127
UserUploadData.longitude,
130128
UserUploadData.notes,
131-
UserUploadData.tags,
129+
UserUploadData.timestamp,
132130
Prediction.prediction,
133131
).join(Prediction)
134132
records = session.exec(statement).all()

src/moin_moin/frontend/_conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
HOST = "localhost" if ENV in ("local", "dev") else "backend"
88
BACKEND_URL: Final[str] = f"http://{HOST}:8081"
99

10-
INSTITUTION_MAPPING: Final[dict[str, str]] = {
10+
INSTITUTION_COLOR_MAPPING: Final[dict[str, str]] = {
1111
"Police Department": "#48b5a5",
1212
"Fire Department": "#7b64ab",
1313
"Hospital": "#20be64",
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from __future__ import annotations
2+
3+
import httpx
4+
import pandas as pd
5+
import streamlit as st
6+
7+
from moin_moin.frontend._conf import BACKEND_URL
8+
from moin_moin.frontend._conf import INSTITUTION_COLOR_MAPPING
9+
10+
11+
def issue_tracker_page() -> None:
12+
"""Create the issue tracker page."""
13+
st.sidebar.title("Issue tracker")
14+
st.sidebar.markdown("This page displays all the issues in the database in a map and on a table")
15+
16+
result = (
17+
pd.DataFrame(httpx.get(f"{BACKEND_URL}/load-records").json())
18+
.drop_duplicates()
19+
.assign(_color=lambda t: t["prediction"].map(INSTITUTION_COLOR_MAPPING))
20+
)
21+
22+
st.header(f"Found {result.shape[0]} issues")
23+
24+
left_column, right_column = st.columns(2)
25+
26+
left_column.map(data=result, latitude="latitude", longitude="longitude", color="_color", size=10)
27+
right_column.dataframe(
28+
data=(
29+
result[["prediction", "notes", "timestamp"]].rename(
30+
columns={"prediction": "Assigned to", "notes": "Notes", "timestamp": "Report created"}
31+
)
32+
),
33+
hide_index=True,
34+
)
35+
36+
37+
issue_tracker_page()

src/moin_moin/frontend/_overview.py

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/moin_moin/frontend/_user_input.py renamed to src/moin_moin/frontend/_report_an_issue_page.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,19 @@ def parse_location(raw_loc: str) -> None | Any: # noqa: ANN401
3131
return GEOLOCATOR.geocode(raw_loc)
3232

3333

34-
def main() -> None:
34+
def user_input_page() -> None:
3535
"""Render user input page."""
36-
st.sidebar.title("Tell us about the issue...")
36+
st.sidebar.title("Tell us about your issue!")
37+
st.sidebar.markdown(
38+
"This page allows a citizen to report issues found around a city by just uploading an image and location."
39+
)
3740

3841
raw_image = st.sidebar.file_uploader(
3942
label="Upload an image",
4043
type=["png", "jpg"],
4144
)
4245

43-
raw_loc = st.sidebar.text_input("Enter your location (e.g., city name, street, number):")
44-
45-
tags = st.sidebar.multiselect("Select tags for the image:", TAGS)
46+
raw_loc = st.sidebar.text_input("Enter your location:", placeholder="Street, number, city name")
4647

4748
notes = st.sidebar.text_area(
4849
label="Additional information",
@@ -68,7 +69,6 @@ def main() -> None:
6869
"latitude": getattr(loc, "latitude", None),
6970
"longitude": getattr(loc, "longitude", None),
7071
"notes": notes,
71-
"tags": ",".join(tags) if tags else "",
7272
},
7373
files={"image_bytes": ("image.jpg", buffer, "image/jpeg")},
7474
timeout=10,
@@ -83,9 +83,7 @@ def main() -> None:
8383

8484
st.header(f"Assigned Institution: {result}")
8585
st.write("---")
86-
left_column, right_column = st.columns(
87-
2,
88-
)
86+
left_column, right_column = st.columns(2)
8987
if loc:
9088
right_column.map(data=pd.DataFrame({"lat": [loc.latitude], "lon": [loc.longitude]}))
9189
right_column.write(loc)
@@ -95,4 +93,4 @@ def main() -> None:
9593
left_column.image(image)
9694

9795

98-
main()
96+
user_input_page()

src/moin_moin/frontend/app.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
import streamlit as st
66

77
st.set_page_config(page_title="Smart cities", layout="wide", page_icon="🏙️")
8-
create_page = st.Page("_user_input.py", title="Upload Issues", icon="🔧")
9-
overview = st.Page("_overview.py", title="Issue Tracker", icon="🗺️")
8+
st.title("🏡 Moin Moin")
9+
report_an_issue_page = st.Page("_report_an_issue_page.py", title="Report an issue", icon="🔧")
10+
issue_tracker_page = st.Page("_issue_tracker_page.py", title="Issue tracker", icon="🗺️")
1011

11-
pg = st.navigation([create_page, overview])
12+
pg = st.navigation([report_an_issue_page, issue_tracker_page])
1213
pg.run()

0 commit comments

Comments
 (0)