Skip to content

Instantly share code, notes, and snippets.

@akshaykhadse
Last active May 6, 2024 09:19
Show Gist options
  • Save akshaykhadse/7acc91dd41f52944c6150754e5530c4b to your computer and use it in GitHub Desktop.
Save akshaykhadse/7acc91dd41f52944c6150754e5530c4b to your computer and use it in GitHub Desktop.
C++ Google Colab Plugin

C++ Google Colab Plugin

Example notebook can be found here.

Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "CPP.ipynb",
"provenance": [],
"collapsed_sections": [],
"toc_visible": true,
"authorship_tag": "ABX9TyOMBpgP7RZdGP+gGyEZd4ew",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/akshaykhadse/7acc91dd41f52944c6150754e5530c4b/cpp.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "shmDs3e3BOrc"
},
"source": [
"# C++ magic for Google Colab\n",
"Regular syntax highlighting options available for IPython and Jupyter doesen't seem to work with the Google Colab. So, I created a work around of sorts.\n",
"The plugin used in this notebook can print syntax highlghted C++ code."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "63xBUG5tCGHg"
},
"source": [
"## Setup"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "zfoPP8qkBvra"
},
"source": [
"### Get the C++ magic command plugin."
]
},
{
"cell_type": "code",
"metadata": {
"id": "CpE6eLtlgQtE",
"outputId": "743f60a8-d510-4079-a3c5-78897b9235a4",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 301
}
},
"source": [
"!wget -O cpp_plugin.py https://gist.github.com/akshaykhadse/7acc91dd41f52944c6150754e5530c4b/raw/cpp_plugin.py"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"--2020-07-28 14:42:48-- https://gist.github.com/akshaykhadse/7acc91dd41f52944c6150754e5530c4b/raw/cpp_plugin.py\n",
"Resolving gist.github.com (gist.github.com)... 140.82.118.4\n",
"Connecting to gist.github.com (gist.github.com)|140.82.118.4|:443... connected.\n",
"HTTP request sent, awaiting response... 301 Moved Permanently\n",
"Location: https://gist.githubusercontent.com/akshaykhadse/7acc91dd41f52944c6150754e5530c4b/raw/cpp_plugin.py [following]\n",
"--2020-07-28 14:42:48-- https://gist.githubusercontent.com/akshaykhadse/7acc91dd41f52944c6150754e5530c4b/raw/cpp_plugin.py\n",
"Resolving gist.githubusercontent.com (gist.githubusercontent.com)... 151.101.0.133, 151.101.64.133, 151.101.128.133, ...\n",
"Connecting to gist.githubusercontent.com (gist.githubusercontent.com)|151.101.0.133|:443... connected.\n",
"HTTP request sent, awaiting response... 200 OK\n",
"Length: 2730 (2.7K) [text/plain]\n",
"Saving to: ‘cpp_plugin.py’\n",
"\n",
"\rcpp_plugin.py 0%[ ] 0 --.-KB/s \rcpp_plugin.py 100%[===================>] 2.67K --.-KB/s in 0s \n",
"\n",
"2020-07-28 14:42:48 (12.0 MB/s) - ‘cpp_plugin.py’ saved [2730/2730]\n",
"\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "ZCFCfE3kCAf5"
},
"source": [
"### Load the plugin"
]
},
{
"cell_type": "code",
"metadata": {
"id": "WGgFM6ZFAIgN"
},
"source": [
"%load_ext cpp_plugin"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "St9kxRcLCOK_"
},
"source": [
"## Usage"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "BHnULhmMCP48"
},
"source": [
"### Create source file\n",
"Use the cell magic `%%cpp -n <filename>.cpp` to create a file with the contents of the cell and then display the syntax highlighted code in output. If `-n` is not used, then `src.cpp` will be used as default filename. "
]
},
{
"cell_type": "code",
"metadata": {
"id": "CUv5nwF0mAdl",
"cellView": "both",
"outputId": "ce48820f-1a93-4e1e-f2a2-34aadf0ffb78",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 204
}
},
"source": [
"#@title Refer output of this cell. Double-click to hide source.\n",
"%%cpp -n test.cpp\n",
"#include <iostream>\n",
"\n",
"int foo();\n",
"int bar();\n",
"\n",
"using namespace std;\n",
"int main() {\n",
" foo();\n",
" bar();\n",
"}"
],
"execution_count": null,
"outputs": [
{
"output_type": "display_data",
"data": {
"text/html": [
"<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\"\n",
" \"http://www.w3.org/TR/html4/strict.dtd\">\n",
"\n",
"<html>\n",
"<head>\n",
" <title></title>\n",
" <meta http-equiv=\"content-type\" content=\"text/html; charset=None\">\n",
" <style type=\"text/css\">\n",
"td.linenos { background-color: #f0f0f0; padding-right: 10px; }\n",
"span.lineno { background-color: #f0f0f0; padding: 0 5px 0 5px; }\n",
"pre { line-height: 125%; }\n",
"body .hll { background-color: #4f424c }\n",
"body .c { color: #776e71 } /* Comment */\n",
"body .err { color: #ef6155 } /* Error */\n",
"body .k { color: #815ba4 } /* Keyword */\n",
"body .l { color: #f99b15 } /* Literal */\n",
"body .n { color: #e7e9db } /* Name */\n",
"body .o { color: #5bc4bf } /* Operator */\n",
"body .p { color: #e7e9db } /* Punctuation */\n",
"body .ch { color: #776e71 } /* Comment.Hashbang */\n",
"body .cm { color: #776e71 } /* Comment.Multiline */\n",
"body .cp { color: #776e71 } /* Comment.Preproc */\n",
"body .cpf { color: #776e71 } /* Comment.PreprocFile */\n",
"body .c1 { color: #776e71 } /* Comment.Single */\n",
"body .cs { color: #776e71 } /* Comment.Special */\n",
"body .gd { color: #ef6155 } /* Generic.Deleted */\n",
"body .ge { font-style: italic } /* Generic.Emph */\n",
"body .gh { color: #e7e9db; font-weight: bold } /* Generic.Heading */\n",
"body .gi { color: #48b685 } /* Generic.Inserted */\n",
"body .gp { color: #776e71; font-weight: bold } /* Generic.Prompt */\n",
"body .gs { font-weight: bold } /* Generic.Strong */\n",
"body .gu { color: #5bc4bf; font-weight: bold } /* Generic.Subheading */\n",
"body .kc { color: #815ba4 } /* Keyword.Constant */\n",
"body .kd { color: #815ba4 } /* Keyword.Declaration */\n",
"body .kn { color: #5bc4bf } /* Keyword.Namespace */\n",
"body .kp { color: #815ba4 } /* Keyword.Pseudo */\n",
"body .kr { color: #815ba4 } /* Keyword.Reserved */\n",
"body .kt { color: #fec418 } /* Keyword.Type */\n",
"body .ld { color: #48b685 } /* Literal.Date */\n",
"body .m { color: #f99b15 } /* Literal.Number */\n",
"body .s { color: #48b685 } /* Literal.String */\n",
"body .na { color: #06b6ef } /* Name.Attribute */\n",
"body .nb { color: #e7e9db } /* Name.Builtin */\n",
"body .nc { color: #fec418 } /* Name.Class */\n",
"body .no { color: #ef6155 } /* Name.Constant */\n",
"body .nd { color: #5bc4bf } /* Name.Decorator */\n",
"body .ni { color: #e7e9db } /* Name.Entity */\n",
"body .ne { color: #ef6155 } /* Name.Exception */\n",
"body .nf { color: #06b6ef } /* Name.Function */\n",
"body .nl { color: #e7e9db } /* Name.Label */\n",
"body .nn { color: #fec418 } /* Name.Namespace */\n",
"body .nx { color: #06b6ef } /* Name.Other */\n",
"body .py { color: #e7e9db } /* Name.Property */\n",
"body .nt { color: #5bc4bf } /* Name.Tag */\n",
"body .nv { color: #ef6155 } /* Name.Variable */\n",
"body .ow { color: #5bc4bf } /* Operator.Word */\n",
"body .w { color: #e7e9db } /* Text.Whitespace */\n",
"body .mb { color: #f99b15 } /* Literal.Number.Bin */\n",
"body .mf { color: #f99b15 } /* Literal.Number.Float */\n",
"body .mh { color: #f99b15 } /* Literal.Number.Hex */\n",
"body .mi { color: #f99b15 } /* Literal.Number.Integer */\n",
"body .mo { color: #f99b15 } /* Literal.Number.Oct */\n",
"body .sb { color: #48b685 } /* Literal.String.Backtick */\n",
"body .sc { color: #e7e9db } /* Literal.String.Char */\n",
"body .sd { color: #776e71 } /* Literal.String.Doc */\n",
"body .s2 { color: #48b685 } /* Literal.String.Double */\n",
"body .se { color: #f99b15 } /* Literal.String.Escape */\n",
"body .sh { color: #48b685 } /* Literal.String.Heredoc */\n",
"body .si { color: #f99b15 } /* Literal.String.Interpol */\n",
"body .sx { color: #48b685 } /* Literal.String.Other */\n",
"body .sr { color: #48b685 } /* Literal.String.Regex */\n",
"body .s1 { color: #48b685 } /* Literal.String.Single */\n",
"body .ss { color: #48b685 } /* Literal.String.Symbol */\n",
"body .bp { color: #e7e9db } /* Name.Builtin.Pseudo */\n",
"body .vc { color: #ef6155 } /* Name.Variable.Class */\n",
"body .vg { color: #ef6155 } /* Name.Variable.Global */\n",
"body .vi { color: #ef6155 } /* Name.Variable.Instance */\n",
"body .il { color: #f99b15 } /* Literal.Number.Integer.Long */\n",
"\n",
" </style>\n",
"</head>\n",
"<body>\n",
"<h2></h2>\n",
"\n",
"<div class=\"highlight\"><pre><span></span><span class=\"cp\">#include</span> <span class=\"cpf\">&lt;iostream&gt;</span><span class=\"cp\"></span>\n",
"\n",
"<span class=\"kt\">int</span> <span class=\"nf\">foo</span><span class=\"p\">();</span>\n",
"<span class=\"kt\">int</span> <span class=\"nf\">bar</span><span class=\"p\">();</span>\n",
"\n",
"<span class=\"k\">using</span> <span class=\"k\">namespace</span> <span class=\"n\">std</span><span class=\"p\">;</span>\n",
"<span class=\"kt\">int</span> <span class=\"nf\">main</span><span class=\"p\">()</span> <span class=\"p\">{</span>\n",
" <span class=\"n\">foo</span><span class=\"p\">();</span>\n",
" <span class=\"n\">bar</span><span class=\"p\">();</span>\n",
"<span class=\"p\">}</span>\n",
"</pre></div>\n",
"</body>\n",
"</html>\n"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {
"tags": []
}
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "qwlI7OraCkus"
},
"source": [
"### Hide unhighlighted code\n",
"\n",
"Here, `#@title` on the first line creates an empty form. Double clicking on the right hand pane hides the code cell. This can also be done using right-click > Form.\n",
"\n",
"Following is a preview of how the hidden code will look like."
]
},
{
"cell_type": "code",
"metadata": {
"id": "TYEGLFp6FSfJ",
"cellView": "form",
"outputId": "b41d979f-489c-489f-a768-5b40fdea5d22",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 102
}
},
"source": [
"#@title Refer output of this cell. Double-click to hide source.\n",
"%%cpp\n",
"\n",
"int add(int a, int b){\n",
" int c = a + b;\n",
" return c;\n",
"}"
],
"execution_count": null,
"outputs": [
{
"output_type": "display_data",
"data": {
"text/html": [
"<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\"\n",
" \"http://www.w3.org/TR/html4/strict.dtd\">\n",
"\n",
"<html>\n",
"<head>\n",
" <title></title>\n",
" <meta http-equiv=\"content-type\" content=\"text/html; charset=None\">\n",
" <style type=\"text/css\">\n",
"td.linenos { background-color: #f0f0f0; padding-right: 10px; }\n",
"span.lineno { background-color: #f0f0f0; padding: 0 5px 0 5px; }\n",
"pre { line-height: 125%; }\n",
"body .hll { background-color: #4f424c }\n",
"body .c { color: #776e71 } /* Comment */\n",
"body .err { color: #ef6155 } /* Error */\n",
"body .k { color: #815ba4 } /* Keyword */\n",
"body .l { color: #f99b15 } /* Literal */\n",
"body .n { color: #e7e9db } /* Name */\n",
"body .o { color: #5bc4bf } /* Operator */\n",
"body .p { color: #e7e9db } /* Punctuation */\n",
"body .ch { color: #776e71 } /* Comment.Hashbang */\n",
"body .cm { color: #776e71 } /* Comment.Multiline */\n",
"body .cp { color: #776e71 } /* Comment.Preproc */\n",
"body .cpf { color: #776e71 } /* Comment.PreprocFile */\n",
"body .c1 { color: #776e71 } /* Comment.Single */\n",
"body .cs { color: #776e71 } /* Comment.Special */\n",
"body .gd { color: #ef6155 } /* Generic.Deleted */\n",
"body .ge { font-style: italic } /* Generic.Emph */\n",
"body .gh { color: #e7e9db; font-weight: bold } /* Generic.Heading */\n",
"body .gi { color: #48b685 } /* Generic.Inserted */\n",
"body .gp { color: #776e71; font-weight: bold } /* Generic.Prompt */\n",
"body .gs { font-weight: bold } /* Generic.Strong */\n",
"body .gu { color: #5bc4bf; font-weight: bold } /* Generic.Subheading */\n",
"body .kc { color: #815ba4 } /* Keyword.Constant */\n",
"body .kd { color: #815ba4 } /* Keyword.Declaration */\n",
"body .kn { color: #5bc4bf } /* Keyword.Namespace */\n",
"body .kp { color: #815ba4 } /* Keyword.Pseudo */\n",
"body .kr { color: #815ba4 } /* Keyword.Reserved */\n",
"body .kt { color: #fec418 } /* Keyword.Type */\n",
"body .ld { color: #48b685 } /* Literal.Date */\n",
"body .m { color: #f99b15 } /* Literal.Number */\n",
"body .s { color: #48b685 } /* Literal.String */\n",
"body .na { color: #06b6ef } /* Name.Attribute */\n",
"body .nb { color: #e7e9db } /* Name.Builtin */\n",
"body .nc { color: #fec418 } /* Name.Class */\n",
"body .no { color: #ef6155 } /* Name.Constant */\n",
"body .nd { color: #5bc4bf } /* Name.Decorator */\n",
"body .ni { color: #e7e9db } /* Name.Entity */\n",
"body .ne { color: #ef6155 } /* Name.Exception */\n",
"body .nf { color: #06b6ef } /* Name.Function */\n",
"body .nl { color: #e7e9db } /* Name.Label */\n",
"body .nn { color: #fec418 } /* Name.Namespace */\n",
"body .nx { color: #06b6ef } /* Name.Other */\n",
"body .py { color: #e7e9db } /* Name.Property */\n",
"body .nt { color: #5bc4bf } /* Name.Tag */\n",
"body .nv { color: #ef6155 } /* Name.Variable */\n",
"body .ow { color: #5bc4bf } /* Operator.Word */\n",
"body .w { color: #e7e9db } /* Text.Whitespace */\n",
"body .mb { color: #f99b15 } /* Literal.Number.Bin */\n",
"body .mf { color: #f99b15 } /* Literal.Number.Float */\n",
"body .mh { color: #f99b15 } /* Literal.Number.Hex */\n",
"body .mi { color: #f99b15 } /* Literal.Number.Integer */\n",
"body .mo { color: #f99b15 } /* Literal.Number.Oct */\n",
"body .sb { color: #48b685 } /* Literal.String.Backtick */\n",
"body .sc { color: #e7e9db } /* Literal.String.Char */\n",
"body .sd { color: #776e71 } /* Literal.String.Doc */\n",
"body .s2 { color: #48b685 } /* Literal.String.Double */\n",
"body .se { color: #f99b15 } /* Literal.String.Escape */\n",
"body .sh { color: #48b685 } /* Literal.String.Heredoc */\n",
"body .si { color: #f99b15 } /* Literal.String.Interpol */\n",
"body .sx { color: #48b685 } /* Literal.String.Other */\n",
"body .sr { color: #48b685 } /* Literal.String.Regex */\n",
"body .s1 { color: #48b685 } /* Literal.String.Single */\n",
"body .ss { color: #48b685 } /* Literal.String.Symbol */\n",
"body .bp { color: #e7e9db } /* Name.Builtin.Pseudo */\n",
"body .vc { color: #ef6155 } /* Name.Variable.Class */\n",
"body .vg { color: #ef6155 } /* Name.Variable.Global */\n",
"body .vi { color: #ef6155 } /* Name.Variable.Instance */\n",
"body .il { color: #f99b15 } /* Literal.Number.Integer.Long */\n",
"\n",
" </style>\n",
"</head>\n",
"<body>\n",
"<h2></h2>\n",
"\n",
"<div class=\"highlight\"><pre><span></span><span class=\"kt\">int</span> <span class=\"nf\">add</span><span class=\"p\">(</span><span class=\"kt\">int</span> <span class=\"n\">a</span><span class=\"p\">,</span> <span class=\"kt\">int</span> <span class=\"n\">b</span><span class=\"p\">){</span>\n",
" <span class=\"kt\">int</span> <span class=\"n\">c</span> <span class=\"o\">=</span> <span class=\"n\">a</span> <span class=\"o\">+</span> <span class=\"n\">b</span><span class=\"p\">;</span>\n",
" <span class=\"k\">return</span> <span class=\"n\">c</span><span class=\"p\">;</span>\n",
"<span class=\"p\">}</span>\n",
"</pre></div>\n",
"</body>\n",
"</html>\n"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {
"tags": []
}
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "u93ec48s2Oxi"
},
"source": [
"### Change style\n",
"Use the option `-s <stylename>` to change syntax highlighting style.\n",
"* default\n",
"* emacs\n",
"* friendly\n",
"* colorful\n",
"* autumn\n",
"* murphy\n",
"* manni\n",
"* monokai\n",
"* perldoc\n",
"* pastie\n",
"* borland\n",
"* trac\n",
"* native\n",
"* fruity\n",
"* bw\n",
"* vim\n",
"* vs\n",
"* tango\n",
"* rrt\n",
"* xcode\n",
"* igor\n",
"* paraiso-light\n",
"* paraiso-dark\n",
"* lovelace\n",
"* algol\n",
"* algol_nu"
]
},
{
"cell_type": "code",
"metadata": {
"id": "pwG4rmAk2zSA",
"outputId": "2d6243c9-d8a5-409e-d6df-4f3e952a044e",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 85
}
},
"source": [
"%%cpp -n src.cpp -s fruity\n",
"int foo() {\n",
" cout << \"Hello World!\" << endl;\n",
"}"
],
"execution_count": null,
"outputs": [
{
"output_type": "display_data",
"data": {
"text/html": [
"<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\"\n",
" \"http://www.w3.org/TR/html4/strict.dtd\">\n",
"\n",
"<html>\n",
"<head>\n",
" <title></title>\n",
" <meta http-equiv=\"content-type\" content=\"text/html; charset=None\">\n",
" <style type=\"text/css\">\n",
"td.linenos { background-color: #f0f0f0; padding-right: 10px; }\n",
"span.lineno { background-color: #f0f0f0; padding: 0 5px 0 5px; }\n",
"pre { line-height: 125%; }\n",
"body .hll { background-color: #333333 }\n",
"body .c { color: #008800; font-style: italic; background-color: #0f140f } /* Comment */\n",
"body .err { color: #ffffff } /* Error */\n",
"body .esc { color: #ffffff } /* Escape */\n",
"body .g { color: #ffffff } /* Generic */\n",
"body .k { color: #fb660a; font-weight: bold } /* Keyword */\n",
"body .l { color: #ffffff } /* Literal */\n",
"body .n { color: #ffffff } /* Name */\n",
"body .o { color: #ffffff } /* Operator */\n",
"body .x { color: #ffffff } /* Other */\n",
"body .p { color: #ffffff } /* Punctuation */\n",
"body .ch { color: #008800; font-style: italic; background-color: #0f140f } /* Comment.Hashbang */\n",
"body .cm { color: #008800; font-style: italic; background-color: #0f140f } /* Comment.Multiline */\n",
"body .cp { color: #ff0007; font-weight: bold; font-style: italic; background-color: #0f140f } /* Comment.Preproc */\n",
"body .cpf { color: #008800; font-style: italic; background-color: #0f140f } /* Comment.PreprocFile */\n",
"body .c1 { color: #008800; font-style: italic; background-color: #0f140f } /* Comment.Single */\n",
"body .cs { color: #008800; font-style: italic; background-color: #0f140f } /* Comment.Special */\n",
"body .gd { color: #ffffff } /* Generic.Deleted */\n",
"body .ge { color: #ffffff } /* Generic.Emph */\n",
"body .gr { color: #ffffff } /* Generic.Error */\n",
"body .gh { color: #ffffff; font-weight: bold } /* Generic.Heading */\n",
"body .gi { color: #ffffff } /* Generic.Inserted */\n",
"body .go { color: #444444; background-color: #222222 } /* Generic.Output */\n",
"body .gp { color: #ffffff } /* Generic.Prompt */\n",
"body .gs { color: #ffffff } /* Generic.Strong */\n",
"body .gu { color: #ffffff; font-weight: bold } /* Generic.Subheading */\n",
"body .gt { color: #ffffff } /* Generic.Traceback */\n",
"body .kc { color: #fb660a; font-weight: bold } /* Keyword.Constant */\n",
"body .kd { color: #fb660a; font-weight: bold } /* Keyword.Declaration */\n",
"body .kn { color: #fb660a; font-weight: bold } /* Keyword.Namespace */\n",
"body .kp { color: #fb660a } /* Keyword.Pseudo */\n",
"body .kr { color: #fb660a; font-weight: bold } /* Keyword.Reserved */\n",
"body .kt { color: #cdcaa9; font-weight: bold } /* Keyword.Type */\n",
"body .ld { color: #ffffff } /* Literal.Date */\n",
"body .m { color: #0086f7; font-weight: bold } /* Literal.Number */\n",
"body .s { color: #0086d2 } /* Literal.String */\n",
"body .na { color: #ff0086; font-weight: bold } /* Name.Attribute */\n",
"body .nb { color: #ffffff } /* Name.Builtin */\n",
"body .nc { color: #ffffff } /* Name.Class */\n",
"body .no { color: #0086d2 } /* Name.Constant */\n",
"body .nd { color: #ffffff } /* Name.Decorator */\n",
"body .ni { color: #ffffff } /* Name.Entity */\n",
"body .ne { color: #ffffff } /* Name.Exception */\n",
"body .nf { color: #ff0086; font-weight: bold } /* Name.Function */\n",
"body .nl { color: #ffffff } /* Name.Label */\n",
"body .nn { color: #ffffff } /* Name.Namespace */\n",
"body .nx { color: #ffffff } /* Name.Other */\n",
"body .py { color: #ffffff } /* Name.Property */\n",
"body .nt { color: #fb660a; font-weight: bold } /* Name.Tag */\n",
"body .nv { color: #fb660a } /* Name.Variable */\n",
"body .ow { color: #ffffff } /* Operator.Word */\n",
"body .w { color: #888888 } /* Text.Whitespace */\n",
"body .mb { color: #0086f7; font-weight: bold } /* Literal.Number.Bin */\n",
"body .mf { color: #0086f7; font-weight: bold } /* Literal.Number.Float */\n",
"body .mh { color: #0086f7; font-weight: bold } /* Literal.Number.Hex */\n",
"body .mi { color: #0086f7; font-weight: bold } /* Literal.Number.Integer */\n",
"body .mo { color: #0086f7; font-weight: bold } /* Literal.Number.Oct */\n",
"body .sb { color: #0086d2 } /* Literal.String.Backtick */\n",
"body .sc { color: #0086d2 } /* Literal.String.Char */\n",
"body .sd { color: #0086d2 } /* Literal.String.Doc */\n",
"body .s2 { color: #0086d2 } /* Literal.String.Double */\n",
"body .se { color: #0086d2 } /* Literal.String.Escape */\n",
"body .sh { color: #0086d2 } /* Literal.String.Heredoc */\n",
"body .si { color: #0086d2 } /* Literal.String.Interpol */\n",
"body .sx { color: #0086d2 } /* Literal.String.Other */\n",
"body .sr { color: #0086d2 } /* Literal.String.Regex */\n",
"body .s1 { color: #0086d2 } /* Literal.String.Single */\n",
"body .ss { color: #0086d2 } /* Literal.String.Symbol */\n",
"body .bp { color: #ffffff } /* Name.Builtin.Pseudo */\n",
"body .vc { color: #fb660a } /* Name.Variable.Class */\n",
"body .vg { color: #fb660a } /* Name.Variable.Global */\n",
"body .vi { color: #fb660a } /* Name.Variable.Instance */\n",
"body .il { color: #0086f7; font-weight: bold } /* Literal.Number.Integer.Long */\n",
"\n",
" </style>\n",
"</head>\n",
"<body>\n",
"<h2></h2>\n",
"\n",
"<div class=\"highlight\"><pre><span></span><span class=\"kt\">int</span> <span class=\"nf\">foo</span><span class=\"p\">()</span> <span class=\"p\">{</span>\n",
" <span class=\"n\">cout</span> <span class=\"o\">&lt;&lt;</span> <span class=\"s\">&quot;Hello World!&quot;</span> <span class=\"o\">&lt;&lt;</span> <span class=\"n\">endl</span><span class=\"p\">;</span>\n",
"<span class=\"p\">}</span>\n",
"</pre></div>\n",
"</body>\n",
"</html>\n"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {
"tags": []
}
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "llPk4IfzC7lO"
},
"source": [
"### Append to an existing file\n",
"Use option `-a` to append to the file mentoined via `-n` argument."
]
},
{
"cell_type": "code",
"metadata": {
"id": "Ln-iP7f7zsVF",
"cellView": "both",
"outputId": "8ec82caa-2a14-441c-88d2-446dac3a4c5e",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 85
}
},
"source": [
"#@title Refer output of this cell. Double-click to hide source.\n",
"%%cpp -n test.cpp -a\n",
"\n",
"int foo() {\n",
" cout << \"Hello World!\" << endl;\n",
"}"
],
"execution_count": null,
"outputs": [
{
"output_type": "display_data",
"data": {
"text/html": [
"<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\"\n",
" \"http://www.w3.org/TR/html4/strict.dtd\">\n",
"\n",
"<html>\n",
"<head>\n",
" <title></title>\n",
" <meta http-equiv=\"content-type\" content=\"text/html; charset=None\">\n",
" <style type=\"text/css\">\n",
"td.linenos { background-color: #f0f0f0; padding-right: 10px; }\n",
"span.lineno { background-color: #f0f0f0; padding: 0 5px 0 5px; }\n",
"pre { line-height: 125%; }\n",
"body .hll { background-color: #4f424c }\n",
"body .c { color: #776e71 } /* Comment */\n",
"body .err { color: #ef6155 } /* Error */\n",
"body .k { color: #815ba4 } /* Keyword */\n",
"body .l { color: #f99b15 } /* Literal */\n",
"body .n { color: #e7e9db } /* Name */\n",
"body .o { color: #5bc4bf } /* Operator */\n",
"body .p { color: #e7e9db } /* Punctuation */\n",
"body .ch { color: #776e71 } /* Comment.Hashbang */\n",
"body .cm { color: #776e71 } /* Comment.Multiline */\n",
"body .cp { color: #776e71 } /* Comment.Preproc */\n",
"body .cpf { color: #776e71 } /* Comment.PreprocFile */\n",
"body .c1 { color: #776e71 } /* Comment.Single */\n",
"body .cs { color: #776e71 } /* Comment.Special */\n",
"body .gd { color: #ef6155 } /* Generic.Deleted */\n",
"body .ge { font-style: italic } /* Generic.Emph */\n",
"body .gh { color: #e7e9db; font-weight: bold } /* Generic.Heading */\n",
"body .gi { color: #48b685 } /* Generic.Inserted */\n",
"body .gp { color: #776e71; font-weight: bold } /* Generic.Prompt */\n",
"body .gs { font-weight: bold } /* Generic.Strong */\n",
"body .gu { color: #5bc4bf; font-weight: bold } /* Generic.Subheading */\n",
"body .kc { color: #815ba4 } /* Keyword.Constant */\n",
"body .kd { color: #815ba4 } /* Keyword.Declaration */\n",
"body .kn { color: #5bc4bf } /* Keyword.Namespace */\n",
"body .kp { color: #815ba4 } /* Keyword.Pseudo */\n",
"body .kr { color: #815ba4 } /* Keyword.Reserved */\n",
"body .kt { color: #fec418 } /* Keyword.Type */\n",
"body .ld { color: #48b685 } /* Literal.Date */\n",
"body .m { color: #f99b15 } /* Literal.Number */\n",
"body .s { color: #48b685 } /* Literal.String */\n",
"body .na { color: #06b6ef } /* Name.Attribute */\n",
"body .nb { color: #e7e9db } /* Name.Builtin */\n",
"body .nc { color: #fec418 } /* Name.Class */\n",
"body .no { color: #ef6155 } /* Name.Constant */\n",
"body .nd { color: #5bc4bf } /* Name.Decorator */\n",
"body .ni { color: #e7e9db } /* Name.Entity */\n",
"body .ne { color: #ef6155 } /* Name.Exception */\n",
"body .nf { color: #06b6ef } /* Name.Function */\n",
"body .nl { color: #e7e9db } /* Name.Label */\n",
"body .nn { color: #fec418 } /* Name.Namespace */\n",
"body .nx { color: #06b6ef } /* Name.Other */\n",
"body .py { color: #e7e9db } /* Name.Property */\n",
"body .nt { color: #5bc4bf } /* Name.Tag */\n",
"body .nv { color: #ef6155 } /* Name.Variable */\n",
"body .ow { color: #5bc4bf } /* Operator.Word */\n",
"body .w { color: #e7e9db } /* Text.Whitespace */\n",
"body .mb { color: #f99b15 } /* Literal.Number.Bin */\n",
"body .mf { color: #f99b15 } /* Literal.Number.Float */\n",
"body .mh { color: #f99b15 } /* Literal.Number.Hex */\n",
"body .mi { color: #f99b15 } /* Literal.Number.Integer */\n",
"body .mo { color: #f99b15 } /* Literal.Number.Oct */\n",
"body .sb { color: #48b685 } /* Literal.String.Backtick */\n",
"body .sc { color: #e7e9db } /* Literal.String.Char */\n",
"body .sd { color: #776e71 } /* Literal.String.Doc */\n",
"body .s2 { color: #48b685 } /* Literal.String.Double */\n",
"body .se { color: #f99b15 } /* Literal.String.Escape */\n",
"body .sh { color: #48b685 } /* Literal.String.Heredoc */\n",
"body .si { color: #f99b15 } /* Literal.String.Interpol */\n",
"body .sx { color: #48b685 } /* Literal.String.Other */\n",
"body .sr { color: #48b685 } /* Literal.String.Regex */\n",
"body .s1 { color: #48b685 } /* Literal.String.Single */\n",
"body .ss { color: #48b685 } /* Literal.String.Symbol */\n",
"body .bp { color: #e7e9db } /* Name.Builtin.Pseudo */\n",
"body .vc { color: #ef6155 } /* Name.Variable.Class */\n",
"body .vg { color: #ef6155 } /* Name.Variable.Global */\n",
"body .vi { color: #ef6155 } /* Name.Variable.Instance */\n",
"body .il { color: #f99b15 } /* Literal.Number.Integer.Long */\n",
"\n",
" </style>\n",
"</head>\n",
"<body>\n",
"<h2></h2>\n",
"\n",
"<div class=\"highlight\"><pre><span></span><span class=\"kt\">int</span> <span class=\"nf\">foo</span><span class=\"p\">()</span> <span class=\"p\">{</span>\n",
" <span class=\"n\">cout</span> <span class=\"o\">&lt;&lt;</span> <span class=\"s\">&quot;Hello World!&quot;</span> <span class=\"o\">&lt;&lt;</span> <span class=\"n\">endl</span><span class=\"p\">;</span>\n",
"<span class=\"p\">}</span>\n",
"</pre></div>\n",
"</body>\n",
"</html>\n"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {
"tags": []
}
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "YuqGHTyeDmsG"
},
"source": [
"### Compile\n",
"You can also compile the source file when writing or appending. Use `-c true` to compile using `g++ -o <filename> <filename.ext>`, where, `<filename>` is file name without extension as specified by `-n` and `<fiename.ext>` is file name with extension as specifed by `-n` option.\n",
"\n",
"You can give custom build commands via `-c '<compile command>'`. Please note that the compilr command must be enclosed in `'`."
]
},
{
"cell_type": "code",
"metadata": {
"id": "7u8c9VMhDxdg",
"outputId": "18fac437-105f-430b-bc94-0db3a7b17cc2",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 103
}
},
"source": [
"#@title Refer output of this cell. Double-click to hide source.\n",
"%%cpp -n test.cpp -c true -a\n",
"\n",
"int bar() {\n",
" cout << \"This is C++ Plugin.\" << endl;\n",
"}"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"\n"
],
"name": "stdout"
},
{
"output_type": "display_data",
"data": {
"text/html": [
"<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\"\n",
" \"http://www.w3.org/TR/html4/strict.dtd\">\n",
"\n",
"<html>\n",
"<head>\n",
" <title></title>\n",
" <meta http-equiv=\"content-type\" content=\"text/html; charset=None\">\n",
" <style type=\"text/css\">\n",
"td.linenos { background-color: #f0f0f0; padding-right: 10px; }\n",
"span.lineno { background-color: #f0f0f0; padding: 0 5px 0 5px; }\n",
"pre { line-height: 125%; }\n",
"body .hll { background-color: #4f424c }\n",
"body .c { color: #776e71 } /* Comment */\n",
"body .err { color: #ef6155 } /* Error */\n",
"body .k { color: #815ba4 } /* Keyword */\n",
"body .l { color: #f99b15 } /* Literal */\n",
"body .n { color: #e7e9db } /* Name */\n",
"body .o { color: #5bc4bf } /* Operator */\n",
"body .p { color: #e7e9db } /* Punctuation */\n",
"body .ch { color: #776e71 } /* Comment.Hashbang */\n",
"body .cm { color: #776e71 } /* Comment.Multiline */\n",
"body .cp { color: #776e71 } /* Comment.Preproc */\n",
"body .cpf { color: #776e71 } /* Comment.PreprocFile */\n",
"body .c1 { color: #776e71 } /* Comment.Single */\n",
"body .cs { color: #776e71 } /* Comment.Special */\n",
"body .gd { color: #ef6155 } /* Generic.Deleted */\n",
"body .ge { font-style: italic } /* Generic.Emph */\n",
"body .gh { color: #e7e9db; font-weight: bold } /* Generic.Heading */\n",
"body .gi { color: #48b685 } /* Generic.Inserted */\n",
"body .gp { color: #776e71; font-weight: bold } /* Generic.Prompt */\n",
"body .gs { font-weight: bold } /* Generic.Strong */\n",
"body .gu { color: #5bc4bf; font-weight: bold } /* Generic.Subheading */\n",
"body .kc { color: #815ba4 } /* Keyword.Constant */\n",
"body .kd { color: #815ba4 } /* Keyword.Declaration */\n",
"body .kn { color: #5bc4bf } /* Keyword.Namespace */\n",
"body .kp { color: #815ba4 } /* Keyword.Pseudo */\n",
"body .kr { color: #815ba4 } /* Keyword.Reserved */\n",
"body .kt { color: #fec418 } /* Keyword.Type */\n",
"body .ld { color: #48b685 } /* Literal.Date */\n",
"body .m { color: #f99b15 } /* Literal.Number */\n",
"body .s { color: #48b685 } /* Literal.String */\n",
"body .na { color: #06b6ef } /* Name.Attribute */\n",
"body .nb { color: #e7e9db } /* Name.Builtin */\n",
"body .nc { color: #fec418 } /* Name.Class */\n",
"body .no { color: #ef6155 } /* Name.Constant */\n",
"body .nd { color: #5bc4bf } /* Name.Decorator */\n",
"body .ni { color: #e7e9db } /* Name.Entity */\n",
"body .ne { color: #ef6155 } /* Name.Exception */\n",
"body .nf { color: #06b6ef } /* Name.Function */\n",
"body .nl { color: #e7e9db } /* Name.Label */\n",
"body .nn { color: #fec418 } /* Name.Namespace */\n",
"body .nx { color: #06b6ef } /* Name.Other */\n",
"body .py { color: #e7e9db } /* Name.Property */\n",
"body .nt { color: #5bc4bf } /* Name.Tag */\n",
"body .nv { color: #ef6155 } /* Name.Variable */\n",
"body .ow { color: #5bc4bf } /* Operator.Word */\n",
"body .w { color: #e7e9db } /* Text.Whitespace */\n",
"body .mb { color: #f99b15 } /* Literal.Number.Bin */\n",
"body .mf { color: #f99b15 } /* Literal.Number.Float */\n",
"body .mh { color: #f99b15 } /* Literal.Number.Hex */\n",
"body .mi { color: #f99b15 } /* Literal.Number.Integer */\n",
"body .mo { color: #f99b15 } /* Literal.Number.Oct */\n",
"body .sb { color: #48b685 } /* Literal.String.Backtick */\n",
"body .sc { color: #e7e9db } /* Literal.String.Char */\n",
"body .sd { color: #776e71 } /* Literal.String.Doc */\n",
"body .s2 { color: #48b685 } /* Literal.String.Double */\n",
"body .se { color: #f99b15 } /* Literal.String.Escape */\n",
"body .sh { color: #48b685 } /* Literal.String.Heredoc */\n",
"body .si { color: #f99b15 } /* Literal.String.Interpol */\n",
"body .sx { color: #48b685 } /* Literal.String.Other */\n",
"body .sr { color: #48b685 } /* Literal.String.Regex */\n",
"body .s1 { color: #48b685 } /* Literal.String.Single */\n",
"body .ss { color: #48b685 } /* Literal.String.Symbol */\n",
"body .bp { color: #e7e9db } /* Name.Builtin.Pseudo */\n",
"body .vc { color: #ef6155 } /* Name.Variable.Class */\n",
"body .vg { color: #ef6155 } /* Name.Variable.Global */\n",
"body .vi { color: #ef6155 } /* Name.Variable.Instance */\n",
"body .il { color: #f99b15 } /* Literal.Number.Integer.Long */\n",
"\n",
" </style>\n",
"</head>\n",
"<body>\n",
"<h2></h2>\n",
"\n",
"<div class=\"highlight\"><pre><span></span><span class=\"kt\">int</span> <span class=\"nf\">bar</span><span class=\"p\">()</span> <span class=\"p\">{</span>\n",
" <span class=\"n\">cout</span> <span class=\"o\">&lt;&lt;</span> <span class=\"s\">&quot;This is C++ Plugin.&quot;</span> <span class=\"o\">&lt;&lt;</span> <span class=\"n\">endl</span><span class=\"p\">;</span>\n",
"<span class=\"p\">}</span>\n",
"</pre></div>\n",
"</body>\n",
"</html>\n"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {
"tags": []
}
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "wHbDJQVADOVm"
},
"source": [
"### Execute"
]
},
{
"cell_type": "code",
"metadata": {
"id": "4QM92lhcsITU",
"outputId": "8afd9350-b3b3-4352-e03b-8840aaf10700",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 52
}
},
"source": [
"!./test"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"Hello World!\n",
"This is C++ Plugin.\n"
],
"name": "stdout"
}
]
}
]
}
import subprocess
import os
from IPython.core.magic import Magics, cell_magic, magics_class
from IPython.core.magic_arguments import argument, magic_arguments, parse_argstring
from pygments import highlight
from pygments.lexers import CppLexer
from pygments.formatters import HtmlFormatter
from IPython.display import display, HTML
def print_out(out: str):
for l in out.split('\n'):
print(l)
def displayHTML(html_code):
'''
Display HTML in notebook
'''
display(HTML(html_code))
@magics_class
class CPP(Magics):
@staticmethod
def compile(src, out):
compiler = 'g++'
res = subprocess.check_output(
[compiler, "-o", out, src], stderr=subprocess.STDOUT)
print_out(res.decode("utf8"))
@staticmethod
def custom_compile(arg_list):
res = subprocess.check_output(
arg_list, stderr=subprocess.STDOUT)
print_out(res.decode("utf8"))
@magic_arguments()
@argument('-n', '--name', type=str, help='File name that will be produced by the cell.')
@argument('-c', '--compile', type=str, help='Compile command. Use true for default command or specify command in single quotes.')
@argument('-a', '--append', help='Should be appended to same file', action="store_true")
@argument('-s', '--style', type=str, help='Pygments style name')
@cell_magic
def cpp(self, line='', cell=None):
'''
C++ syntax highlighting cell magic.
'''
global style
args = parse_argstring(self.cpp, line)
if args.name != None:
ex = args.name.split('.')[-1]
if ex not in ['c', 'cpp', 'h', 'hpp']:
raise Exception('Name must end with .cpp, .c, .hpp, or .h')
else:
args.name = 'src.cpp'
if args.append:
mode = "a"
else:
mode = "w"
with open(args.name, mode) as f:
f.write(cell)
if args.compile != None:
try:
if args.compile == 'true':
self.compile(args.name, args.name.split('.')[0])
else:
self.custom_compile(args.compile.replace("'", "").split(' '))
except subprocess.CalledProcessError as e:
print_out(e.output.decode("utf8"))
if args.style == None:
displayHTML(highlight(cell, CppLexer(), HtmlFormatter(full=True,nobackground=True,style='paraiso-dark')))
else:
displayHTML(highlight(cell, CppLexer(), HtmlFormatter(full=True,nobackground=True,style=args.style)))
def load_ipython_extension(ip):
os.system('pip install pygments ipywidgets')
plugin = CPP(ip)
ip.register_magics(plugin)
@rawstar134
Copy link

If we want to run c++ or c, or java program on google colab , we can quickly run on it. Would you please follow this article that gives the best idea to run the program on google colab.
https://debuggingsolution.blogspot.com/2021/10/run-c-program-in-google-colab.html

@cjwomack
Copy link

Thanks for sharing your gist. I was able to adapt it for Colab for Kotlin as the Kotlin kernel by Jetbrains doesn't work and syntax highlighting doesn't work either for Colab out of the box. So I now have syntax highlighting for Kotlin!! Otherwise to suppress weird syntax error highlighting had to use workaround use %%writefile or %%script magics

I figure it's good to have a record of code that is runnable. I find it interesting that Google's Kotlin tutorials uses Kotlin Playground which doesn't show the compiling under the hood which previous commenter wasn't happy re this gist for c++, c, java...

If you are doing a lot of code this plugin is useful to save time.

@akshaykhadse
Copy link
Author

akshaykhadse commented Feb 18, 2023

Thanks for sharing your gist. I was able to adapt it for Colab for Kotlin as the Kotlin kernel by Jetbrains doesn't work and syntax highlighting doesn't work either for Colab out of the box. So I now have syntax highlighting for Kotlin!! Otherwise to suppress weird syntax error highlighting had to use workaround use %%writefile or %%script magics

I figure it's good to have a record of code that is runnable. I find it interesting that Google's Kotlin tutorials uses Kotlin Playground which doesn't show the compiling under the hood which previous commenter wasn't happy re this gist for c++, c, java...

If you are doing a lot of code this plugin is useful to save time.

I am glad that this helped you.

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