How can I get Bottle to restart on file change?
Asked Answered
H

4

36

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!

Haigh answered 12/6, 2012 at 20:18 Comment(0)
W
54

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)
Wells answered 12/6, 2012 at 20:24 Comment(5)
Oh, cool! Are there any situations in which this will fail to reload a module? I assume it watches all imported modulesHaigh
Unfortunately I don't have enough experience to be able to say one way or the other.Wells
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.Stiffen
I wish I could tell it to restart any time I change my configuration files thoughHaigh
To defnulls point, you can enable debug by: from bottle import debug ... debug(True)Chaunceychaunt
S
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.

Straightout answered 15/6, 2012 at 15:3 Comment(0)
N
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)
Noni answered 22/5, 2016 at 6:41 Comment(0)
C
-1

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)
Contemptible answered 21/8, 2017 at 22:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.