feat: option to show likes, dislikes or both

This commit is contained in:
2025-02-12 17:54:07 +01:00
parent 06fd18ef4c
commit d37c6f7158
3 changed files with 42 additions and 15 deletions

View File

@@ -49,7 +49,7 @@ def sociogram_json():
return JSONResponse({"nodes": nodes, "links": links})
def sociogram_data(dislike: bool | None = False):
def sociogram_data(show: int | None = 2):
G = nx.DiGraph()
with Session(engine) as session:
for p in session.exec(select(P)).fetchall():
@@ -62,13 +62,14 @@ def sociogram_data(dislike: bool | None = False):
)
statement2 = (
select(C)
.where(C.user.in_(["Kruse", "Franz", "ck"]))
# .where(C.user.in_(["Kruse", "Franz", "ck"]))
.join(subquery, (C.user == subquery.c.user) & (C.time == subquery.c.latest))
)
for c in session.exec(statement2):
for i, p in enumerate(c.love):
G.add_edge(c.user, p, group="love", rank=i, popularity=1 - 0.08 * i)
if dislike:
if show >= 1:
for i, p in enumerate(c.love):
G.add_edge(c.user, p, group="love", rank=i, popularity=1 - 0.08 * i)
if show <= 1:
for i, p in enumerate(c.hate):
G.add_edge(c.user, p, group="hate", rank=8, popularity=-0.16)
return G
@@ -82,7 +83,7 @@ class Params(BaseModel):
distance: float | None = 0.2
weighting: bool | None = True
popularity: bool | None = True
dislike: bool | None = False
show: int | None = 2
ARROWSTYLE = {"love": "-|>", "hate": "-|>"}
@@ -96,7 +97,7 @@ async def render_sociogram(params: Params):
ax.set_facecolor("none") # Set the axis face color to none (transparent)
ax.axis("off") # Turn off axis ticks and frames
G = sociogram_data(params.dislike)
G = sociogram_data(show=params.show)
pos = nx.spring_layout(G, scale=2, k=params.distance, iterations=50, seed=None)
nodes = nx.draw_networkx_nodes(
G,