I happened to make a simple web application using python, so I tried using it.
What you want
--Easy to install --Minimal functionality such as routing
I wish I had. Just looking for it, Bottle was a good thing, so instead of a memo.
For the time being, I will use python for the first time, so from the installation of python
I didn't even know which one to use, python2 or 3, so I decided to use pyenv, which has multiple environments.
install pyenv
cd ~
git clone git://github.com/yyuu/pyenv.git .pyenv
echo 'export PYENV_ROOT="${HOME}/.pyenv"' >> ~/.zshrc
echo 'eval "$(plenv init -)"' >> ~/.zshrc
exec $SHELL -l
For the time being, insert 2.7 series It's convenient because pip (like a gem?) Is also included.
pyenv install 2.7.5
Then I was told that there was no readline, so install with homebrew
brew install readline
brew link readline
It seems to be a common problem, and there are many other things written in here.
I entered it for the time being, so I will use Bottle
pip install bottle
Enter with.
The bottle is all in one file called bottle.py
. It seems that you will use it while importing what you need from there.
For the time being hello world (sample as it is)
hello.py
from bottle import route, run
@route('/hello/:name')
def hello(name):
return '<h1>Hello %s!</h1>' % name
run(host='localhost', port=8080)
Now on the command line
python hello.py
Now when you access [locahost: 8080 / hello / tak0303](locahost: 8080 / hello / tak0303),
It becomes Hello tak0303!
. Easy.
If you want to use template in html, create a folder called views
in the same directory and put it there.
show.tpl(It seems that the extension can be anything)
<h1>{{name}}</h1>
hello.py
from bottle import route, run, template
@route('/hello/<name>')
def hello(name='unknown'):
return template('show', name=name)
run(host='localhost', port=8080)
You can do it like this
Also, in the initial state, you have to restart the server when changing files, and it is difficult to debug, which is quite inconvenient.
python
run(host='localhost', port=8080, debug=True, reloader=True)
By doing this, an error will be displayed on the browser and the server will be restarted automatically when the file is changed.
Reference: http://bottlepy.org/docs/dev/index.html
Recommended Posts