Python Syntax Highlighting

The AnsiConsolePythonExtensions class provides extension methods for IAnsiConsole to render Python code with syntax highlighting.

Methods

WritePythonAsync (Stream, default styles)

Writes Python code from a stream to the console with default syntax highlighting asynchronously.

Task<string> WritePythonAsync(this IAnsiConsole ansiConsole, Stream stream)

Parameters:

Returns: A task representing the asynchronous operation with the parsed string.

WritePythonAsync (Stream, custom styles)

Writes Python code from a stream to the console with custom syntax highlighting asynchronously.

Task<string> WritePythonAsync(this IAnsiConsole ansiConsole, Stream stream, PythonStyles pythonStyles)

Parameters:

Returns: A task representing the asynchronous operation with the parsed string.

WritePython (Stream, default styles)

Writes Python code from a stream to the console with default syntax highlighting synchronously.

string WritePython(this IAnsiConsole ansiConsole, Stream stream)

Parameters:

Returns: The parsed string.

WritePython (Stream, custom styles)

Writes Python code from a stream to the console with custom syntax highlighting synchronously.

string WritePython(this IAnsiConsole ansiConsole, Stream stream, PythonStyles pythonStyles)

Parameters:

Returns: The parsed string.

WritePython (string, default styles)

Writes Python code from a string to the console with default syntax highlighting.

void WritePython(this IAnsiConsole ansiConsole, string value)

Parameters:

WritePython (string, custom styles)

Writes Python code from a string to the console with custom syntax highlighting.

void WritePython(this IAnsiConsole ansiConsole, string value, PythonStyles pythonStyles)

Parameters:

Example Usage

Basic Usage with String

using Spectre.Console;
using NTokenizers.Extensions.Spectre.Console;

var pythonCode = @"
def main():
    print(""Hello, World!"")

if __name__ == ""__main__"":
    main()
";

AnsiConsole.Console.WritePython(pythonCode);

Custom Styles

using Spectre.Console;
using NTokenizers.Extensions.Spectre.Console;
using NTokenizers.Extensions.Spectre.Console.Styles;

var customPythonStyles = PythonStyles.Default;
customPythonStyles.Keyword = new Style(Color.Orange1);

AnsiConsole.Console.WritePython(pythonCode, customPythonStyles);

Async with Stream

using Spectre.Console;
using NTokenizers.Extensions.Spectre.Console;

await AnsiConsole.Console.WritePythonAsync(stream);