Skip to content

Commit

Permalink
limited series ktor3 release for kap
Browse files Browse the repository at this point in the history
  • Loading branch information
altavir committed Dec 18, 2024
1 parent 6dd0ec4 commit 062bec9
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 39 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ val plotlyVersion by extra("2.29.0")

allprojects {
group = "space.kscience"
version = "0.7.1.1"
version = "0.7.1.1-ktor3"
}

apiValidation {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ package space.kscience.plotly.fx
import io.ktor.server.engine.ApplicationEngine
import javafx.beans.property.SimpleIntegerProperty
import javafx.beans.property.SimpleStringProperty
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.*
import tornadofx.*
import java.net.URI

Expand Down Expand Up @@ -35,9 +32,9 @@ class PlotlyFXController : Controller() {

val address = SimpleStringProperty()

fun displayPage(page: String) {
fun displayPage(page: String) = runBlocking {
server?.let {
val connector = it.environment.connectors.first()
val connector = it.resolvedConnectors().first()
val uri = URI("http", null, connector.host, connector.port, null, null, null)
address.set("$uri/$page")
}
Expand Down
92 changes: 69 additions & 23 deletions examples/notebooks/plotlykt-demo.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,22 @@
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import space.kscience.plotly.PlotlyIntegration\n",
"\n",
"val integration = PlotlyIntegration()\n",
"\n",
"USE(integration.getDefinitions(notebook).first())\n",
"//%use plotly"
]
},
{
"cell_type": "code",
"outputs": [],
"source": [
"Plotly.jupyter.notebook()"
"USE(PlotlyIntegration())\n",
"//@file:CompilerArgs(\"-jvm-target=11\")\n",
"//@file:Repository(\"https://repo.kotlin.link\")\n",
"//@file:DependsOn(\"space.kscience:plotlykt-jupyter-jvm:0.7.1\")"
],
"metadata": {
"collapsed": false
},
"outputs": [],
"execution_count": null
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"import kotlin.math.*\n",
"\n",
Expand Down Expand Up @@ -59,14 +45,74 @@
" }\n",
"\n",
"}"
]
],
"outputs": [],
"execution_count": null
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"source": [
"HTML(\n",
" \"\"\"\n",
" <div id=\"debug\">\n",
" debug\n",
" </div>\n",
" \"\"\".trimIndent()\n",
")"
],
"metadata": {
"collapsed": false
},
"outputs": [],
"source": []
"execution_count": null
},
{
"cell_type": "code",
"source": [
"HTML(\"\"\"\n",
"<script type = \"text/javascript\">\n",
" element = document.getElementById(\"debug\")\n",
" element.append(window.Plotly)\n",
" element.append(window.plotlyConnect)\n",
"</script>\n",
" \"\"\".trimIndent())"
],
"metadata": {
"collapsed": false
},
"outputs": [],
"execution_count": null
},
{
"cell_type": "code",
"source": [],
"metadata": {
"collapsed": false
},
"outputs": [],
"execution_count": null
},
{
"cell_type": "code",
"source": [
"HTML(\"\"\"\n",
"<script src=\"https://cdn.plot.ly/plotly-2.29.1.min.js\" charset=\"utf-8\"></script>\n",
"\"\"\")"
],
"metadata": {
"collapsed": false
},
"outputs": [],
"execution_count": null
},
{
"cell_type": "code",
"source": [],
"metadata": {
"collapsed": false
},
"outputs": [],
"execution_count": null
}
],
"metadata": {
Expand Down
4 changes: 1 addition & 3 deletions plotlykt-server/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import space.kscience.gradle.KScienceVersions

plugins {
id("space.kscience.gradle.mpp")
kotlin("jupyter.api")
`maven-publish`
}

val dataforgeVersion: String by rootProject.extra
val ktorVersion = KScienceVersions.ktorVersion
val ktorVersion = "3.0.2"

kscience{
jvm()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package space.kscience.plotly.server

import io.ktor.http.*
import io.ktor.server.application.*
import io.ktor.server.cio.CIO
import io.ktor.server.engine.ApplicationEngine
import io.ktor.server.engine.embeddedServer
import io.ktor.server.html.respondHtml
Expand All @@ -17,6 +18,7 @@ import io.ktor.websocket.Frame
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.runBlocking
import kotlinx.html.*
import kotlinx.serialization.json.JsonObject
import space.kscience.dataforge.meta.*
Expand All @@ -25,7 +27,6 @@ import space.kscience.plotly.*
import space.kscience.plotly.server.PlotlyServer.Companion.DEFAULT_PAGE
import java.awt.Desktop
import java.net.URI
import kotlin.collections.set
import kotlin.coroutines.CoroutineContext

public enum class PlotlyUpdateMode {
Expand Down Expand Up @@ -273,7 +274,7 @@ public fun Application.plotlyModule(route: String = DEFAULT_PAGE, block: PlotlyS
}

// val root: Route = feature(Routing).createRouteFromPath(route)
return PlotlyServer(plugin(Routing), route).apply(block)
return PlotlyServer(plugin(RoutingRoot), route).apply(block)
}


Expand Down Expand Up @@ -304,14 +305,14 @@ public fun Plotly.serve(
host: String = "localhost",
port: Int = 7777,
block: PlotlyServer.() -> Unit,
): ApplicationEngine = scope.embeddedServer(io.ktor.server.cio.CIO, port, host) {
): ApplicationEngine = scope.embeddedServer(CIO, port, host) {
// install(CallLogging)
install(CORS) {
anyHost()
}

plotlyModule(block = block)
}.start()
}.start().engine

/**
* A shortcut to make a single plot at the default page
Expand All @@ -328,8 +329,8 @@ public fun PlotlyServer.plot(
}
}

public fun ApplicationEngine.show() {
val connector = environment.connectors.first()
public fun ApplicationEngine.show(): Unit = runBlocking {
val connector = resolvedConnectors().first()
val uri = URI("http", null, connector.host, connector.port, null, null, null)
Desktop.getDesktop().browse(uri)
}
Expand Down

0 comments on commit 062bec9

Please sign in to comment.