Skip to content

Commit

Permalink
formats
Browse files Browse the repository at this point in the history
  • Loading branch information
WolfgangFahl committed Feb 5, 2024
1 parent 84c41a3 commit ad62f68
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 53 deletions.
4 changes: 1 addition & 3 deletions dcm/dcm_assessment.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,7 @@ def store(self) -> str:

# Determine the file path for storing the learner's data
filename = self.learner.file_name + ".json"
file_path = os.path.join(
self.webserver.config.storage_path, filename
)
file_path = os.path.join(self.webserver.config.storage_path, filename)

# Write the serialized data to the file
with open(file_path, "w") as file:
Expand Down
11 changes: 7 additions & 4 deletions dcm/dcm_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"""
import json
import os
from datetime import datetime
from dataclasses import dataclass, field
from datetime import datetime
from json.decoder import JSONDecodeError
from typing import Any, Dict, List, Optional, Tuple, Union

Expand Down Expand Up @@ -475,7 +475,7 @@ def file_name(self):
self.learner_id, lowercase=False, regex_pattern=r"[^\w\s\-]"
)
return file_name

@property
def most_recent_achievement_iso_date(self) -> Optional[str]:
"""
Expand All @@ -486,14 +486,17 @@ def most_recent_achievement_iso_date(self) -> Optional[str]:
"""
if not self.achievements:
return None
dates = [achievement.date_assessed_iso for achievement in self.achievements if achievement.date_assessed_iso]
dates = [
achievement.date_assessed_iso
for achievement in self.achievements
if achievement.date_assessed_iso
]
if not dates:
return None
# Parse the ISO dates and return the most recent one
most_recent_date = max(datetime.fromisoformat(date) for date in dates)
return most_recent_date.isoformat()


def get_achievement_index(self, path) -> int:
a_index = self.achievement_indices_by_path.get(path)
return a_index
Expand Down
80 changes: 44 additions & 36 deletions dcm/dcm_webserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from dcm.svg import SVG, SVGConfig
from dcm.version import Version


class SVGRenderRequest(BaseModel):
"""
A request for rendering an SVG.
Expand All @@ -33,12 +34,14 @@ 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
text_mode: Optional[str] = "empty"
config: Optional[SVGConfig] = None


class DynamicCompentenceMapWebServer(InputWebserver):
"""
server to supply Dynamic Competence Map Visualizations
Expand All @@ -52,12 +55,12 @@ def get_config(cls) -> WebserverConfig:
copy_right = ""
config = WebserverConfig(
short_name="dcm",
copy_right=copy_right,
version=Version(),
default_port=8885
copy_right=copy_right,
version=Version(),
default_port=8885,
)
server_config=WebserverConfig.get(config)
server_config.solution_class=DcmSolution
server_config = WebserverConfig.get(config)
server_config.solution_class = DcmSolution
return server_config

def __init__(self):
Expand All @@ -66,7 +69,7 @@ def __init__(self):
self, config=DynamicCompentenceMapWebServer.get_config()
)
self.examples = DynamicCompetenceMap.get_examples(markup="yaml")

# FastAPI endpoints
@app.post("/svg/")
async def render_svg(svg_render_request: SVGRenderRequest) -> HTMLResponse:
Expand Down Expand Up @@ -151,23 +154,25 @@ async def get_description_for_tree(tree_id: str) -> HTMLResponse:
# nicegui RESTFul endpoints
@ui.page("/learner/{learner_slug}")
async def show_learner(client: Client, learner_slug: str):
return await self.page(client, DcmSolution.assess_learner_by_slug,learner_slug)

async def render_svg(self, svg_render_request: SVGRenderRequest) -> HTMLResponse:
"""
render the given request
"""
r = svg_render_request
dcm = DynamicCompetenceMap.from_definition_string(
r.name, r.definition, content_class=CompetenceTree, markup=r.markup
)
dcm_chart = DcmChart(dcm)
svg_markup = dcm_chart.generate_svg_markup(
config=r.config, with_java_script=True, text_mode=r.text_mode
return await self.page(
client, DcmSolution.assess_learner_by_slug, learner_slug
)
response = HTMLResponse(content=svg_markup)
return response


async def render_svg(self, svg_render_request: SVGRenderRequest) -> HTMLResponse:
"""
render the given request
"""
r = svg_render_request
dcm = DynamicCompetenceMap.from_definition_string(
r.name, r.definition, content_class=CompetenceTree, markup=r.markup
)
dcm_chart = DcmChart(dcm)
svg_markup = dcm_chart.generate_svg_markup(
config=r.config, with_java_script=True, text_mode=r.text_mode
)
response = HTMLResponse(content=svg_markup)
return response

async def show_description(self, path: str = None) -> HTMLResponse:
"""
Show the HTML description of a specific
Expand Down Expand Up @@ -211,12 +216,13 @@ def configure_run(self):
self.root_path,
]
pass



