Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to physically print python code in color from IDLE?

I have searched for an answer to this but the related solutions seem to concern 'print'ing in the interpreter.

I am wondering if it is possible to print (physically on paper) python code in color from IDLE?

I have gone to: File > Print Window in IDLE and it seems to just print out a black and white version without prompting whether to print in color etc.

Edit:

It seems like this might not be available so the option is to copy code to a text editor like SciTE and print from there - quite like the default IDLE syntax highlighting though.

like image 668
user1063287 Avatar asked May 30 '13 10:05

user1063287


People also ask

How do you print in color in Python IDLE?

In IDLE, select Options > Configure IDLE , then click on the Highlights tab. Click the drop down Normal text and select a type of text to change. In this example we will choose "Python Definitions" which is known in idlecolors as the colour user1() .

How do you color text in Python?

To add color and style to text, you should create a class called ANSI, and inside this class, declare the configurations about the text and color with code ANSI. Functions Used: background: allows background formatting. Accepts ANSI codes between 40 and 47, 100 and 107.

How do you print code in Python?

Print FunctionThe Python print() function takes in any number of parameters, and prints them out on one line of text. The items are each converted to text form, separated by spaces, and there is a single '\n' at the end (the "newline" char).


2 Answers

A good and simple solution is to use IDLE2HTML, an extension for IDLE.
The tool allows IDLE to save code from the editor window (or interactive shell) to a HTML file,
with colour included in the CSS format. After this, the file can be printed (as asked for) or its formatted code used with other programs such as word processors.
A reliable website currently to download the Python files and view some instructions is the Python Package Index (PyPI) - the page is https://pypi.python.org/pypi/IDLE2HTML or the pip module, built-in to newer Python versions, can install the files for you.

An useful advantage of this method is that it gets the colour formatting from IDLE instead of taking time analysing the code and using stored colours (which many other solutions do). This means it uses the actual colour scheme and works with different IDLE themes such as "IDLE Dark". This should result in less resources and shorter code being used.
Also, the current extension (version 2.0) could probably be adapted to use a different file format when saving the data because it has quite simple code.

Some details about how it works:

Since the tool is Python code running as an extension, it has access to a special variable provided by IDLE, which contains data about the editor window. The extend.txt file of the idlelib Python extension (that runs the editor) describes this variable called editwin:

An IDLE extension class is instantiated with a single argument, `editwin', an EditorWindow instance.

It also includes some other files about extending IDLE such as "config-main.def" and "config-extensions.def". The official Python documentation has more information.

Installation instructions:

Copy IDLE2HTML.py to the "idlelib" folder of your python installation.
Append the contents of "config-extensions.txt" to your "config-extensions.def" file (also in the idlelib folder) and restart IDLE.

How to use:

Select "Save as HTML" from the "Options" menu and specify the filename in the FileDialog window. The HTML file is created immediately.

A code tweak to work in Python 3.x:
Currently, the IDLE2HTML extension (version 2.0) won't work with Python 3.x until a small change is made to the code which I found in a fixed version included with the IDLEX Python module. Here is a file comparison, with the original on the left and the fixed IDLEX version on the right of the screenshot image.

File comparison image described above.

Description: if Python version is Python 3.x, use import tkinter as Tkinter instead of import Tkinter and import tkinter.filedialog as tkFileDialog instead of import tkFileDialog. The code shown uses sys.version from the built-in "sys" module in an if/else block to run the right code.

like image 27
Edward Avatar answered Sep 20 '22 17:09

Edward


IDLE can't do it, but you can do it in an indirect way: Use LaTeX to format your script with the IDLE colors.

The listings package supports IDLE-like colorizing for python, but it's pretty well hidden: You must know to include a "preference file" that is mentioned in the listings manual, but without a description of its features. The following almost-minimal example will turn your script myscript.py into color PDF in the IDLE colors.

\documentclass{article}

\usepackage{listings}
\input{listings-python.prf}  % defines the python-idle-code style
\usepackage{textcomp} % Needed for straight quotes                               

\lstset{
 basicstyle=\normalsize\ttfamily, % size of the fonts for the code               
 language={Python},style={python-idle-code},
 showstringspaces=false,
 tabsize=4,
 upquote=true,   % Requires textcomp                                             
}

\oddsidemargin=-0.5cm
\textwidth=7in  % This just fits 80 characters at 10pt                           

\begin{document}
\lstinputlisting{myscript.py}
\end{document}

You could also include code snippets in a real LaTeX document.

Availability: You'll need at least listings version 1.5b (2013/08/26). Users of Texlive 2013 must download it from CTAN (e.g. with the command line texlive manager, tlmgr). It is part of TexLive 2016.

Unfortunately there still seems to be a problem with the CTAN version: The preferences file listings-python.prf is in the documentation folder for the package, not in tex/latex/listings where TeX can find it. You'll need to move it manually for the inclusion to work.

like image 199
alexis Avatar answered Sep 20 '22 17:09

alexis