Is it possible to emulate a Sage session with SageTeX?

I'm editing an existing calculus book that uses Sage to demonstrate concepts. The existing book has Sage code listings that show what output the user will see if they duplicate the commands. For example, one of the blocks has the following:

sage: a,b = var("a,b")
sage: log(sqrt(a))
log(a)/2
sage: log(a/b).simplify_log()
log(a) - log(b)
sage: sin(a+b).simplify_trig()
cos(a)*sin(b) + sin(a)*cos(b)
sage: cos(a+b).simplify_trig()
cos(a)*cos(b) - sin(a)*sin(b)
sage: (a+b)^5
(b + a)^5
sage: expand((a+b)^5)
b^5 + 5*a*b^4 + 10*a^2*b^3 + 10*a^3*b^2 + 5*a^4*b + a^5


(Yes, some of the responses quoted above aren't correct, which is one of the things I'm correcting in the book.)

Is it possible to use SageTeX to automate the responses. I tried doing the following:

\begin{sageblock}
a,b = var("a,b")
log(sqrt(a))
\sage{log(sqrt(a))}
log(a/b).simplify_log()
\sage{log(a/b).simplify_log()}
sin(a+b).simplify_trig()
\sage{sin(a+b).simplify_trig()}
cos(a+b).simplify_trig()
\sage{cos(a+b).simplify_trig()}
(a+b)^5
\sage{(a+b)^5}
expand((a+b)^5)
\sage{expand((a+b)^5)}
\end{sageblock}


But when I ran Sage on the file it produced by SageTeX I got an error:

\$ sage master.sagetex.sage
File "master.sagetex.sage.py", line 11
* BackslashOperator() * sage{log(sqrt(a))}
^


I'm thinking my mistake might be that the \sage command can't appear within a sageblock.

Regardless, I think I'm getting across what I want to do. Is this possible, and if so, how can I go about it?

Are you looking for sagecommandline?

% !TEX TS-program = sage

\documentclass{article}
\usepackage{sagetex}
\begin{document}
\begin{sagecommandline}
sage: a,b = var("a,b")
sage: sin(a+b).simplify_trig()
sage: (a+b)^2
sage: expand((a+b)^2)
\end{sagecommandline}
\end{document}


This code will produce

sage: a,b = var("a,b")          1
sage: sin(a+b).simplify_trig()  2
cos(b)*sin(a) + cos(a)*sin(b)   3
sage: (a+b)^2                   4
(a + b)^2                       5
sage: expand((a+b)^2)           6
a^2 + 2*a*b + b^2               7