Python web application
I want to create a very simple python web app. I don't need Django or any other web framwwork like that. Isn't there a simpler way 开发者_如何学Goto create a web app in python?
Thanks
If you don't need Django, try web.py
http://webpy.org/
import web
urls = (
'/(.*)', 'hello'
)
app = web.application(urls, globals())
class hello:
def GET(self, name):
if not name:
name = 'world'
return 'Hello, ' + name + '!'
if __name__ == "__main__":
app.run()
Sure! For example,
print 'Content-Type: text/plain'
print ''
print 'Hello, world!'
this is a web app -- if you save it into a file in an appropriate directory of a machine running a web server and set the server's configuration properly (depending on the server); the article I pointed to specifically shows how to deploy this web app to Google App Engine, but just about any web server can serve CGI apps, and this is a simple example thereof.
Of course, CGI has its limits, and you can use more sophisticated approaches (still short of a framework!) such as WSGI (also universally supported, if nothing else because it can run on top of CGI -- but in most cases you can also deploy it in more advanced ways) and possibly some of the many excellent utility components you can deploy with WSGI to save you work in coding certain parts of your apps.
WSGI is probably what you are looking for. Though there are several lightweight python web frameworks around which are less monolithic than django.
The truth is that you do need a framework of some sort even if it's extremely minimal. You can use WSGI as a base and at least you're doing a little better. Python is a very powerful, very unspecific programming language so if you decide to do it without a framework you're going to have to rewrite huge amounts of code that you may be taking for granted.
If you do decide to go with something other than Django try this list and maybe you'll find something simple enough that you'll feel good about it. :)
first we have to install the package.
In terminal:
>>> pip install PySimpleGUI
Then go on type in this code...
# Code
import PySimpleGUI as sg
sg.theme('add_your_bgcoloor_here') layout = [[sg.Text('This is for the text')],
[sg.InputText('This is to input the texts')],
[sg.InputCombo(['Option 1', 'Option 2', 'Option 3'])
[sg.Radio('Checkbox', 1)],
[sg.Spin([x for x in range(1, 100)], initial_value=1)],
[sg.Button('go']]
# creating our window window = sg.Window('Window, layout)
# defining the events and the value in the layout using while True statement while True:
event, values = window.read()
# defining our button 'go' (sg.Button('go'))
if event == 'go':
break window.close()
Yep WSGI...
def hello_wsgi(environ, start_response):
start_response('200 OK', [('content-type', 'text/html')])
return ['Hello world!']
If you want to abstract this in terms of request/response to get a little further away from http try webob.
from webob import Request, Response
def hello_wsgi(environ, start_response):
request = Request(environ)
#do something with the request
#return a response
return Response("Hello World!")(environ, start_response)
I use bottle all the time as a minimal web framework. It is very simple to use.
as a minimum example - taken from the web site :
from bottle import route, run
@route('/hello/:name')
def index(name='World'):
return '<b>Hello %s!</b>' % name
run(host='localhost', port=8080)
you simply associate url (route) to functions. This one even get an optional argument. It has an optional light templating language, and you can tweak it a lot for our needs. Very powerful.
It is also very easy to instal - as it comes as a single file, standing along your app, and is pure compliant python. It is also very easy to debug, with a nice autoreload on modif while in development mode.
As a final advantages, it runs smoothly under pypy - thus providing a speed boost over other frameworks.
Now a days it is better to use PhusionPassenger Standalone or with NGINX using same technique as PHP by proxy passing it to FastCGI in case of PHP and WSGI for Python.
The URL and all explanation for Passenger can be found: Here
All information about Python running on NGINX over FastCGI, uWSGI or Passenger
About frameworks that wrap Python for easier Web development I do recommend Django if it is a larger application and once you get the hang of it Django isn't that hard actually.
Good Luck!
You can try Appier (https://github.com/hivesolutions/appier). Here's a sample app:
import appier
class HelloApp(appier.App):
@appier.route("/", "GET")
def hello(self):
return "Hello World"
HelloApp().serve()
And here's how you run it:
pip install appier
python hello.py
Disclaimer: This framework is part of the open-source portfolio of my company. We built the framework to make the code for our consulting work as simple and clean as possible (to improve our efficiency). The project is very active, as we use the framework all the time, however, the caveat is that we've only started talking about it publicly recently, so there's no community around it yet. However, for that very reason, we're very open to cooperating closely with early bird developers in improving our documentation and adding new features.
精彩评论