Example notebook can be found here.
-
-
Save akshaykhadse/7acc91dd41f52944c6150754e5530c4b to your computer and use it in GitHub Desktop.
| 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) |
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.
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.
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