{ "info": { "author": "Nir Izraeli", "author_email": "nirizr@gmail.com", "bugtrack_url": null, "classifiers": [], "description": "[![Version Status][v-image]][pypi-url] \r\n\r\n# README #\r\n\r\nThis web.py based simple module allows you to automatically set up a simple HTTP web server out of advanced `argparse.ArgumentParser` objects and similar (`argh.ArgumentParser`) ones.\r\nUsing this on top of argh lets you automatically generate web user interface out of simple functions defined in your application.\r\nThis package was made for getting your personal command line scripts to the next stage - internal shared utilities.\r\n\r\n### How do I set up? ###\r\n\r\nFor a production like setup you'll need:\r\n\r\n1. make your main script expose an application global object by calling webui.Webui.wsgi() method\r\n\r\n2. modify `index.wsgi` to fit your application (trivial configuration, import aforementioned application)\r\n\r\n3. set up a wsgi supporting apache (or any other) server \r\n\r\nFor debugging like setup you'll need (but since it's used for internal tools, this might also be fine):\r\n\r\n1. replace methods like `argparse.ArgumentParser.parse_args()` or `argh.dispatch()` with `webui.Webui.getone()` or `webui.Webui.dispatch()` respectively.\r\n\r\n`dispatch()` will instantiate a web service and call dispatch methods (either provided by the user - you - or dispatch methods of supporting argument parsers like `argh`)\r\n\r\n`get()` and `getone()` wrap the `dispatch()` method and yield results as they are submitted in the web form, providing an interface that resembles the `parse_args()` method.\r\n\r\n### Dependencies ###\r\n\r\n`argparseweb` requires `web.py` to be available. You can install it (check for the latest version) with: `pip install web.py`\r\n\r\n### Basic examples ###\r\nThis example will set up an http server, get one valid input, tear the http server down, print a welcoming message to stdout and exit:\r\n```python\r\nimport argparse\r\nfrom argparseweb import *\r\n\r\ndef main():\r\n parser = argparse.ArgumentParser()\r\n\r\n parser.add_argument(\"name\", default=\"Anonymous\")\r\n\r\n # previously opts = parser.parse_args()\r\n opts = webui.Webui(parser).getone()\r\n print(\"Hello {name},\\nthis is a simple example.\".format(name=opts.name))\r\n\r\nif __name__ == \"__main__\":\r\n main()\r\n```\r\n\r\nThis example will also run until stopped, printing a welcoming message for every valid input:\r\n```python\r\nimport argparse\r\nfrom argparseweb import *\r\n\r\ndef main():\r\n parser = argparse.ArgumentParser()\r\n\r\n parser.add_argument(\"name\", default=\"Anonymous\")\r\n\r\n # previously opts = parser.parse_args()\r\n for opts in webui.Webui(parser).get():\r\n print(\"Hello {name},\\nthis is a simple example.\".format(name=opts.name))\r\n\r\nif __name__ == \"__main__\":\r\n main()\r\n```\r\n\r\nThis example will print the welcoming message in the http response, sending it back to the user:\r\n```python\r\nimport argparse\r\nfrom argparseweb import *\r\n\r\ndef welcome(opts):\r\n print(\"Hello {name},\\nthis is a simple example.\".format(name=opts.name))\r\n\r\ndef main():\r\n parser = argparse.ArgumentParser()\r\n\r\n parser.add_argument(\"name\", default=\"Anonymous\")\r\n\r\n # previously opts = parser.parse_args()\r\n webui.Webui(parser).dispatch(welcome, parsed=True)\r\n\r\nif __name__ == \"__main__\":\r\n main()\r\n```\r\n\r\n### A more complicated example ###\r\n\r\nThis snippet includes three modes of operation for the webui utility:\r\n\r\n1. first and simplest: dispatch methods using argh's automatic function to command line parser facilities, this is completely unrelated to webui and that way you won't lose existing command line usage ability.\r\n\r\n2. getting `--webui` as the first command line argument, sets up a development web server (defaults to *:8080) and is ready to use.\r\n\r\n3. exposing an `application` global object that supports the wsgi interface. once you point a browser with correct wsgi configuration (was a bit of a pain for me first time) it'll work like magic :)\r\n\r\nmyapp.py:\r\n```python\r\nimport argparse\r\nfrom argparseweb import *\r\n\r\ndef get_parser():\r\n \"\"\"Generate generic argument parser\"\"\"\r\n cmd_parser = argh.ArghParser()\r\n cmd_parser.add_commands([...])\r\n\r\n return cmd_parser\r\n\r\ndef main_1():\r\n # k. get the parser as usual\r\n cmd_parser = get_parser()\r\n\r\n # last chance to use webui, if --webui is passed as first command line argument\r\n # remove it as let webui handle the rest\r\n if sys.argv[1] == '--webui':\r\n sys.argv.remove('--webui')\r\n webui.Webui(cmd_parser).dispatch() # second mode of operation - development/fast setup\r\n else:\r\n # dispatch either webui or argh\r\n cmd_parser.dispatch() # first mode of operation - regular command line\r\n\r\ndef main_2():\r\n parser = argparse.ArgumentParser()\r\n\r\n # TODO: fill argparse\r\n\r\n # opts = parser.parse_args()\r\n opts = webui.Webui(parser).getone()\r\n\r\n # TODO: use opts as you would with any ArgumentParser generated namespace,\r\n # opts is really a namespace object directly created by parser, and webui only compiled an argument sequence\r\n # based on the filled form, passed into parser.parse_args() and back to you\r\n\r\ndef wsgi():\r\n global application\r\n\r\n # create a webui application using the command line argument parser object\r\n # and make it's wsgi function the global `application` parameter as required by wsgi\r\n cmd_parser = get_parser()\r\n application = webui.Webui(cmd_parser).wsgi() # third mode of operation - production wsgi application\r\n\r\nif __name__ == \"__main__\":\r\n # script initialized as main, lets do our trick\r\n main()\r\nelse:\r\n # if script wasn't initialized as main script, we're probably running\r\n # in wsgi mode\r\n wsgi()\r\n```\r\nindex.wsgi:\r\n```python\r\n# TODO: replace with your application path\r\n# i found now way to get it automatically in wsgi :/\r\nAPP_DIR = '/var/www/myapp'\r\n\r\nimport sys, os\r\nsys.path.insert(0, APP_DIR)\r\nos.chdir(APP_DIR)\r\n\r\nfrom myapp import application\r\n\r\n```\r\n\r\nMore examples are at `test.py`\r\n\r\n### known issues ###\r\n\r\n* right now vary-length arguments (nargs='?', nargs='*', nargs='+') are limited to one argument because i didn't write the HTML required for that. i'm considering multiple text inputs or textarea with line separation, input (and code) are most welcome.\r\n\r\nDone:\r\n\r\n* some code reordering is needed (split template to another file - it's grown quite big, handle action parameters better - shouldn't pass everything as html attributes although it's comfortable)\r\n* smoother integration into existing code.\r\n\r\n[v-image]: https://img.shields.io/pypi/v/argparseweb.svg\r\n[dm-image]: https://img.shields.io/pypi/dm/argparseweb.svg\r\n\r\n[pypi-url]: https://pypi.python.org/pypi/argparseweb/", "description_content_type": null, "docs_url": null, "download_url": "UNKNOWN", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/nirizr/argparseweb", "keywords": "webui,web,user,interface,ui,argparse,webpy,web.py", "license": "UNKNOWN", "maintainer": null, "maintainer_email": null, "name": "argparseweb", "package_url": "https://pypi.org/project/argparseweb/", "platform": "UNKNOWN", "project_url": "https://pypi.org/project/argparseweb/", "project_urls": { "Download": "UNKNOWN", "Homepage": "https://github.com/nirizr/argparseweb" }, "release_url": "https://pypi.org/project/argparseweb/0.1.3/", "requires_dist": null, "requires_python": null, "summary": "Automatic exposure of argparse-compatible scripts as a web interface", "version": "0.1.3" }, "last_serial": 2566836, "releases": { "0.1.2": [ { "comment_text": "", "digests": { "md5": "2fa93e096faf6546bf59d896a104ae05", "sha256": "06b5775e77797d8468a5c9a0e2c5b6aa2ed534f391818b705aae1ef148244b48" }, "downloads": -1, "filename": "argparseweb-0.1.2.tar.gz", "has_sig": false, "md5_digest": "2fa93e096faf6546bf59d896a104ae05", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 8812, "upload_time": "2016-07-24T21:02:52", "url": "https://files.pythonhosted.org/packages/ec/0a/cb8d85a0b3e9829f9c444f17cd4aea673c2b1e980e5a7f3e5e98eb5885aa/argparseweb-0.1.2.tar.gz" } ], "0.1.3": [] }, "urls": [] }