diff --git a/material/rest-api-ui-ktor-quiz-collector/.gitignore b/material/rest-api-ui-ktor-quiz-collector/.gitignore index c426c32f..ecf450de 100644 --- a/material/rest-api-ui-ktor-quiz-collector/.gitignore +++ b/material/rest-api-ui-ktor-quiz-collector/.gitignore @@ -33,4 +33,5 @@ out/ /.nb-gradle/ ### VS Code ### -.vscode/ \ No newline at end of file +.vscode/ +.kotlin \ No newline at end of file diff --git a/material/rest-api-ui-ktor-quiz-collector/assets/gradle-build.png b/material/rest-api-ui-ktor-quiz-collector/assets/gradle-build.png new file mode 100644 index 00000000..f8d25536 Binary files /dev/null and b/material/rest-api-ui-ktor-quiz-collector/assets/gradle-build.png differ diff --git a/material/rest-api-ui-ktor-quiz-collector/gradle.properties b/material/rest-api-ui-ktor-quiz-collector/gradle.properties index 7fc6f1ff..d8ec6a9b 100644 --- a/material/rest-api-ui-ktor-quiz-collector/gradle.properties +++ b/material/rest-api-ui-ktor-quiz-collector/gradle.properties @@ -1 +1,5 @@ kotlin.code.style=official + +org.gradle.caching=true +org.gradle.configuration-cache=true +org.gradle.parallel=true \ No newline at end of file diff --git a/material/rest-api-ui-ktor-quiz-collector/quiz-collector-bruno/results.bru b/material/rest-api-ui-ktor-quiz-collector/quiz-collector-bruno/responses.bru similarity index 53% rename from material/rest-api-ui-ktor-quiz-collector/quiz-collector-bruno/results.bru rename to material/rest-api-ui-ktor-quiz-collector/quiz-collector-bruno/responses.bru index 77d7627b..9f4f0516 100644 --- a/material/rest-api-ui-ktor-quiz-collector/quiz-collector-bruno/results.bru +++ b/material/rest-api-ui-ktor-quiz-collector/quiz-collector-bruno/responses.bru @@ -1,11 +1,11 @@ meta { - name: /results + name: /responses type: http seq: 4 } get { - url: http://localhost:8080/results + url: http://localhost:8080/responses body: none auth: none } diff --git a/material/rest-api-ui-ktor-quiz-collector/readme.md b/material/rest-api-ui-ktor-quiz-collector/readme.md new file mode 100644 index 00000000..c1bf9088 --- /dev/null +++ b/material/rest-api-ui-ktor-quiz-collector/readme.md @@ -0,0 +1,11 @@ +# Ktor + jupyter quiz collection + +Enable dev mode + +- Make sure development mode is enabled in your Ktor configuration file. This depends on how you created and run your + server. If you use EngineMain to run a server, enable development mode in the configuration file. +- Add run task that runs `./gradlew -t build` and run it. + +![img.png](assets/gradle-build.png) + +- Run the ktor server with `./gradlew run` or from a run task \ No newline at end of file diff --git a/material/rest-api-ui-ktor-quiz-collector/result-explorer/demo01.ipynb b/material/rest-api-ui-ktor-quiz-collector/result-explorer/demo01.ipynb index 9c813c92..367f42c7 100644 --- a/material/rest-api-ui-ktor-quiz-collector/result-explorer/demo01.ipynb +++ b/material/rest-api-ui-ktor-quiz-collector/result-explorer/demo01.ipynb @@ -9,128 +9,28 @@ }, { "cell_type": "code", - "execution_count": 3, "metadata": { "vscode": { "languageId": "python" + }, + "ExecuteTime": { + "end_time": "2024-08-23T22:10:39.416441300Z", + "start_time": "2024-08-23T22:10:34.053252400Z" } }, + "source": [ + "%use kandy\n", + "%use dataframe\n", + "val df = DataFrame.read(\"http://localhost:8080/responses\")\n", + "df" + ], "outputs": [ { "data": { - "application/kotlindataframe+json": { - "columns": [ - "responses" - ], - "kotlin_dataframe": [ - { - "responses": [ - { - "answer": "Muzukashi", - "question": "What is the primary goal of Kotlin Multiplatform?" - }, - { - "answer": "", - "question": "How does Kotlin Multiplatform facilitate code sharing between platforms?" - } - ] - }, - { - "responses": [ - { - "answer": "To share code between multiple platforms", - "question": "What is the primary goal of Kotlin Multiplatform?" - }, - { - "answer": "By sharing business logic and adapting UI", - "question": "How does Kotlin Multiplatform facilitate code sharing between platforms?" - }, - { - "answer": "Android, iOS, and web", - "question": "Which platforms does Kotlin Multiplatform support?" - }, - { - "answer": "Developing a cross-platform app", - "question": "What is a common use case for Kotlin Multiplatform?" - }, - { - "answer": "Shared module", - "question": "What is a shared code module in Kotlin Multiplatform called?" - }, - { - "answer": "Through expect and actual declarations", - "question": "How does Kotlin Multiplatform handle platform-specific implementations?" - }, - { - "answer": "Java, JavaScript, Swift", - "question": "What languages can be interoperable with Kotlin Multiplatform?" - }, - { - "answer": "IntelliJ IDEA, Android Studio", - "question": "What tooling supports Kotlin Multiplatform development?" - }, - { - "answer": "Code reuse and sharing", - "question": "What is the benefit of using Kotlin Multiplatform for mobile development?" - }, - { - "answer": "Kotlin Multiplatform allows sharing code between different platforms using common modules.", - "question": "How does Kotlin Multiplatform differ from Kotlin Native and Kotlin/JS?" - } - ] - }, - { - "responses": [ - { - "answer": "Nashi", - "question": "What is the primary goal of Kotlin Multiplatform?" - }, - { - "answer": "Don't know", - "question": "How does Kotlin Multiplatform facilitate code sharing between platforms?" - }, - { - "answer": "Windows Phone", - "question": "Which platforms does Kotlin Multiplatform support?" - }, - { - "answer": "No", - "question": "What is a common use case for Kotlin Multiplatform?" - }, - { - "answer": "Nashi", - "question": "What is a shared code module in Kotlin Multiplatform called?" - }, - { - "answer": "Copy paste is the best", - "question": "How does Kotlin Multiplatform handle platform-specific implementations?" - }, - { - "answer": "Zenbu", - "question": "What languages can be interoperable with Kotlin Multiplatform?" - }, - { - "answer": "Vim", - "question": "What tooling supports Kotlin Multiplatform development?" - }, - { - "answer": "Code reuse and sharing", - "question": "What is the benefit of using Kotlin Multiplatform for mobile development?" - }, - { - "answer": "Kotlin Multiplatform allows sharing code between different platforms using common modules.", - "question": "How does Kotlin Multiplatform differ from Kotlin Native and Kotlin/JS?" - } - ] - } - ], - "ncol": 1, - "nrow": 3 - }, "text/html": [ - " \n", - " \n", - " \n", " \n", " \n", - "
\n", - "\n", - "

