Skip to content

Instantly share code, notes, and snippets.

@pafonta
Created November 29, 2017 14:52
Show Gist options
  • Save pafonta/c4c46729c808976cea7d240b1502c439 to your computer and use it in GitHub Desktop.
Save pafonta/c4c46729c808976cea7d240b1502c439 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Print formatted and colorized JSON-LD responses"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"author: @pafonta"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Python version"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Python 3.6.3 :: Anaconda, Inc.\r\n"
]
}
],
"source": [
"!python -V"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Create a fake JSON-LD object\n",
"\n",
"Example \"Activity\" from https://json-ld.org/playground/."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import json"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"json_obj = json.loads(\"\"\"{\"@context\":\"http://www.w3.org/ns/activitystreams\",\"@type\":\"Create\",\"actor\":{\"@type\":\"\"\"\n",
" \"\"\"\"Person\",\"@id\":\"acct:sally@example.org\",\"displayName\":\"Sally\"},\"object\":{\"@type\":\"Note\",\"\"\"\n",
" \"\"\"\"content\":\"This is a simple note\"},\"published\":\"2015-01-25T12:34:56Z\"}\"\"\")"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'@context': 'http://www.w3.org/ns/activitystreams',\n",
" '@type': 'Create',\n",
" 'actor': {'@id': 'acct:sally@example.org',\n",
" '@type': 'Person',\n",
" 'displayName': 'Sally'},\n",
" 'object': {'@type': 'Note', 'content': 'This is a simple note'},\n",
" 'published': '2015-01-25T12:34:56Z'}"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"json_obj"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Pretty format the JSON-LD object"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"json_str = json.dumps(json_obj, indent=2)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{\n",
" \"@context\": \"http://www.w3.org/ns/activitystreams\",\n",
" \"@type\": \"Create\",\n",
" \"actor\": {\n",
" \"@type\": \"Person\",\n",
" \"@id\": \"acct:sally@example.org\",\n",
" \"displayName\": \"Sally\"\n",
" },\n",
" \"object\": {\n",
" \"@type\": \"Note\",\n",
" \"content\": \"This is a simple note\"\n",
" },\n",
" \"published\": \"2015-01-25T12:34:56Z\"\n",
"}\n"
]
}
],
"source": [
"print(json_str)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Use Pygments to color the JSON-LD pretty formatted string\n",
"\n",
"pip install pygments\n",
"\n",
"http://pygments.org/"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"from pygments import highlight\n",
"from pygments.lexers import JsonLdLexer\n",
"from pygments.formatters import TerminalFormatter, TerminalTrueColorFormatter"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"lexer = JsonLdLexer()"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{\n",
" \u001b[30;01m\"@context\"\u001b[39;49;00m: \u001b[33m\"http://www.w3.org/ns/activitystreams\"\u001b[39;49;00m,\n",
" \u001b[30;01m\"@type\"\u001b[39;49;00m: \u001b[33m\"Create\"\u001b[39;49;00m,\n",
" \u001b[34;01m\"actor\"\u001b[39;49;00m: {\n",
" \u001b[30;01m\"@type\"\u001b[39;49;00m: \u001b[33m\"Person\"\u001b[39;49;00m,\n",
" \u001b[30;01m\"@id\"\u001b[39;49;00m: \u001b[33m\"acct:sally@example.org\"\u001b[39;49;00m,\n",
" \u001b[34;01m\"displayName\"\u001b[39;49;00m: \u001b[33m\"Sally\"\u001b[39;49;00m\n",
" },\n",
" \u001b[34;01m\"object\"\u001b[39;49;00m: {\n",
" \u001b[30;01m\"@type\"\u001b[39;49;00m: \u001b[33m\"Note\"\u001b[39;49;00m,\n",
" \u001b[34;01m\"content\"\u001b[39;49;00m: \u001b[33m\"This is a simple note\"\u001b[39;49;00m\n",
" },\n",
" \u001b[34;01m\"published\"\u001b[39;49;00m: \u001b[33m\"2015-01-25T12:34:56Z\"\u001b[39;49;00m\n",
"}\n",
"\n"
]
}
],
"source": [
"print(highlight(json_str, lexer, TerminalFormatter()))"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{\n",
" \u001b[38;2;170;34;255m\"@context\"\u001b[39m: \u001b[38;2;186;33;33m\"http://www.w3.org/ns/activitystreams\"\u001b[39m,\n",
" \u001b[38;2;170;34;255m\"@type\"\u001b[39m: \u001b[38;2;186;33;33m\"Create\"\u001b[39m,\n",
" \u001b[38;2;0;128;0;01m\"actor\"\u001b[39;00m: {\n",
" \u001b[38;2;170;34;255m\"@type\"\u001b[39m: \u001b[38;2;186;33;33m\"Person\"\u001b[39m,\n",
" \u001b[38;2;170;34;255m\"@id\"\u001b[39m: \u001b[38;2;186;33;33m\"acct:sally@example.org\"\u001b[39m,\n",
" \u001b[38;2;0;128;0;01m\"displayName\"\u001b[39;00m: \u001b[38;2;186;33;33m\"Sally\"\u001b[39m\n",
" },\n",
" \u001b[38;2;0;128;0;01m\"object\"\u001b[39;00m: {\n",
" \u001b[38;2;170;34;255m\"@type\"\u001b[39m: \u001b[38;2;186;33;33m\"Note\"\u001b[39m,\n",
" \u001b[38;2;0;128;0;01m\"content\"\u001b[39;00m: \u001b[38;2;186;33;33m\"This is a simple note\"\u001b[39m\n",
" },\n",
" \u001b[38;2;0;128;0;01m\"published\"\u001b[39;00m: \u001b[38;2;186;33;33m\"2015-01-25T12:34:56Z\"\u001b[39m\n",
"}\n",
"\n"
]
}
],
"source": [
"print(highlight(json_str, lexer, TerminalTrueColorFormatter()))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
@pafonta
Copy link
Author

pafonta commented Nov 29, 2017

The output of the 9th cell is colorized but the GitHub rendering doesn't show it.
Here it is rendered correctly: https://nbviewer.jupyter.org/gist/pafonta/c4c46729c808976cea7d240b1502c439.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment