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.


    How can I open 'html' file ?

    Pythonista
    2
    14
    6271
    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.
    • cvp
      cvp @shiro last edited by

      @shiro still one question, is your html file in iCloud Drive/Pythonista3 or in another iCloud folder?

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

        Here, showing a split view with, at left: console of a little script starting a Bottle server,
        at right: a Safari proving loading an html file stored in iCloud Drive/Pythonista3

        #coding: utf-8
        import ui
        import threading
        import requests
        import os
        
        from bottle import Bottle, ServerAdapter, route, static_file
        
        class MyWSGIRefServer(ServerAdapter):
        	server = None
        
        	def run(self, handler):
        		from wsgiref.simple_server import make_server, WSGIRequestHandler
        		if self.quiet:
        			class QuietHandler(WSGIRequestHandler):
        				def log_request(*args, **kw): pass
        			self.options['handler_class'] = QuietHandler
        		self.server = make_server(self.host, self.port, handler, **self.options)
        		self.server.serve_forever()
        
        	def stop(self):
        		self.server.shutdown()
            
        app = Bottle()
            
        @app.get('/<path:path>')
        def bottle_get(path):
        	from bottle import static_file
        	import os
        	# http://localhost:8080/MesTests/test.jpg
        	# http://localhost:8080/iCloud/MesTests/test.jpg
        	# path = folder/file
        	# path = iCloud/folder/file
        	t = 'iCloud/'
        	if path[:len(t)] == t:
        		full_path = ('/private/var/mobile/Library/Mobile Documents/iCloud~com~omz-software~Pythonista3/Documents/' + path[len(t):])
        	else:
        		full_path = os.path.expanduser('~/Documents/' + path)
        	i = full_path.rfind('/')
        	filename = full_path[i+1:]
        	root = full_path[:i]
        	#print(path,filename,root)
        	return static_file(filename, root=root)
        
        if __name__ == '__main__':
        
        	port = 8080
        	server = MyWSGIRefServer(host='localhost',port=port)
          
        	t = threading.Thread(group=None, target=app.run, name='BottleWebServer', args=(), kwargs={'server': server, 'quiet': False})
        	t.server = server	# used by threads_indicator_in_status_bar to stop thread
        	t.port   = port  	# used by Add home screen shortcut 
        	t.start()
        
        1 Reply Last reply Reply Quote 0
        • shiro
          shiro last edited by ccc

          Dear cvp

          Thank you for advice.

          Html file in iCloud Drive/Pythonista3.

          iCloudDrive/iCloud~com~omz-software~Pythonista3/777/777.ipynb

          I tryed your code like bellow.

          #coding: utf-8
          import ui
          import threading
          import requests
          import os
          
          from bottle import Bottle, ServerAdapter, route, static_file
          
          class MyWSGIRefServer(ServerAdapter):
              server = None
          
              def run(self, handler):
                  from wsgiref.simple_server import make_server, WSGIRequestHandler
                  if self.quiet:
                      class QuietHandler(WSGIRequestHandler):
                          def log_request(*args, **kw): pass
                      self.options['handler_class'] = QuietHandler
                  self.server = make_server(self.host, self.port, handler, **self.options)
                  self.server.serve_forever()
          
              def stop(self):
                  self.server.shutdown()
              
          app = Bottle()
              
          @app.get('/<path:path>')
          def bottle_get(path):
              from bottle import static_file
              import os
              # http://localhost:8080/MesTests/test.jpg
              # http://localhost:8080/iCloud/MesTests/test.jpg
              # path = folder/file
              # path = iCloud/folder/file
              path = iCloudDrive/777/777
              t = 'iCloud/'
              if path[:len(t)] == t:
                  full_path = ('/private/var/mobile/Library/Mobile Documents/iCloud~com~omz-software~Pythonista3/Documents/' + path[len(t):])
              else:
                  full_path = os.path.expanduser('~/Documents/' + path)
              i = full_path.rfind('/')
              filename = full_path[i+1:]
              root = full_path[:i]
              #print(path,filename,root)
              return static_file(filename, root=root)
          
          if __name__ == '__main__':
          
              port = 8080
              server = MyWSGIRefServer(host='localhost',port=port)
            
              t = threading.Thread(group=None, target=app.run, name='BottleWebServer', args=(), kwargs={'server': server, 'quiet': False})
              t.server = server   # used by threads_indicator_in_status_bar to stop thread
              t.port   = port     # used by Add home screen shortcut 
              t.start()
          

          sever runnung, and I open html file using sagari like this.

          http://localhost:8080/iCloudDrive/777.html

          but still do not open file.

          Safari monitor is wrriten error meesage Japanese .

          meaning is,

          "Can not open page. Safari can not conect sever"

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

            @shiro does Safari run on the same device as the script?

            and in Safari:

            http://localhost:8080/iCloud/777.html
            

            and file must be in root of iCloud of Pythonista3

            Or, if 777.html in folder 777 of iCloud of Pythonista3

            http://localhost:8080/iCloud/777/777.html
            
            1 Reply Last reply Reply Quote 0
            • shiro
              shiro last edited by ccc

              I don't know how to appended picture of Safari monitar hardcopy (png file).

              On the top of monitor of Safari,"localhost" letter apperes.
              and middle monitor of Safari,"Can not open page. Safari can not conect sever"

              Still, I failed.

              ・iphone

              http://localhost:8080/iCloud/777/777.html

              ・PC jyupyter notebook

              #coding: utf-8
              import ui
              import threading
              import requests
              import os
              
              from bottle import Bottle, ServerAdapter, route, static_file
              
              class MyWSGIRefServer(ServerAdapter):
                  server = None
              
                  def run(self, handler):
                      from wsgiref.simple_server import make_server, WSGIRequestHandler
                      if self.quiet:
                          class QuietHandler(WSGIRequestHandler):
                              def log_request(*args, **kw): pass
                          self.options['handler_class'] = QuietHandler
                      self.server = make_server(self.host, self.port, handler, **self.options)
                      self.server.serve_forever()
              
                  def stop(self):
                      self.server.shutdown()
                  
              app = Bottle()
                  
              @app.get('/<path:path>')
              def bottle_get(path):
                  from bottle import static_file
                  import os
                  # http://localhost:8080/MesTests/test.jpg
                  # http://localhost:8080/iCloud/MesTests/test.jpg
                  # path = folder/file
                  # path = iCloud/folder/file
              
                  t = 'iCloud/'
                  if path[:len(t)] == t:
                      full_path = ('/private/var/mobile/Library/Mobile Documents/iCloud~com~omz-software~Pythonista3/Documents/' + path[len(t):])
                  else:
                      full_path = os.path.expanduser('~/Documents/' + path)
                  i = full_path.rfind('/')
                  filename = full_path[i+1:]
                  root = full_path[:i]
                  #print(path,filename,root)
              
                #print(path,filename,root)
              
                  return static_file(filename, root=root)
              
              if __name__ == '__main__':
              
                  port = 8080
                  server = MyWSGIRefServer(host='localhost',port=port)
                
                  t = threading.Thread(group=None, target=app.run, name='BottleWebServer', args=(), kwargs={'server': server, 'quiet': False})
                  t.server = server   # used by threads_indicator_in_status_bar to stop thread
                  t.port   = port     # used by Add home screen shortcut 
                  t.start()
              

              After run jyupyter notebook python,

              Bottle v0.12.16 server starting up (using MyWSGIRefServer())...
              Listening on http://localhost:8080/
              Hit Ctrl-C to quit.

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

                @shiro Ok, I did not understand that you are running your server on a PC, I thought it was on iPad/iPhone, thus forget my script
                Thus my http://..... does not work with your script.
                But first, instead of local host, your have to put the IP of your PC
                Local host will only work if you go on Safari or other browser on your PC.

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

                  This post is deleted!
                  1 Reply Last reply Reply Quote 0
                  • shiro
                    shiro last edited by

                    Dear cvp

                    I did iphone and PC conect icloud.

                    so,I try another way.
                    Only iphone did.

                    and change localhost from 8080 to 5000 because 8080 alredy used message.

                    Finally, safari sever reply like bellow.

                    Bottle v0.12.5 server starting up (using MyWSGIRefServer())...
                    Listening on http://localhost:5000/
                    Hit Ctrl-C to quit.

                    127.0.0.1 - - [28/Mar/2019 07:07:07] "GET / HTTP/1.1" 404 720
                    iCloud/777/777.html 777.html /private/var/mobile/Library/Mobile Documents/iCloud~com~omz-software~Pythonista3/Documents/777

                    In safari,

                    http://localhost:5000/iCloud/777/777.html

                    http://127.0.0.1:5000/iCloud/777/777.html

                    but,"File does not exist"

                    error continue,but almost I think.

                    Does wrong procedure in safari http comand?

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

                      Dear cvp

                      I am sorry. I missed to import file name.

                      It's not "777.html" but "777.htm".

                      Finally, I succesed to run html from Safari.
                      I realy appreciated your devoting kindness!

                      I can do like bellow.

                      ①In Pythonista3 (iCloud/777/777.py), run "777.py"

                      ②In 777.py "console" monitor, tap "http://localhost:5000" → need to get URL

                      ③In Safari,input URL "http://localhost:5000/iCloud/777/777.htm

                      ④Appeare "777.htm" on the monitor and run.

                      It's really usefull code.

                      Thank you so much!

                      I appreciated your work.

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

                        @shiro As my script launches BottleServer as a thread, it may stay running, that's why you have needed to change the port from 8080 to 5000.
                        If you want to stop all these servers in Pythonista, run this little script

                        # stop BottleWebServer threads
                        import threading
                        threads = threading.enumerate()
                        for thread in threads:
                        	if thread.name == 'BottleWebServer':
                        		thread.server.stop()
                        
                        1 Reply Last reply Reply Quote 0
                        • shiro
                          shiro last edited by

                          Dear cvp

                          Creating a web frame using bottle was the first experience. Thank you for your kindness.

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