Skip to content

Commit

Permalink
fixes learner display
Browse files Browse the repository at this point in the history
  • Loading branch information
WolfgangFahl committed Jan 25, 2024
1 parent b6c7564 commit aa88cdd
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 37 deletions.
5 changes: 2 additions & 3 deletions dcm/dcm_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import markdown2
import yaml
from dataclasses_json import dataclass_json
from ngwidgets.yamlable import YamlAble
from ngwidgets.yamlable import YamlAble, lod_storable
from slugify import slugify

from dcm.svg import SVG, SVGNodeConfig
Expand Down Expand Up @@ -401,8 +401,7 @@ def facet_id(self):
return parts[3] if len(parts) > 3 else None


@dataclass_json
@dataclass
@lod_storable
class Learner:
"""
A learner with achievements.
Expand Down
66 changes: 32 additions & 34 deletions dcm/dcm_webserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
@author: wf
"""
import json
import asyncio
import os
import uuid
from dataclasses import dataclass
Expand Down Expand Up @@ -36,7 +36,6 @@ class SVGRenderRequest(BaseModel):
markup (str): The format of the definition ('json' or 'yaml').
config (SVGConfig): Optional configuration for SVG rendering. Defaults to None, which uses default settings.
"""

name: str
definition: str
markup: str
Expand Down Expand Up @@ -85,17 +84,15 @@ def __init__(self):
)
self.examples = DynamicCompetenceMap.get_examples(markup="yaml")
self.dcm = None
self.container = None
self.learner = None
self.assessment = None
self.content_div = None
self.timeout=0.5
self.text_mode = "none"
config_path = os.path.join(os.environ["HOME"], ".dcm/config.yaml")
self.server_config = ServerConfig.from_yaml(config_path)

@app.get("/learner/{learner_slug}")
async def show_learner(learner_slug: str):
return await self.assess_learner_by_slug(learner_slug)

# FastAPI endpoints
@app.post("/svg/")
async def render_svg(svg_render_request: SVGRenderRequest) -> HTMLResponse:
"""
Expand Down Expand Up @@ -175,6 +172,12 @@ async def get_description_for_tree(tree_id: str) -> HTMLResponse:
"""
path = f"{tree_id}"
return await self.show_description(path)

# nicegui RESTFul endpoints
@ui.page("/learner/{learner_slug}")
async def show_learner(client: Client,learner_slug: str):
await client.connected(timeout=self.timeout)
return await self.assess_learner_by_slug(learner_slug)

async def show_description(self, path: str = None) -> HTMLResponse:
"""
Expand Down Expand Up @@ -239,7 +242,7 @@ async def render(self, _click_args=None):
input_source = self.input
if input_source:
name = self.get_basename_without_extension(input_source)
with self.container:
with self.content_div:
ui.notify(f"rendering {name}")
definition = self.do_read_input(input_source)
# Determine the format based on the file extension
Expand Down Expand Up @@ -312,7 +315,7 @@ def prepare_ui(self):
ui.add_head_html(java_script)

def show_ui(self):
with ui.element("div").classes("w-full") as self.container:
with self.content_div:
with ui.splitter() as splitter:
with splitter.before:
with ui.grid(columns=2).classes("w-full") as self.left_selection:
Expand Down Expand Up @@ -375,15 +378,16 @@ def assess_learner(self, dcm, learner):
learner(Learner): the learner to get the self assessment for
"""
if self.container:
with self.container:
if self.assessment is not None:
self.assessment.reset(dcm=dcm, learner=learner)
else:
with self.left_grid:
with ui.row() as self.assessment_row:
self.assessment = Assessment(self, dcm=dcm, learner=learner)
self.assessment.update_achievement_view()
if not self.content_div:
return
with self.content_div:
if self.assessment is not None:
self.assessment.reset(dcm=dcm, learner=learner)
else:
with self.left_grid:
with ui.row() as self.assessment_row:
self.assessment = Assessment(self, dcm=dcm, learner=learner)
self.assessment.update_achievement_view()

def new_assess(self):
"""
Expand All @@ -402,24 +406,18 @@ async def assess_learner_by_slug(self, learner_slug: str):
Raises:
HTTPException: If the learner file does not exist or an error occurs.
"""

def show():
learner_file = os.path.join(
self.server_config.storage_path, f"{learner_slug}.json"
)
if not os.path.exists(learner_file):
raise HTTPException(status_code=404, detail="Learner not found")
try:
with open(learner_file, "r") as file:
learner_data = json.load(file)
learner = Learner.from_dict(learner_data)
except Exception as e:
# Handle any exceptions related to file reading or JSON parsing
raise HTTPException(status_code=500, detail=str(e))
pass
self.assess(learner)
self.show_ui()
learner_file = os.path.join(
self.server_config.storage_path, f"{learner_slug}.json"
)
learner = Learner.load_from_json_file(learner_file)
self.assess(learner)
except Exception as ex:
self.handle_exception(ex, self.do_trace)

await self.setup_content_div(show())
await self.setup_content_div(show)

def assess(self, learner: Learner, tree_id: str = None):
"""
Expand Down Expand Up @@ -458,7 +456,7 @@ async def download(self, _args):
allow downloading the assessment result
"""
try:
with self.container:
with self.content_div:
if not self.assessment:
ui.notify("no active learner assessment")
return
Expand Down

0 comments on commit aa88cdd

Please sign in to comment.