Export code and output as pdf file
mikael last edited by mikael
@mikael I had also thought to an online conversion html to pdf but it needs Internet and you have to share/select Shortcuts/select shortcut instead of tools/select tool
thus one step more 😂
all I deduce is that we have a great application with Pythonista
Edit: could you share the shortcut, I'm interested, thanks
Edit2: and your solution provides lines numbers, I'm giving up, you're the best
JonB last edited by
Note the professor wants both code AND program output.
(output, i can understand... i have no idea why anyone would ever want to see python code in pdf form)
I imagine also that he might expect some kind of header/format?
@JonB Agree, but passing via html helps for other infos like headers..., isn'it.
@mikael Thanks a lot. I didn't even know this feature of posting to an API in Shortcuts app.
I like it.
I always try to process all in Pythonista but I wait the day when @omz would add the functionality to share/open a file to a specific app without passing via a menu, because Shortcuts can do it...
ccc last edited by ccc
path = editor.get_path() file_name = os.path.basename(path) i = file_name.rfind('.') if i>= 0: file_name = file_name[:i] + '.pdf' # Can be rewritten as... file_name = os.path.basename(os.path.splitext(editor.get_path())) + '.pdf'
@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 😢