Start developing web UI using bottle microframework.
Implement simple server.
SQLObject>=2.2.1; python_version >= '2.7' and python_version < '3.0'
SQLObject>=3.0.0; python_version >= '3.4'
m_lib.defenc>=1.0
+bottle
При использовании опции `-v` также выводится id из БД.
+
+ml-web.py
+------------
+
+Использование::
+
+ ml-web.py [-p port]
+
+Опции::
+
+ -p, --port port Порт протокола HTTP
+
+Запускает web-сервер. Если указан порт, то используется указанный порт.
+Иначе выбирается случайный порт из числа свободных. Программа запускает
+браузер (или открывает новое окно уже запущенного web-обозревателя) с
+адресом, указывающим на сервер.
+
.. vim: set tw=72 :
With one option `-v` it also prints database id.
+
+ml-web.py
+------------
+
+Usage::
+
+ ml-web.py [-p port]
+
+Options::
+
+ -p, --port port HTTP port to listen to
+
+Run a web server. If a port is given listen on the given port. Else
+choose a random port. Start a browser (or open a new window of a running
+browser) pointing it to the server.
+
.. vim: set tw=72 :
--- /dev/null
+from bottle import route
+
+
+@route('/')
+def hello():
+ return "Hello World!"
--- /dev/null
+from bottle import run
+
+from wsgiref import simple_server
+from wsgiref.handlers import SimpleHandler
+from wsgiref.simple_server import WSGIServer
+from bottle import route
+
+simple_server.ServerHandler = SimpleHandler # Stop logging to stdout
+
+
+class QuitWSGIServer(WSGIServer):
+ _quit_flag = False
+
+ def serve_forever(self):
+ while not self._quit_flag:
+ self.handle_request()
+
+
+@route('/quit')
+def quit():
+ QuitWSGIServer._quit_flag = True
+ return "The program has finished. Have a nice day!"
+
+
+def run_server(host='localhost', port=0):
+ run(host=host, port=port, server_class=QuitWSGIServer, debug=True)
--- /dev/null
+
+def get_open_port():
+ # https://stackoverflow.com/a/2838309/7976758
+ import socket
+ s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+ s.bind(("", 0))
+ # s.listen(1)
+ port = s.getsockname()[1]
+ s.close()
+ return port
--- /dev/null
+#! /usr/bin/env python
+
+import argparse
+import time
+import webbrowser
+
+from bottle import thread # portable import
+
+import m_librarian.web.app # noqa: F401 imported but unused
+from m_librarian.web.server import run_server
+from m_librarian.web.utils import get_open_port
+
+
+def start_browser(port):
+ time.sleep(1) # A small timeout to allow the main thread to run the server
+ webbrowser.open_new('http://localhost:%d/' % port)
+
+
+if __name__ == '__main__':
+ parser = argparse.ArgumentParser(description='Init')
+ parser.add_argument('-p', '--port', help='HTTP server port')
+ args = parser.parse_args()
+
+ if args.port:
+ port = args.port
+ else:
+ port = get_open_port()
+
+ thread.start_new_thread(start_browser, (port,))
+ run_server(port=port)
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
],
- packages=['m_librarian'],
+ packages=['m_librarian', 'm_librarian.web'],
package_data={'m_librarian': [
'glst/*.txt', 'glst/genres_*.glst',
'translations/*.mo'
]
},
scripts=['scripts/ml-import.py', 'scripts/ml-initdb.py',
- 'scripts/ml-search.py'],
+ 'scripts/ml-search.py', 'scripts/ml-web.py'],
python_requires='>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*',
install_requires=[
'SQLObject>=2.2.1; python_version=="2.7"',
extras_require={
'm_lib': ['m_lib>=3.1'],
'pbar': ['m_lib>=3.1'],
+ 'web': ['bottle'],
+ 'bottle': ['bottle'],
},
)