Last active
February 7, 2020 18:37
-
-
Save shadaj/0af341e0d82b838f52f56423950365b4 to your computer and use it in GitHub Desktop.
Circe Colab Demo.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"name": "Circe Colab Demo.ipynb", | |
"version": "0.3.2", | |
"provenance": [], | |
"collapsed_sections": [], | |
"toc_visible": true, | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"display_name": "Scala", | |
"language": "scala", | |
"name": "scala" | |
} | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/shadaj/0af341e0d82b838f52f56423950365b4/circe-colab-demo.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "up2G8zDbhsEN", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"# Running Circe in Colab\n", | |
"\n", | |
"In this notebook, we'll take a look at how to import libraries into a Scala notebook. Here, we'll load up [Circe](https://circe.github.io/circe/), a library for handling JSON data in Scala." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "sL1Vq6r6J6dA", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"## Install the Scala Kernel\n", | |
"If you get a \"scala\" kernel not recognized warning when loading up the notebook for the first time, start by running the two cells below. Once you are done **reload the page** to load the notebook in the installed Scala kernel." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "RoCcdvTCAqeR", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"%%shell\n", | |
"SCALA_VERSION=2.12.8 ALMOND_VERSION=0.3.0+16-548dc10f-SNAPSHOT\n", | |
"curl -Lo coursier https://git.io/coursier-cli\n", | |
"chmod +x coursier\n", | |
"./coursier bootstrap \\\n", | |
" -r jitpack -r sonatype:snapshots \\\n", | |
" -i user -I user:sh.almond:scala-kernel-api_$SCALA_VERSION:$ALMOND_VERSION \\\n", | |
" sh.almond:scala-kernel_$SCALA_VERSION:$ALMOND_VERSION \\\n", | |
" --sources --default=true \\\n", | |
" -o almond-snapshot --embed-files=false\n", | |
"rm coursier\n", | |
"./almond-snapshot --install --global --force\n", | |
"rm almond-snapshot" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "BZchIo9sArF4", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"%%shell\n", | |
"echo \"{\n", | |
" \\\"language\\\" : \\\"scala\\\",\n", | |
" \\\"display_name\\\" : \\\"Scala\\\",\n", | |
" \\\"argv\\\" : [\n", | |
" \\\"bash\\\",\n", | |
" \\\"-c\\\",\n", | |
" \\\"env LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libpython3.6m.so:\\$LD_PRELOAD java -jar /usr/local/share/jupyter/kernels/scala/launcher.jar --connection-file {connection_file}\\\"\n", | |
" ]\n", | |
"}\" > /usr/local/share/jupyter/kernels/scala/kernel.json" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "sgPteJTmOkhU", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"## Use Circe" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "p-0RXR7aFFDA", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"First, we'll import Circe into the notebook." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "a9z0PxRg_iOp", | |
"colab_type": "code", | |
"outputId": "9eed0f5d-2883-4c0d-e336-90e684b998c0", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 85 | |
} | |
}, | |
"source": [ | |
"import $ivy.`io.circe::circe-core:0.10.0`, $ivy.`io.circe::circe-generic:0.10.0`, $ivy.`io.circe::circe-parser:0.10.0`\n", | |
"\n", | |
"import io.circe._, io.circe.generic.auto._, io.circe.parser._, io.circe.syntax._" | |
], | |
"execution_count": 0, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"\u001b[32mimport \u001b[39m\u001b[36m$ivy.$ , $ivy.$ , $ivy.$ \n", | |
"\n", | |
"\u001b[39m\n", | |
"\u001b[32mimport \u001b[39m\u001b[36mio.circe._, io.circe.generic.auto._, io.circe.parser._, io.circe.syntax._\u001b[39m" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 1 | |
} | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "L8qlVXHYFHkq", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"With Circe loaded, we can convert a Scala object to JSON!" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "XRUL2lxrFKi5", | |
"colab_type": "code", | |
"outputId": "c2cc2120-6351-4333-b785-1818bf575c8a", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 102 | |
} | |
}, | |
"source": [ | |
"case class Qux(i: Int, d: Option[Double])\n", | |
"\n", | |
"val json = Qux(13, Some(14.0)).asJson.spaces2" | |
], | |
"execution_count": 0, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"defined \u001b[32mclass\u001b[39m \u001b[36mQux\u001b[39m\n", | |
"\u001b[36mjson\u001b[39m: \u001b[32mString\u001b[39m = \u001b[32m\"\"\"{\n", | |
" \"i\" : 13,\n", | |
" \"d\" : 14.0\n", | |
"}\"\"\"\u001b[39m" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 2 | |
} | |
] | |
} | |
] | |
} |
@mdvik did you run the installer cells first and then reload the page? The error you saw is usually what happens when the Scala kernel isn't detected.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you for your sharing, it is an awesome work!
However I encountered the following error and wonder if you can help to sort it out? Screenshot is below:
https://snipboard.io/ZCueN8.jpg