class DcmSolution(InputWebSolution):
"""
the Dynamic Competence Map solution
"""

def __init__(self, webserver: DynamicCompentenceMapWebServer, client: Client):
"""
Initialize the solution
Expand All @@ -228,7 +234,7 @@ def __init__(self, webserver: DynamicCompentenceMapWebServer, client: Client):
"""
super().__init__(webserver, client) # Call to the superclass constructor
self.dcm = None
self.learner=None
self.learner = None
self.assessment = None
self.text_mode = "empty"

Expand All @@ -244,9 +250,9 @@ def save_session_state(self) -> None:
"""
Save the current session state to app.storage.user.
"""
learner_id=self.learner.learner_id if self.learner else None
app.storage.user['learner_id'] = learner_id
app.storage.user['assessment'] = self.assessment is not None
learner_id = self.learner.learner_id if self.learner else None
app.storage.user["learner_id"] = learner_id
app.storage.user["assessment"] = self.assessment is not None

def get_learner_file_path(self, learner_slug: str) -> str:
"""
Expand Down Expand Up @@ -357,9 +363,9 @@ def prepare_ui(self):
"""
prepare the user interface
"""
self.user_id = app.storage.browser['id']
self.user_id = app.storage.browser["id"]
self.prepare_svg()

def prepare_svg(self):
"""
prepare the SVG / javascript display
Expand All @@ -369,7 +375,7 @@ def prepare_svg(self):
java_script = self.svg.get_java_script()

# Add the script using ui.add_head_html()
ui.add_head_html(java_script,shared=True)
ui.add_head_html(java_script, shared=True)

def show_ui(self):
"""
Expand Down Expand Up @@ -416,7 +422,9 @@ def show_ui(self):
with splitter.after:
self.svg_view = ui.html("")

async def home(self,):
async def home(
self,
):
"""Generates the home page with a selection of examples and
svg display
"""
Expand Down Expand Up @@ -447,9 +455,9 @@ def new_assess(self):
"""
run a new assessment for a new learner
"""
try:
learner_id=f"{uuid.uuid4()}"
self.learner=Learner(learner_id)
try:
learner_id = f"{uuid.uuid4()}"
self.learner = Learner(learner_id)
self.save_session_state()
self.assess_learner(self.dcm, self.learner)
except Exception as ex:
Expand Down Expand Up @@ -517,7 +525,7 @@ def assess_state(self):
# the assessment is already on
self.assessment_button.disable()
if self.assessment is not None:
# downloading is possible
# downloading is possible
self.download_button.enable()
else:
# downloading is not possible - we have n learner
Expand Down
13 changes: 5 additions & 8 deletions tests/test_dcm_chart.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,7 @@ def test_radar_chart(self):

# Use the function to calculate points for the scores
radar_points = dcm_chart.calculate_radar_chart_points(
self.scores,
max_score=100.0,
center=center,
radius=radius
self.scores, max_score=100.0, center=center, radius=radius
)

# Create a Polygon for the radar chart
Expand All @@ -84,9 +81,9 @@ def test_radar_chart(self):

# Create an SVG instance and add the radar chart Polygon
svg = SVG(SVGConfig(width=600, height=600))

# Add concentric circles at every 10% interval
for i in range(1,11):
for i in range(1, 11):
circle_radius = (radius * i) / 10
svg.add_circle(
SVGNodeConfig(
Expand All @@ -108,5 +105,5 @@ def test_radar_chart(self):
if self.debug:
svg_markup = svg.get_svg_markup()
print(svg_markup)
self.assertTrue('''<polygon points="500.0''' in svg_markup)

self.assertTrue("""<polygon points="500.0""" in svg_markup)
6 changes: 4 additions & 2 deletions tests/test_learner.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,7 @@ def test_learner_competence_trees(self):
self.assertEqual("architecture", tree_ids[0])
for achievement in learner.achievements:
self.assertTrue(achievement.path in learner.achievements_by_path)
debug=True
self.assertEqual("2023-06-20T00:00:00",learner.most_recent_achievement_iso_date)
debug = True
self.assertEqual(
"2023-06-20T00:00:00", learner.most_recent_achievement_iso_date
)

0 comments on commit ad62f68

Please sign in to comment.