Skip to content

Commit 23d3455

Browse files
Merge pull request #279 from coding-for-reproducible-research/julia_session_2
Render output for io quiz
2 parents 773d80a + f49d6b5 commit 23d3455

File tree

1 file changed

+121
-24
lines changed
  • individual_modules/introduction_to_julia

1 file changed

+121
-24
lines changed

individual_modules/introduction_to_julia/io.ipynb

Lines changed: 121 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -677,7 +677,7 @@
677677
},
678678
{
679679
"cell_type": "code",
680-
"execution_count": 12,
680+
"execution_count": 1,
681681
"id": "4b88b1f7-2ac2-4924-a0c5-3ee98ca0728b",
682682
"metadata": {
683683
"editable": true,
@@ -690,26 +690,115 @@
690690
},
691691
"outputs": [
692692
{
693-
"ename": "LoadError",
694-
"evalue": "ArgumentError: Package JSON not found in current path.\n- Run `import Pkg; Pkg.add(\"JSON\")` to install the JSON package.",
695-
"output_type": "error",
696-
"traceback": [
697-
"ArgumentError: Package JSON not found in current path.\n- Run `import Pkg; Pkg.add(\"JSON\")` to install the JSON package.",
698-
"",
699-
"Stacktrace:",
700-
" [1] macro expansion",
701-
" @ .\\loading.jl:2296 [inlined]",
702-
" [2] macro expansion",
703-
" @ .\\lock.jl:273 [inlined]",
704-
" [3] __require(into::Module, mod::Symbol)",
705-
" @ Base .\\loading.jl:2271",
706-
" [4] #invoke_in_world#3",
707-
" @ .\\essentials.jl:1089 [inlined]",
708-
" [5] invoke_in_world",
709-
" @ .\\essentials.jl:1086 [inlined]",
710-
" [6] require(into::Module, mod::Symbol)",
711-
" @ Base .\\loading.jl:2260"
712-
]
693+
"data": {
694+
"text/html": [
695+
"<style>\n",
696+
".quiz-question {\n",
697+
" background-color: #6c63ff;\n",
698+
" color: white;\n",
699+
" padding: 12px;\n",
700+
" border-radius: 10px;\n",
701+
" font-weight: bold;\n",
702+
" font-size: 1.2em;\n",
703+
" margin-bottom: 10px;\n",
704+
"}\n",
705+
"\n",
706+
".quiz-form {\n",
707+
" margin-bottom: 20px;\n",
708+
"}\n",
709+
"\n",
710+
".quiz-answer {\n",
711+
" display: block;\n",
712+
" background-color: #f2f2f2;\n",
713+
" border: none;\n",
714+
" border-radius: 10px;\n",
715+
" padding: 10px;\n",
716+
" margin: 5px 0;\n",
717+
" font-size: 1em;\n",
718+
" cursor: pointer;\n",
719+
" text-align: left;\n",
720+
" transition: background-color 0.3s;\n",
721+
" width: 100%;\n",
722+
"}\n",
723+
"\n",
724+
".quiz-answer:hover {\n",
725+
" background-color: #e0e0e0;\n",
726+
"}\n",
727+
"\n",
728+
".correct {\n",
729+
" background-color: #4CAF50 !important;\n",
730+
" color: white !important;\n",
731+
" border: none;\n",
732+
"}\n",
733+
"\n",
734+
".incorrect {\n",
735+
" background-color: #D32F2F !important;\n",
736+
" color: white !important;\n",
737+
" border: none;\n",
738+
"}\n",
739+
"\n",
740+
".feedback {\n",
741+
" margin-top: 10px;\n",
742+
" font-weight: bold;\n",
743+
" font-size: 1em;\n",
744+
"}\n",
745+
"</style>\n",
746+
"\n",
747+
"<script>\n",
748+
"function handleAnswer(qid, aid, feedback, isCorrect) {\n",
749+
" // Reset all buttons for the question\n",
750+
" let buttons = document.querySelectorAll(\".answer-\" + qid);\n",
751+
" buttons.forEach(btn => {\n",
752+
" btn.classList.remove('correct', 'incorrect');\n",
753+
" });\n",
754+
"\n",
755+
" // Apply correct/incorrect to selected\n",
756+
" let selected = document.getElementById(aid);\n",
757+
" selected.classList.add(isCorrect ? 'correct' : 'incorrect');\n",
758+
"\n",
759+
" // Show feedback below the question\n",
760+
" let feedbackBox = document.getElementById('feedback_' + qid);\n",
761+
" feedbackBox.innerHTML = feedback;\n",
762+
" feedbackBox.style.color = isCorrect ? 'green' : 'red';\n",
763+
"}\n",
764+
"</script>\n",
765+
"<div class=\"quiz-question\">Which of the following is the most memory-efficient way to read a very large text file line-by-line in Julia?</div><form class=\"quiz-form\"><button type=\"button\" class=\"quiz-answer answer-1\" id=\"q1_a1\"\n",
766+
" onclick=\"handleAnswer('1', 'q1_a1', 'Incorrect', false)\">\n",
767+
" read(file, String)\n",
768+
"</button>\n",
769+
"<button type=\"button\" class=\"quiz-answer answer-1\" id=\"q1_a2\"\n",
770+
" onclick=\"handleAnswer('1', 'q1_a2', 'Incorrect', false)\">\n",
771+
" readline(file)\n",
772+
"</button>\n",
773+
"<button type=\"button\" class=\"quiz-answer answer-1\" id=\"q1_a3\"\n",
774+
" onclick=\"handleAnswer('1', 'q1_a3', 'Correct', true)\">\n",
775+
" eachline(file)\n",
776+
"</button>\n",
777+
"<button type=\"button\" class=\"quiz-answer answer-1\" id=\"q1_a4\"\n",
778+
" onclick=\"handleAnswer('1', 'q1_a4', 'Incorrect', false)\">\n",
779+
" readlines(file)\n",
780+
"</button>\n",
781+
"<div class=\"feedback\" id=\"feedback_1\"></div></form><hr><div class=\"quiz-question\">In Julia, what is the recommended way to ensure a file is always closed after reading or writing, even if an error occurs during processing?</div><form class=\"quiz-form\"><button type=\"button\" class=\"quiz-answer answer-2\" id=\"q2_a1\"\n",
782+
" onclick=\"handleAnswer('2', 'q2_a1', 'Incorrect', false)\">\n",
783+
" Manually call close(file) after reading/writing.\n",
784+
"</button>\n",
785+
"<button type=\"button\" class=\"quiz-answer answer-2\" id=\"q2_a2\"\n",
786+
" onclick=\"handleAnswer('2', 'q2_a2', 'Incorrect', false)\">\n",
787+
" Use try..catch..finally to close the file\n",
788+
"</button>\n",
789+
"<button type=\"button\" class=\"quiz-answer answer-2\" id=\"q2_a3\"\n",
790+
" onclick=\"handleAnswer('2', 'q2_a3', 'Correct', true)\">\n",
791+
" Use the do block form of open()\n",
792+
"</button>\n",
793+
"<button type=\"button\" class=\"quiz-answer answer-2\" id=\"q2_a4\"\n",
794+
" onclick=\"handleAnswer('2', 'q2_a4', 'Incorrect', false)\">\n",
795+
" Let Julias garbage collector handle it\n",
796+
"</button>\n",
797+
"<div class=\"feedback\" id=\"feedback_2\"></div></form><hr>"
798+
]
799+
},
800+
"metadata": {},
801+
"output_type": "display_data"
713802
}
714803
],
715804
"source": [
@@ -817,19 +906,27 @@
817906
"# Use the function\n",
818907
"show_quiz_from_json(\"questions/summary_io.json\")"
819908
]
909+
},
910+
{
911+
"cell_type": "code",
912+
"execution_count": null,
913+
"id": "09d9a8d1-84b5-40e0-94f4-ba8415cf59b6",
914+
"metadata": {},
915+
"outputs": [],
916+
"source": []
820917
}
821918
],
822919
"metadata": {
823920
"kernelspec": {
824-
"display_name": "Julia 1.10.9",
921+
"display_name": "Julia 1.11.5",
825922
"language": "julia",
826-
"name": "julia-1.10"
923+
"name": "julia-1.11"
827924
},
828925
"language_info": {
829926
"file_extension": ".jl",
830927
"mimetype": "application/julia",
831928
"name": "julia",
832-
"version": "1.10.9"
929+
"version": "1.11.5"
833930
}
834931
},
835932
"nbformat": 4,

0 commit comments

Comments
 (0)