omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    Export code and output as pdf file

    Pythonista
    pdfkit pdf
    5
    23
    12575
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • JonB
      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?

      cvp 1 Reply Last reply Reply Quote 0
      • cvp
        cvp @JonB last edited by

        @JonB Agree, but passing via html helps for other infos like headers..., isn'it.

        1 Reply Last reply Reply Quote 0
        • mikael
          mikael @cvp last edited by

          @cvp, I agree that an all-Pythonista solution would be nicer and provide more opportunities to handle several files with a single click. Anyway, here’s the shortcut.

          cvp 1 Reply Last reply Reply Quote 0
          • cvp
            cvp @mikael last edited by

            @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...

            1 Reply Last reply Reply Quote 0
            • ccc
              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())[0]) + '.pdf'
              
              cvp 1 Reply Last reply Reply Quote 0
              • cvp
                cvp @ccc last edited by cvp

                @ccc Thanks. Shame on me, I knew it.... 😢

                Edit: also modified in my github

                1 Reply Last reply Reply Quote 0
                • cvp
                  cvp last edited by

                  Still to find in the tool the way to get the console output to add it to the pdf...

                  mikael 1 Reply Last reply Reply Quote 0
                  • mikael
                    mikael @cvp last edited by

                    @cvp, easier to redirect all output to a file?

                    cvp 1 Reply Last reply Reply Quote 0
                    • cvp
                      cvp @mikael last edited by

                      @mikael Sure but needs to change the code?

                      mikael 1 Reply Last reply Reply Quote 0
                      • cvp
                        cvp last edited by cvp

                        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

                        1 Reply Last reply Reply Quote 0
                        • mikael
                          mikael @cvp last edited by

                          @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')
                          
                          cvp 1 Reply Last reply Reply Quote 0
                          • cvp
                            cvp @mikael last edited by

                            @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.

                            mikael 1 Reply Last reply Reply Quote 0
                            • mikael
                              mikael @cvp last edited by

                              @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.

                              cvp 1 Reply Last reply Reply Quote 0
                              • cvp
                                cvp @mikael last edited by

                                @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 😢

                                1 Reply Last reply Reply Quote 1
                                • First post
                                  Last post
                                Powered by NodeBB Forums | Contributors