Skip to content

Instantly share code, notes, and snippets.

@zoomlogo
Last active December 14, 2021 11:37
Show Gist options
  • Save zoomlogo/6325e5600c75ceb987dae394f77df357 to your computer and use it in GitHub Desktop.
Save zoomlogo/6325e5600c75ceb987dae394f77df357 to your computer and use it in GitHub Desktop.
A lightweight Python repl with syntax highlighting amongst other things.
#!python
import code
import sys
import os
from rich import inspect, print
from prompt_toolkit import PromptSession
from prompt_toolkit.lexers import PygmentsLexer
from prompt_toolkit.styles import style_from_pygments_cls
from pygments.lexers.python import Python3Lexer
from pygments.styles import get_style_by_name
sys.ps2 = sys.ps1 = " " * 6
def cls():
os.system('tput clear')
def pip(args):
os.system('pip ' + args)
def ls():
os.system('ls --color -a')
def cd(d):
os.chdir(d)
def I(*args, **kwargs):
inspect(*args, **kwargs, all=True)
class BetterConsole(code.InteractiveConsole):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.session = PromptSession()
def raw_input(self, prompt):
r = self.session.prompt(prompt,
lexer=PygmentsLexer(Python3Lexer),
style=style_from_pygments_cls(
get_style_by_name("gruvbox-dark")))
if r == 'q':
exit(0)
return r
BetterConsole(locals={
'cls': cls,
'pip': pip,
'ls': ls,
'cd': cd,
'I': I,
'print': print,
'random': __import__("random"),
'timeit': __import__("timeit").timeit,
'math': __import__("math")}).interact("")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment