Forum Archive

How can I open 'html' file ?

shiro

Hello.

I try to open 'html' file use iPhone colaborate with iCloud Drive.
In iCloud Drive, sever run by python code.
But I can not open html file.

How can I open 'html' file ?
Could you let me know.

I am bigginer iphone and pythonista3.
so I can not open html file by Safari.

My environment is,

・pythonista3、stash、icloud Drive

・iOs12、iphoneXR, Safari

cvp

@shiro Sorry, some questions:
- what is the meaning of "In iCloud Drive, sever run by python code"?
- where is stored the html file?
- which url do you type in Safari to "Open the html file"?

shiro

Dear cvp

In iCloud Drive, I make '777' folder and put into two file '777.htm' , '777.ipynb' .

777---|---777.htm (java) three.min.js , preloadjs.min.js
|
|---777.ipynb

Then, Run '777.ipynb'

・777.ipynb

!pip install bottle

import sys, os, subprocess
from bottle import route, run, static_file, ServerAdapter

class SSLWebServer(ServerAdapter):
def run(self, handler):
from gevent.pywsgi import WSGIServer
srv = WSGIServer( (self.host, self.port), handler,
certfile='./server.pem', keyfile='./server.pem')
srv.serve_forever()

@route('/')
def static(filename):
return static_file(filename, root="/Users/shiro/source/py35_opencv3/")

run(host='0.0.0.0', port=8080, server=SSLWebServer)

After that, localhost sever running.

Next, I have to open '777.htm' file with iPhone using Safari.

But I do not know how to open it using Safari from iCloud Drive '777' folder.

cvp

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

cvp

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()
shiro

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

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

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

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

shiro

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/ukiyoemulation-master

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?

shiro

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?

shiro

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

@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()
shiro

Dear cvp

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