bottle
bottle.py is a fast and simple micro-framework for python web-applications.
Bottle: Python Web Framework — Bottle 0.13-dev documentation bottle is a fast, simple and lightweight wsgi micro web-framework for python.
I've done some coding with Bottle. It's really simple and fits my needs. However, I got stick when I tried to wrap the application into a class :
import bottle
app = bottle
class App():
def __init__(self,param):
self.param = param
# Doesn't work
@app.route("/1")
def index1(self):
return("I'm 1 | self.param = %s" % self.param)
# Doesn't work
@app.route("/2")
def index2(self):
return("I'm 2")
# Works fine
@app.route("/3")
def index3():
return("I'm 3")
Is it possible to use methods instead of functions in Bottle?
Source: (StackOverflow)
How do I go about returning json data from a bottle request handler. I see a dict2json method in the bottle src but I am not sure how to use it.
What is in the documentation:
@route('/spam')
def spam():
return {'status':'online', 'servertime':time.time()}
Gives me this when I bring up the page:
<html>
<head></head>
<body>statusservertime</body>
</html>
Source: (StackOverflow)
I've been looking into microframeworks for Python, and have come across two interesting options, Flask and Bottle. each have some similar features. One thing I noticed is that all the example sites show all the application code located inside a single Python file. Obviously, for even moderately sized sites, this would become difficult to manage quite quickly. Do either (or both) of these frameworks support being broken up among different files, and if so how would that be accomplished?
I'm familiar with Django, and like how its a little more structured, but i would rather use something more lightweight, but still powerful.
Source: (StackOverflow)
What are the large distinctions between these two microframeworks? It seems Bottle is more flexible in terms of the templating engine and other configurations, but flask supports many useful plugins like flask-openID.
How are they fundamentally different, and why have they not merged?
Source: (StackOverflow)
I'm really enjoying Bottle so far, but the fact that I have to CTRL+C out of the server and restart it every time I make a code change is a big hit on my productivity. I've thought about using Watchdog to keep track of files changing then restarting the server, but how can I do that when the bottle.run
function is blocking.
Running the server from an external script that watches for file changes seems like a lot of work to set up. I'd think this was a universal issue for Bottle, CherryPy and etcetera developers.
Thanks for your solutions to the issue!
Source: (StackOverflow)
I'm writing an API using Bottle, which so far has been fantastic. However, I've run up against a small hurdle when trying to return a JSON array. Here's my test app code:
from bottle import route, run
@route('/single')
def returnsingle():
return { "id": 1, "name": "Test Item 1" }
@route('/containsarray')
def returncontainsarray():
return { "items": [{ "id": 1, "name": "Test Item 1" }, { "id": 2, "name": "Test Item 2" }] }
@route('/array')
def returnarray():
return [{ "id": 1, "name": "Test Item 1" }, { "id": 2, "name": "Test Item 2" }]
run(host='localhost', port=8080, debug=True, reloader=True)
When I run this and request each route, I get the JSON responses I'd expect from the first two routes:
/single
{ id: 1, name: "Test Item 1" }
/containsarray
{ "items": [ { "id": 1, "name": "Test Item 1" }, { "id": 2, "name": "Test Item 2" } ] }
So, I had expected returning a list of dictionaries to create the following JSON response:
[ { "id": 1, "name": "Test Object 1" }, { "id": 2, "name": "Test Object 2" } ]
But requesting the /array
route just results in an error. What am I doing wrong, and how can I return a JSON array in this manner?
Source: (StackOverflow)
What I need is a lightweight authentication/ACL library or middleware which is preferably capable of openID (though this is not crucial), and would play nice with bottle framework (i.e, maybe not use exceptions as an internal flow-control mechanism). Any suggestions?
EDIT:
Any thoughts on barrel?
Source: (StackOverflow)
Bottle can use several webservers:
Build-in HTTP development server and support for paste, fapws3, flup, cherrypy or any other WSGI capable server.
I am using Bottle for a desktop-app and I guess that the development server is enough in this case. I would like to know if some of you have experience with one of the alternative server.
Which server for which purpose?
Source: (StackOverflow)
import urllib
fun open():
return urllib.urlopen('http://example.com')
But when example.com opens it does not render css or js. How can I open the webpage in a web browser?
@error(404)
def error404(error):
return webbrowser.open('http://example.com')
I am using bottle. Giving me the error: TypeError("'bool' object is not iterable",)
Source: (StackOverflow)
I have a Nginx vhost than is configured as such:
...
location /one {
include uwsgi_params;
uwsgi_pass unix:///.../one.sock;
}
location /two {
include uwsgi_params;
uwsgi_pass unix:///.../two.sock
}
...
This is a simplified configuration of course
When I request /one/something
I would like my Python script to receive /something
as request_uri
.
I'm using BottlePy but would like this to be handled by Nginx and not in my Python code.
Can I do something like uwsgi_param REQUEST_URI replace($request_uri, '^/one', '')
?
Edit
Here is the request from my Python code:
[pid: 30052|app: 0|req: 1/1] () {42 vars in 844 bytes} [Tue Aug 21 14:22:07 2012] GET /one/something => generated 0 bytes in 4 msecs (HTTP/1.1 200) 2 headers in 85 bytes (0 switches on core 0)
So Python is OK but uWSGI is not.
How to fix that?
Source: (StackOverflow)
I am trying to return a html template with BottlePy. And this works fine. But if I insert a javascript file like this in my tpl-file:
<script type="text/javascript" src="js/main.js" charset="utf-8"></script>
I get an 404 error.
(Failed to load resource: the server responded with a status of 404 (Not Found))
Does anyone know how to fix this problem?
Here is my script file:
from bottle import route, run, view
@route('/')
@view('index')
def index():
return dict()
run(host='localhost', port=8080)
And that is the template file, located in "./views" subfolder.
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="utf-8" />
<script type="text/javascript" src="js/main.js" charset="utf-8"></script>
</head>
<body>
</body>
</html>
Maybe it is the "rootPath/js/main.js" from the development server where it looks for my js-file?
The structure of the files is:
app.py
-js
main.js
-views
index.tpl
Thanks.
Source: (StackOverflow)
I've read the Bottle Documentation but I can't find the example of how to use Bottle with multiple files. Below is the way I did and it's working but I'm not sure whether this is the proper way to go (I saw merge()
and mount()
in API but not sure if they are related to this). Please give me the comments.
all.py (This is the main file for running)
#! /usr/bin/python
from bottle import route, run
import hello1
import hello2 # if I have 10 files, it will be 10 imports
run(host='localhost', port=8080, debug=True)
hello1.py
#! /usr/bin/python
from bottle import route, run
@route('/hello1')
def hello1():
return "Hello world no.1"
hello2.py
#! /usr/bin/python
from bottle import route, run
@route('/hello2')
def hello2():
return "Hello world no.2"
Source: (StackOverflow)
The quickstart tutorial for the Flask-SQLAlchemy plugin instructs users to create table models inheriting the db.Model
class, e.g.
app = Flask(__main__)
db = SQLAlchemy(app)
class Users(db.Model):
__tablename__ = 'users'
...
However, the SQLAlchemy tutorial and the bottle-SQLAlchemy README both suggest that table models inherit a Base
instantiated from declarative_base()
.
Base = declarative_base()
class Users(Base):
__tablename__ = 'users'
...
What is the difference between these two approaches?
Source: (StackOverflow)
I have an application with Python Bottle and I want to add Cache-Control in static files. I am new on this so forgive me if I have done something wrong.
Here is the function and how I serve static files:
@bottle.get('/static/js/<filename:re:.*\.js>')
def javascripts(filename):
return bottle.static_file(filename, root='./static/js/')
To add Cache-Control I have included one more line (I saw it in a tutorial)
@bottle.get('/static/js/<filename:re:.*\.js>')
def javascripts(filename):
bottle.response.headers['Cache-Control'] = 'public, max-age=604800'
return bottle.static_file(filename, root='./static/js/')
But when I check the headers from Developer tools on Chrome: I have either Cache-Control:max-age=0
or Cache-Control:no-cache
Source: (StackOverflow)