Last active
June 19, 2018 14:52
-
-
Save victor141516/a556dcd9f3d99e5e349ad9b982882dec to your computer and use it in GitHub Desktop.
Watch code on path (argv[1]) and say if there has been chages on port (argv[2]). The code for frontend in commented in the head. It will install Flask
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# setInterval(() => { | |
# fetch('http://127.0.0.1:15555', {mode: 'cors'}).then(res => res.text().then(text => { | |
# if (text === '1') { | |
# location.reload(); | |
# } | |
# })); | |
# }, 1000); | |
import os | |
import subprocess | |
import sys | |
if sys.platform == "linux" or sys.platform == "linux2": | |
pkgs = ['Flask', 'inotify'] | |
elif sys.platform == "darwin": | |
pkgs = ['Flask', 'MacFSEvents'] | |
else: | |
print('Windows is not supported for now') | |
sys.exit() | |
subprocess.call(['pip', 'install'] + pkgs, stdout=open(os.devnull, 'w')) | |
import datetime | |
from flask import Flask, Response | |
import logging | |
path = sys.argv[1] if len(sys.argv) > 1 else '.' | |
port = sys.argv[2] if len(sys.argv) > 2 else '15555' | |
to_reload = False | |
def callback(*args, **kwargs): | |
global to_reload | |
print('[' + datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') + '] Refreshing...') | |
to_reload = True | |
def watch_osx(callback): | |
from fsevents import Observer, Stream | |
observer = Observer() | |
stream = Stream(callback, path) | |
observer.start() | |
observer.schedule(stream) | |
def watch_linux(callback): | |
import inotify | |
i = inotify.adapters.Inotify() | |
i.add_watch(path) | |
for event in i.event_gen(yield_nones=False): | |
callback() | |
if sys.platform == "linux" or sys.platform == "linux2": | |
watch_linux(callback) | |
elif sys.platform == "darwin": | |
watch_osx(callback) | |
app = Flask('live-reload') | |
log = logging.getLogger('werkzeug') | |
log.setLevel(logging.ERROR) | |
@app.route("/") | |
def is_reloaded(): | |
global to_reload | |
res = Response('1' if to_reload else '0') | |
res.headers['Access-Control-Allow-Origin'] = '*' | |
to_reload = False | |
return res | |
app.run(host='0.0.0.0', port=port) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment