pclose! " force preview window closed
" copy the buffer into a new window, then run that buffer through python
sil %y a | below new | sil put a | sil %!python -
" indicate the output window as the current previewwindow
setlocal previewwindow ro nomodifiable nomodified
" back into the original window
command! RunPyBuffer call DoRunPyBuffer2()
map <Leader>p :RunPyBuffer<CR>
<Backslash>+p is mapped to run the current buffer through a Python interpreter. The output appears in a new window below the current one.
The code to do this with another interpreted language, such as bash, is almost identical and left as an exercise for the reader.