{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# IPython 渲染" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import IPython\n", "\n", "def code2html(code):\n", " \"\"\"Helper function to use pygments to turn the code string into highlighted html.\"\"\"\n", " import pygments\n", " from pygments.lexers import Python3Lexer\n", " from pygments.formatters import HtmlFormatter\n", " formatter = HtmlFormatter()\n", " html = pygments.highlight(code, Python3Lexer(), formatter)\n", " return \"%s\\n\" % (formatter.get_style_defs(\".highlight\"), html)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "code = \"\"\"\n", "a = 1\n", "b = 2\n", "c = a * b\n", "d << 3\n", "\"\"\"" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
a = 1\n",
"b = 2\n",
"c = a * b\n",
"d << 3\n",
"