How can I get Bottle to restart on file change?

后端 未结 4 1470
长情又很酷
长情又很酷 2020-12-29 20:29

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\'v

相关标签:
4条回答
  • 2020-12-29 20:32

    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.

    0 讨论(0)
  • 2020-12-29 20: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)
    
    0 讨论(0)
  • 2020-12-29 20:56

    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)
    
    0 讨论(0)
  • 2020-12-29 20:57

    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)
    
    0 讨论(0)
提交回复
热议问题