I’m trying to include another library (dataframe) ...
# datascience
p
I’m trying to include another library (dataframe) into my own library (roboquant). But there are conflicting import statements in the Jupyter Integration implementations and I cannot load (%use) my library in a Jupyter notebook. The error is
Conflicting import, imported name 'Instant' is ambiguous
In this particular case I want to import
java.time.Instant
, but dataframe wants to import
kotlinx.datetime.Instant
Is there a way I can manipulate/disable the import statements of the other library (dataframe) before they are executed (or another work around)?
i
You actually can, but it will be rather inconvenient to do. First, you need to disable auto-loading of library definition itself by specifying typename rule (as it's done here for kandy: https://github.com/Kotlin/kotlin-jupyter-libraries/blob/master/kandy.json#L10L12). For Kotlin API, same feature is available Then, you need to obtain a definition:
Copy code
fun getDataframeIntegration(
    lastCellClass: KClass<*>,
    notebook: Notebook,
    version: String
): LibraryDefinition {
    val classLoader = lastCellClass.java.classLoader
    val integrationClass = classLoader.loadClass("org.jetbrains.kotlinx.dataframe.jupyter.Integration")
    val integrationConstructor = integrationClass.constructors.single()
    val integrationProducer = integrationConstructor.newInstance(notebook, mapOf("v" to version)) as LibraryDefinitionProducer
    val dataframeDefinition = integrationProducer.getDefinitions(notebook).first()
    return dataframeDefinition
}
In the notebook context (considering dataframe dependencies were loaded) it should be called like this:
Copy code
val dfIntegration = getDataframeIntegration(this::class, notebook, "0.11.0-dev-1608")
Then, obtained dataframe integration should be used to construct new integration:
Copy code
val newIntegration = libraryDefinition {
    it.afterCellExecution = dfIntegration.afterCellExecution
    it.converters = dfIntegration.converters
    // <do not copy "imports">...
}
And finally, this new integration should be loaded:
Copy code
USE {
    onLoaded {
        scheduleExecution {
            addLibrary(newIntegration)
        }
    }
}
So probably it doesn't worth to do. I hope we'll design better API later
I think I'll extend
notebook.libraryLoader
to load definitions without adding to the context. And also will add a data-class-like copy function for LibraryDefinition
p
@Ilya Muradyan thanks, for now will also look at some alternatives (for example see if roboquant can move to Kotlin Instant type or a fork of DataFrame).