Export code and output as pdf file
Hello guys! how are you doing?
Ireally need to know how can I export in PDF the code and the output of a program in Pythonista. I’m having this programming class where the professor asked for us to send only the .pdf files containing the solution of the exercises + the code that we wrote. It’s as simple as it sounds and still I’m so mad I didn’t figure it out, sorry..
I searched in quite a lot of topics but couldn’t find anywhere, only that maybe has something to do with Py2PDF on Pythonista.
@ccc Thanks. Shame on me, I knew it.... 😢
Edit: also modified in my github
Still to find in the tool the way to get the console output to add it to the pdf...
@cvp, easier to redirect all output to a file?
@mikael Sure but needs to change the code?
This code should return the console content
from objc_util import * def GetConsoleText(): print('called') win = ObjCClass('UIApplication').sharedApplication().keyWindow() main_view = win.rootViewController().view() ret = '' def analyze(v): ret = None for sv in v.subviews(): if 'textview' in str(sv._get_objc_classname()).lower(): if 'OMTextEditorView' not in str(sv.superview()._get_objc_classname()): # not TextView of script return sv.text() ret = analyze(sv) if ret: return ret ret = analyze(main_view) return ret
Thus full code can be found here
@cvp, no need to change the code:
import contextlib, runpy with open('ex1_result.txt', 'w') as f: with contextlib.redirect_stdout(f): runpy.run_module('ex1')
@mikael I didn't know this functionality.
But, that will say that the whole console is redirected, even if your script asks questions...
Anyway, this is a shorter and general solution.
@cvp, agree, case by case either option may be more applicable. I wanted to remind us of both of these capabilities (running a py file and redirecting output), as both might come handy in various contexts.
@mikael "My" solution is not very elegant and I'm not sure it is robust and would stay operational in next versions of Pythonista.
I searched this way for the fun because I I thought that if the console is displayed and is scrollable, it has to be in a TextView...
And during my tests, I discovered the "console 🔽 " button and its pop up menu where you can copy all the console...In the past, I often selected the whole text and copied without knowing this possibility 😢