31

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!

Hubro
  • 48,322
  • 60
  • 196
  • 344

4 Answers4

49

Check out from the tutorial a section entitled "Auto Reloading"

During development, you have to restart the server a lot to test your recent changes. The auto reloader can do this for you. Every time you edit a module file, the reloader restarts the server process and loads the newest version of your code.

This gives the following example:

from bottle import run
run(reloader=True)
Mark Hildreth
  • 36,998
  • 9
  • 113
  • 105
  • Oh, cool! Are there any situations in which this will fail to reload a module? I assume it watches all imported modules – Hubro Jun 12 '12 at 20:30
  • Unfortunately I don't have enough experience to be able to say one way or the other. – Mark Hildreth Jun 12 '12 at 20:31
  • 5
    It watches all imported modules and then completely restarts the server process. There is currently no way to add additional files to the watch-list (e.g. templates) but templates are not cached in debug mode anyway. – defnull Jun 13 '12 at 12:40
  • I wish I could tell it to restart any time I change my configuration files though – Hubro Jun 15 '12 at 13:43
  • To defnulls point, you can enable debug by: `from bottle import debug` ... `debug(True)` – Jarrett Jun 26 '12 at 04:13
4

With

run(reloader=True)

there are situations where it does not reload like when the import is inside a def. To force a reload I used

subprocess.call(['touch', 'mainpgm.py'])

and it reloads fine in linux.

f p
  • 2,937
  • 1
  • 24
  • 33
3

Use reloader=True in run(). Keep in mind that in windows this must be under if __name__ == "__main__": due to the way the multiprocessing module works.

from bottle import run

if __name__ == "__main__":
    run(reloader=True)
Udi
  • 24,758
  • 8
  • 84
  • 116
0

These scripts should do what you are looking for.

AUTOLOAD.PY

import os
def cherche(dir):
    FichList = [ f for f in os.listdir(dir) if os.path.isfile(os.path.join(dir,f)) ]
    return FichList

def read_file(file):
    f = open(file,"r")
    R=f.read()
    f.close()
    return R

def load_html(dir="pages"):
    FL = cherche(dir)
    R={}
    for f in FL:
        if f.split('.')[1]=="html":
            BUFF = read_file(dir+"/"+f)
            R[f.split('.')[0]] = BUFF
    return R 

MAIN.PY

# -*- coding: utf-8 -*-

#Version 1.0 00:37


import sys
reload(sys)
sys.setdefaultencoding("utf-8")
import datetime
import ast
from bottle import route, run, template, get, post, request, response, static_file, redirect

#AUTOLOAD by LAGVIDILO
import autoload
pages = autoload.load_html()




BUFF = ""
for key,i in pages.iteritems():
    BUFF=BUFF+"@get('/"+key+"')\n"
    BUFF=BUFF+"def "+key+"():\n"
    BUFF=BUFF+" return "+pages[key]+"\n"

print "=====\n",BUFF,"\n====="
exec(BUFF)


run(host='localhost', port=8000, reloader=True)
Jason Roman
  • 7,503
  • 10
  • 32
  • 35