Notebooks >> Scripts
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

56 lines
1.2 KiB

{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "1933fb9b-3ebd-42d1-a1cd-094127ace37e",
"metadata": {},
"outputs": [],
"source": [
"\"\"\"\n",
"This example demonstrates how to write a custom highlighter.\n",
"\"\"\"\n",
"\n",
"from random import randint\n",
"\n",
"from rich import print\n",
"from rich.highlighter import Highlighter\n",
"\n",
"\n",
"class RainbowHighlighter(Highlighter):\n",
" def highlight(self, text):\n",
" for index in range(len(text)):\n",
" text.stylize(f\"color({randint(16, 255)})\", index, index + 1)\n",
"\n",
"\n",
"rainbow = RainbowHighlighter()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.9.13 64-bit",
"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.9.13"
},
"vscode": {
"interpreter": {
"hash": "b0fa6594d8f4cbf19f97940f81e996739fb7646882a419484c72d19e05852a7e"
}
}
},
"nbformat": 4,
"nbformat_minor": 5
}