DataFrame: rowsCount = 3, columnsCount = 1

\n", - "
responses
DataFrame [2 x 2]
questionanswer
What is the primary goal of Kotlin Mu...Muzukashi
How does Kotlin Multiplatform facilit...
DataFrame [10 x 2]
questionanswer
What is the primary goal of Kotlin Mu...To share code between multiple platforms
How does Kotlin Multiplatform facilit...By sharing business logic and adaptin...
Which platforms does Kotlin Multiplat...Android, iOS, and web
What is a common use case for Kotlin ...Developing a cross-platform app
What is a shared code module in Kotli...Shared module

... showing only top 5 of 10 rows

DataFrame [10 x 2]
questionanswer
What is the primary goal of Kotlin Mu...Nashi
How does Kotlin Multiplatform facilit...Don't know
Which platforms does Kotlin Multiplat...Windows Phone
What is a common use case for Kotlin ...No
What is a shared code module in Kotli...Nashi

... showing only top 5 of 10 rows

\n", + "
What is the primary goal of Kotlin Multiplatform?How does Kotlin Multiplatform facilitate code sharing between platforms?Which platforms does Kotlin Multiplatform support?What is a common use case for Kotlin Multiplatform?What is a shared code module in Kotlin Multiplatform called?How does Kotlin Multiplatform handle platform-specific implementations?What languages can be interoperable with Kotlin Multiplatform?What tooling supports Kotlin Multiplatform development?What is the benefit of using Kotlin Multiplatform for mobile development?How does Kotlin Multiplatform differ from Kotlin Native and Kotlin/JS?
MuzukashiBy sharing business logic and adaptin...nullnullnullnullnullnullnullnull
To share code between multiple platformsBy sharing business logic and adaptin...Android, iOS, and webDeveloping a cross-platform appShared moduleThrough expect and actual declarationsJava, JavaScript, SwiftIntelliJ IDEA, Android StudioCode reuse and sharingKotlin Multiplatform allows sharing c...
NashiDon't knowWindows PhoneNoNashiCopy paste is the bestZenbuVimCode reuse and sharingKotlin Multiplatform allows sharing c...
\n", " \n", " \n", " " - ] + ], + "application/kotlindataframe+json": "{\"$version\":\"2.0.0\",\"metadata\":{\"columns\":[\"What is the primary goal of Kotlin Multiplatform?\",\"How does Kotlin Multiplatform facilitate code sharing between platforms?\",\"Which platforms does Kotlin Multiplatform support?\",\"What is a common use case for Kotlin Multiplatform?\",\"What is a shared code module in Kotlin Multiplatform called?\",\"How does Kotlin Multiplatform handle platform-specific implementations?\",\"What languages can be interoperable with Kotlin Multiplatform?\",\"What tooling supports Kotlin Multiplatform development?\",\"What is the benefit of using Kotlin Multiplatform for mobile development?\",\"How does Kotlin Multiplatform differ from Kotlin Native and Kotlin/JS?\"],\"nrow\":3,\"ncol\":10},\"kotlin_dataframe\":[{\"What is the primary goal of Kotlin Multiplatform?\":\"Muzukashi\",\"How does Kotlin Multiplatform facilitate code sharing between platforms?\":\"By sharing business logic and adapting UI\",\"Which platforms does Kotlin Multiplatform support?\":null,\"What is a common use case for Kotlin Multiplatform?\":null,\"What is a shared code module in Kotlin Multiplatform called?\":null,\"How does Kotlin Multiplatform handle platform-specific implementations?\":null,\"What languages can be interoperable with Kotlin Multiplatform?\":null,\"What tooling supports Kotlin Multiplatform development?\":null,\"What is the benefit of using Kotlin Multiplatform for mobile development?\":null,\"How does Kotlin Multiplatform differ from Kotlin Native and Kotlin/JS?\":null},{\"What is the primary goal of Kotlin Multiplatform?\":\"To share code between multiple platforms\",\"How does Kotlin Multiplatform facilitate code sharing between platforms?\":\"By sharing business logic and adapting UI\",\"Which platforms does Kotlin Multiplatform support?\":\"Android, iOS, and web\",\"What is a common use case for Kotlin Multiplatform?\":\"Developing a cross-platform app\",\"What is a shared code module in Kotlin Multiplatform called?\":\"Shared module\",\"How does Kotlin Multiplatform handle platform-specific implementations?\":\"Through expect and actual declarations\",\"What languages can be interoperable with Kotlin Multiplatform?\":\"Java, JavaScript, Swift\",\"What tooling supports Kotlin Multiplatform development?\":\"IntelliJ IDEA, Android Studio\",\"What is the benefit of using Kotlin Multiplatform for mobile development?\":\"Code reuse and sharing\",\"How does Kotlin Multiplatform differ from Kotlin Native and Kotlin/JS?\":\"Kotlin Multiplatform allows sharing code between different platforms using common modules.\"},{\"What is the primary goal of Kotlin Multiplatform?\":\"Nashi\",\"How does Kotlin Multiplatform facilitate code sharing between platforms?\":\"Don't know\",\"Which platforms does Kotlin Multiplatform support?\":\"Windows Phone\",\"What is a common use case for Kotlin Multiplatform?\":\"No\",\"What is a shared code module in Kotlin Multiplatform called?\":\"Nashi\",\"How does Kotlin Multiplatform handle platform-specific implementations?\":\"Copy paste is the best\",\"What languages can be interoperable with Kotlin Multiplatform?\":\"Zenbu\",\"What tooling supports Kotlin Multiplatform development?\":\"Vim\",\"What is the benefit of using Kotlin Multiplatform for mobile development?\":\"Code reuse and sharing\",\"How does Kotlin Multiplatform differ from Kotlin Native and Kotlin/JS?\":\"Kotlin Multiplatform allows sharing code between different platforms using common modules.\"}]}" }, - "execution_count": 3, + "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], - "source": [ - "%use kandy\n", - "%use dataframe\n", - "\n", - "val df = DataFrame.read(\"http://localhost:8080/results\")\n", - "df" - ] + "execution_count": 6 }, { "cell_type": "code", - "execution_count": 7, "metadata": { "vscode": { "languageId": "python" + }, + "ExecuteTime": { + "end_time": "2024-08-23T21:40:01.838147400Z", + "start_time": "2024-08-23T21:39:58.091494700Z" } }, + "source": [ + "val table = DataFrame.readJson(\"http://localhost:8080/table\")\n", + "table" + ], "outputs": [ { "data": { - "application/kotlindataframe+json": { - "columns": [ - "What is the primary goal of Kotlin Multiplatform?", - "How does Kotlin Multiplatform facilitate code sharing between platforms?", - "Which platforms does Kotlin Multiplatform support?", - "What is a common use case for Kotlin Multiplatform?", - "What is a shared code module in Kotlin Multiplatform called?", - "How does Kotlin Multiplatform handle platform-specific implementations?", - "What languages can be interoperable with Kotlin Multiplatform?", - "What tooling supports Kotlin Multiplatform development?", - "What is the benefit of using Kotlin Multiplatform for mobile development?", - "How does Kotlin Multiplatform differ from Kotlin Native and Kotlin/JS?" - ], - "kotlin_dataframe": [ - { - "How does Kotlin Multiplatform differ from Kotlin Native and Kotlin/JS?": [ - "Kotlin Multiplatform allows sharing code between different platforms using common modules.", - "Kotlin Multiplatform allows sharing code between different platforms using common modules." - ], - "How does Kotlin Multiplatform facilitate code sharing between platforms?": [ - "By sharing business logic and adapting UI", - "By sharing business logic and adapting UI", - "Don't know" - ], - "How does Kotlin Multiplatform handle platform-specific implementations?": [ - "Through expect and actual declarations", - "Copy paste is the best" - ], - "What is a common use case for Kotlin Multiplatform?": [ - "Developing a cross-platform app", - "No" - ], - "What is a shared code module in Kotlin Multiplatform called?": [ - "Shared module", - "Nashi" - ], - "What is the benefit of using Kotlin Multiplatform for mobile development?": [ - "Code reuse and sharing", - "Code reuse and sharing" - ], - "What is the primary goal of Kotlin Multiplatform?": [ - "Muzukashi", - "To share code between multiple platforms", - "Nashi" - ], - "What languages can be interoperable with Kotlin Multiplatform?": [ - "Java, JavaScript, Swift", - "Zenbu" - ], - "What tooling supports Kotlin Multiplatform development?": [ - "IntelliJ IDEA, Android Studio", - "Vim" - ], - "Which platforms does Kotlin Multiplatform support?": [ - "Android, iOS, and web", - "Windows Phone" - ] - } - ], - "ncol": 10, - "nrow": 1 - }, "text/html": [ - " \n", - " \n", - " \n", " \n", " \n", - "
\n", - "\n", - "

DataFrame: rowsCount = 1, columnsCount = 10

\n", - "
What is the primary goal of Kotlin Multiplatform?How does Kotlin Multiplatform facilitate code sharing between platforms?Which platforms does Kotlin Multiplatform support?What is a common use case for Kotlin Multiplatform?What is a shared code module in Kotlin Multiplatform called?How does Kotlin Multiplatform handle platform-specific implementations?What languages can be interoperable with Kotlin Multiplatform?What tooling supports Kotlin Multiplatform development?What is the benefit of using Kotlin Multiplatform for mobile development?How does Kotlin Multiplatform differ from Kotlin Native and Kotlin/JS?
[Muzukashi, To share code between mul...[By sharing business logic and adapti...[Android, iOS, and web, Windows Phone][Developing a cross-platform app, No][Shared module, Nashi][Through expect and actual declaratio...[Java, JavaScript, Swift, Zenbu][IntelliJ IDEA, Android Studio, Vim][Code reuse and sharing, Code reuse a...[Kotlin Multiplatform allows sharing ...
\n", + "
What is the primary goal of Kotlin Multiplatform?How does Kotlin Multiplatform facilitate code sharing between platforms?Which platforms does Kotlin Multiplatform support?What is a common use case for Kotlin Multiplatform?What is a shared code module in Kotlin Multiplatform called?How does Kotlin Multiplatform handle platform-specific implementations?What languages can be interoperable with Kotlin Multiplatform?What tooling supports Kotlin Multiplatform development?What is the benefit of using Kotlin Multiplatform for mobile development?How does Kotlin Multiplatform differ from Kotlin Native and Kotlin/JS?
[Muzukashi, To share code between mul...[By sharing business logic and adapti...[Android, iOS, and web, Windows Phone][Developing a cross-platform app, No][Shared module, Nashi][Through expect and actual declaratio...[Java, JavaScript, Swift, Zenbu][IntelliJ IDEA, Android Studio, Vim][Code reuse and sharing, Code reuse a...[Kotlin Multiplatform allows sharing ...
\n", " \n", " \n", " " - ] + ], + "application/kotlindataframe+json": "{\"$version\":\"2.0.0\",\"metadata\":{\"columns\":[\"What is the primary goal of Kotlin Multiplatform?\",\"How does Kotlin Multiplatform facilitate code sharing between platforms?\",\"Which platforms does Kotlin Multiplatform support?\",\"What is a common use case for Kotlin Multiplatform?\",\"What is a shared code module in Kotlin Multiplatform called?\",\"How does Kotlin Multiplatform handle platform-specific implementations?\",\"What languages can be interoperable with Kotlin Multiplatform?\",\"What tooling supports Kotlin Multiplatform development?\",\"What is the benefit of using Kotlin Multiplatform for mobile development?\",\"How does Kotlin Multiplatform differ from Kotlin Native and Kotlin/JS?\"],\"nrow\":1,\"ncol\":10},\"kotlin_dataframe\":[{\"What is the primary goal of Kotlin Multiplatform?\":[\"Muzukashi\",\"To share code between multiple platforms\",\"Nashi\"],\"How does Kotlin Multiplatform facilitate code sharing between platforms?\":[\"By sharing business logic and adapting UI\",\"By sharing business logic and adapting UI\",\"Don't know\"],\"Which platforms does Kotlin Multiplatform support?\":[\"Android, iOS, and web\",\"Windows Phone\"],\"What is a common use case for Kotlin Multiplatform?\":[\"Developing a cross-platform app\",\"No\"],\"What is a shared code module in Kotlin Multiplatform called?\":[\"Shared module\",\"Nashi\"],\"How does Kotlin Multiplatform handle platform-specific implementations?\":[\"Through expect and actual declarations\",\"Copy paste is the best\"],\"What languages can be interoperable with Kotlin Multiplatform?\":[\"Java, JavaScript, Swift\",\"Zenbu\"],\"What tooling supports Kotlin Multiplatform development?\":[\"IntelliJ IDEA, Android Studio\",\"Vim\"],\"What is the benefit of using Kotlin Multiplatform for mobile development?\":[\"Code reuse and sharing\",\"Code reuse and sharing\"],\"How does Kotlin Multiplatform differ from Kotlin Native and Kotlin/JS?\":[\"Kotlin Multiplatform allows sharing code between different platforms using common modules.\",\"Kotlin Multiplatform allows sharing code between different platforms using common modules.\"]}]}" }, - "execution_count": 7, + "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], - "source": [ - "val table = DataFrame.readJson(\"http://localhost:8080/table\")\n", - "table" - ] + "execution_count": 4 } ], "metadata": { diff --git a/material/rest-api-ui-ktor-quiz-collector/src/main/kotlin/example/com/QuizCollector.kt b/material/rest-api-ui-ktor-quiz-collector/src/main/kotlin/example/com/QuizCollector.kt index 36e6ee19..dcc94923 100644 --- a/material/rest-api-ui-ktor-quiz-collector/src/main/kotlin/example/com/QuizCollector.kt +++ b/material/rest-api-ui-ktor-quiz-collector/src/main/kotlin/example/com/QuizCollector.kt @@ -17,13 +17,23 @@ fun Application.configureQuizCollector() { call.respond(CollectResponse(quizResponse.getScore())) } - get("/results") { + get("/responses") { + call.respond(quizResponses.map { quizResponse -> + quizResponse.responses.fold(mutableMapOf()) { acc, element -> + acc[element.question] = element.answer + acc + } + }) + } + + get("/table2") { call.respond(quizResponses) } get("/table") { - call.respond(quizResponses.flatMap { it.responses }.groupBy { it.question } - .mapValues { it.value.map { it.answer } }) + val result = quizResponses.flatMap { it.responses }.groupBy { it.question } + .mapValues { it.value.map { value -> value.answer } } + call.respond(result) } get("/ui") { diff --git a/material/rest-api-ui-ktor-quiz-collector/src/main/resources/application.yaml b/material/rest-api-ui-ktor-quiz-collector/src/main/resources/application.yaml index 3689c620..4ac0a499 100644 --- a/material/rest-api-ui-ktor-quiz-collector/src/main/resources/application.yaml +++ b/material/rest-api-ui-ktor-quiz-collector/src/main/resources/application.yaml @@ -1,6 +1,9 @@ ktor: - application: - modules: - - example.com.ApplicationKt.module - deployment: - port: 8080 + development: true + application: + modules: + - example.com.ApplicationKt.module + deployment: + port: 8080 + watch: + - classes