I've provided an example below that will reveal one console line at a time. This is probably the best that can be done, at least without a lot of work. Getting this to work required patching beamer in the preamble.
Automatically inserting a \pause
after each console command, before its output, would be complicated. You would have to create a wrapper for Python's code
module (which is what PythonTeX uses internally) that would highlight each command and output separately, inserting \pause
at appropriate points. And then you would have to connect this custom module into PythonTeX.
However, if simply displaying one line of a console session at a time is sufficient (which is what your example did) the following will work. This relies on redefining \FancyVerbFormatLine
from fancyvrb
, so that a \pause
is inserted at the end of each line of console content.
Just doing that alone gives you the result you want, but you would have to run PythonTeX and then recompile the document for each line of console content, which is obviously unacceptable. This is due to the way that beamer deals with verbatim content; it keeps re-reading verbatim content for each overlay, and each re-read makes PythonTeX think that there is more code to execute. Fixing this requires resetting the PythonTeX counter that governs console code before each re-read. Doing that requires hacking the beamer internals. I've patched the beamer internals in the example below, so that PythonTeX only needs to be run a single time.
\documentclass{beamer}
\usepackage[french]{babel}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{pythontex}
\makeatletter
\AtBeginEnvironment{pyconsole}{%
\renewcommand{\FancyVerbFormatLine}[1]{#1\pause}}
% Need to go ahead and create a counter for each console session
% Proceed assuming only the default
\newcounter{pytx@pycon@default@default}
% Need temp counter for resetting console counter, since the counter
% is incremented each time beamer re-reads verbatim content
\newcounter{pythontex@temp}
\BeforeBeginEnvironment{frame}{%
\setcounter{pythontex@temp}{\value{pytx@pycon@default@default}}}
% Need a custom version of beamer's verbatim input routine
% The original is from beamerbaseframe.sty
\def\beamer@@@frameinput<#1>[#2][#3]{%
\global\advance\c@framenumber by-1\relax%
\edef\beamer@temp{\noexpand\frame<#1>[#2][{#3,fragile=false}]%
{\begingroup
% Reset counter
\setcounter{pytx@pycon@default@default}{\value{pythontex@temp}}%
\noexpand\input{\beamer@verbfilename}\endgroup}}%
\beamer@temp%
\ifx\beamer@frameenvironmentsubst\beamer@frametext%
\else%
\expandafter\endgroup%
\fi%
\beamer@reseteecodes}
\makeatother
\begin{document}
\begin{frame}[fragile]
\frametitle{Exercises}
What is the result in Python 2 of:
\begin{pyconsole}
7 / 2
7./2.
\end{pyconsole}
What will be the last output?
\begin{pyconsole}
L = [5, 3, 42, 1]
L[1:4]
\end{pyconsole}
\end{frame}
\end{document}