Is there any way to pass arguments to web.py handler class constructors?
E.g. These arguments might come from the command line (when the main web.py script is run), after the first arg (which is taken as the port number)
To get started with your web.py application, open up a new text file (let’s call it code.py) and type: This imports the web.py module. Now we need to tell web.py our URL structure. Let’s start out with something simple:
Python exposes a mechanism to capture and extract your Python command line arguments. These values can be used to modify the behavior of a program. For example, if your program processes data read from a file, then you can pass the name of the file to your program, rather than hard-coding the value in your source code.
In this first example, the Python interpreter takes option -c for command, which says to execute the Python command line arguments following the option -c as a Python program. Another example shows how to invoke Python with -h to display the help:
1 A keyword argument is where you provide a name to the variable as you pass it into the function. 2 One can think of the kwargs as being a dictionary that maps each keyword to the value that we pass alongside it. That is... More ...
Sure, depending on exactly what you mean. It's all python after all.
Consider you have available web.config
which is a Storage
object which is visible everywhere. I regularly use that to place system-wide configuration information (& alter debugging output as you'll see in the example).
Being a Storage object, you can add your own configuration items there & then access them in your url handlers.
if __name__ == '__main__':
app = web.application(urls, globals())
web.config.debug_sql = '--debug_sql' in sys.argv
app.run()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With