Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Arguments to web.py handler classes

Tags:

web.py

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)

like image 979
talonx Avatar asked May 21 '12 10:05

talonx


People also ask

How do I get Started with a web Py application?

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:

What is the use of arguments in Python?

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.

How to execute a Python program from command line arguments?

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:

What is a keyword argument in Python?

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


1 Answers

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()
like image 200
pbuck Avatar answered Oct 06 '22 07:10

pbuck