Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: character image in short answer XBlock #5

Merged
merged 1 commit into from
Feb 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions ai_eval/shortanswer.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ class ShortAnswerAIEvalXBlock(AIEvalXBlock):
scope=Scope.settings,
)

character_image = String(
display_name=_("Character Image URL"),
help=_(
"URL for an image to be shown to the left of the chat box; "
"leave empty to disable"
),
scope=Scope.settings,
)

max_responses = Integer(
display_name=_("Max Responses"),
help=_("The maximum number of response messages the student can submit"),
Expand All @@ -47,6 +56,7 @@ class ShortAnswerAIEvalXBlock(AIEvalXBlock):
editable_fields = AIEvalXBlock.editable_fields + (
"max_responses",
"allow_reset",
"character_image",
)

def validate_field_data(self, validation, data):
Expand Down
10 changes: 10 additions & 0 deletions ai_eval/static/css/shortanswer.css
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
/* CSS for ShortAnswerAIEvalXBlock */

.shortanswer_image {
float: left;
margin-right: 4rem;
width: 30%;
}

.shortanswer_image > img {
max-width: 100%;
}

.shortanswer_block .count {
font-weight: bold;
}
Expand Down
6 changes: 6 additions & 0 deletions ai_eval/templates/shortanswer.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
{% if self.character_image %}
<div class="shortanswer_image">
<img src="{{ self.character_image }}" />
</div>
{% endif %}

<div class="shortanswer_block">
<div>
<div id="question-text">
Expand Down
10 changes: 10 additions & 0 deletions ai_eval/tests/test_ai_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,13 @@ def test_reset_forbidden(self):
with self.assertRaises(JsonHandlerError):
block.reset.__wrapped__(block, data={})
self.assertEqual(block.messages, {"USER": ["Hello"], "LLM": ["Hello"]})

def test_character_image(self):
"""Test the character image."""
data = {
**self.data,
"character_image": "/static/image.jpg",
}
block = ShortAnswerAIEvalXBlock(ToyRuntime(), DictFieldData(data), None)
frag = block.student_view()
self.assertIn('<img src="/static/image.jpg" />', frag.content)
Loading