Working calculator
This commit is contained in:
parent
b170557b24
commit
bad5852033
29
app.py
29
app.py
|
@ -156,14 +156,13 @@ def parse_trains(flush: bool = False):
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
|
||||||
|
|
||||||
def find_routes(day, orig, dest):
|
def find_routes(day, origin, destination):
|
||||||
trains = parse_trains(filter_day=date(2023, 2, 17),
|
trains = db.session.query(Train).filter_by(day=day, tgvmax=True).all()
|
||||||
filter_tgvmax=True)
|
|
||||||
|
|
||||||
trains.sort(key=lambda train: train.dep)
|
trains.sort(key=lambda train: train.dep)
|
||||||
|
|
||||||
origin = "STRASBOURG"
|
origin = "STRASBOURG"
|
||||||
dest = "LYON (intramuros)"
|
destination = "LYON (intramuros)"
|
||||||
|
|
||||||
explore = []
|
explore = []
|
||||||
per_arr_explore = {}
|
per_arr_explore = {}
|
||||||
|
@ -172,7 +171,7 @@ def find_routes(day, orig, dest):
|
||||||
for train in tqdm(trains):
|
for train in tqdm(trains):
|
||||||
if train.orig == origin:
|
if train.orig == origin:
|
||||||
it = [train]
|
it = [train]
|
||||||
if train.dest == dest:
|
if train.dest == destination:
|
||||||
# We hope that we have a direct train
|
# We hope that we have a direct train
|
||||||
valid_routes.append(it)
|
valid_routes.append(it)
|
||||||
else:
|
else:
|
||||||
|
@ -190,7 +189,7 @@ def find_routes(day, orig, dest):
|
||||||
|
|
||||||
if last_train.arr <= train.dep:
|
if last_train.arr <= train.dep:
|
||||||
new_it = it + [train]
|
new_it = it + [train]
|
||||||
if train.dest == dest:
|
if train.dest == destination:
|
||||||
# Goal is achieved
|
# Goal is achieved
|
||||||
valid_routes.append(new_it)
|
valid_routes.append(new_it)
|
||||||
else:
|
else:
|
||||||
|
@ -293,7 +292,6 @@ def index():
|
||||||
return render_template('index.html', today=date.today(), max_day=date.today() + timedelta(days=30))
|
return render_template('index.html', today=date.today(), max_day=date.today() + timedelta(days=30))
|
||||||
|
|
||||||
|
|
||||||
@cli.command('test')
|
|
||||||
@app.get('/api/iata-codes/')
|
@app.get('/api/iata-codes/')
|
||||||
def iata_codes():
|
def iata_codes():
|
||||||
query = db.session.query(Train).with_entities(Train.orig_iata, Train.orig).distinct()
|
query = db.session.query(Train).with_entities(Train.orig_iata, Train.orig).distinct()
|
||||||
|
@ -307,5 +305,22 @@ def iata_codes():
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@app.get('/api/routes/<day>/<origin>/<destination>/')
|
||||||
|
def get_routes(day: date, origin: str, destination: str):
|
||||||
|
routes = find_routes(day, origin, destination)
|
||||||
|
return [
|
||||||
|
[{
|
||||||
|
'origin': tr.orig,
|
||||||
|
'origin_iata': tr.orig_iata,
|
||||||
|
'destination': tr.dest,
|
||||||
|
'destination_iata': tr.dest_iata,
|
||||||
|
'departure': tr.dep.isoformat(),
|
||||||
|
'arrival': tr.arr.isoformat(),
|
||||||
|
'number': tr.number,
|
||||||
|
'free_seats': tr.remaining_seats,
|
||||||
|
} for tr in route] for route in routes
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
app.run(debug=True)
|
app.run(debug=True)
|
||||||
|
|
|
@ -28,15 +28,17 @@
|
||||||
</div>
|
</div>
|
||||||
<div class="mb-3">
|
<div class="mb-3">
|
||||||
<label for="day" class="form-label">Jour de départ :</label>
|
<label for="day" class="form-label">Jour de départ :</label>
|
||||||
<input type="date" class="form-control" id="origin" aria-describedby="dayHelp"
|
<input type="date" class="form-control" id="day" aria-describedby="dayHelp"
|
||||||
min="{{ today }}" max="{{ today }}" value="{{ today }}">
|
min="{{ today }}" max="{{ max_day }}" value="{{ today }}">
|
||||||
<div id="dayHelp" class="form-text">Le jour de votre départ.</div>
|
<div id="dayHelp" class="form-text">Le jour de votre départ.</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="mb-3">
|
<div class="mb-3">
|
||||||
<input type="submit" class="form-control btn btn-primary" value="Rechercher…">
|
<input type="submit" class="form-control btn btn-primary" value="Rechercher…">
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
|
<div id="result"></div>
|
||||||
</body>
|
</body>
|
||||||
|
|
||||||
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js"
|
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js"
|
||||||
integrity="sha384-w76AqPfDkMBDXo30jS1Sgez6pr3x5MlQ1ZAGC+nuZB+EYdgRZgiwxhTBTkF7CXvN"
|
integrity="sha384-w76AqPfDkMBDXo30jS1Sgez6pr3x5MlQ1ZAGC+nuZB+EYdgRZgiwxhTBTkF7CXvN"
|
||||||
crossorigin="anonymous"></script>
|
crossorigin="anonymous"></script>
|
||||||
|
@ -72,8 +74,32 @@
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
let day_elem = document.getElementById('day')
|
||||||
|
let result_elem = document.getElementById('result')
|
||||||
document.getElementById('form').addEventListener('submit', () => {
|
document.getElementById('form').addEventListener('submit', () => {
|
||||||
console.log("Hello world!")
|
result_elem.innerHTML = 'Chargement…'
|
||||||
|
fetch('/api/routes/' + day_elem.value + '/' + origin_iata_elem.value + '/' + destination_iata_elem.value + '/')
|
||||||
|
.then(resp => resp.json())
|
||||||
|
.then(routes => {
|
||||||
|
console.log(routes)
|
||||||
|
result_elem.innerHTML = ''
|
||||||
|
let routes_elem = document.createElement('ul')
|
||||||
|
result_elem.appendChild(routes_elem)
|
||||||
|
for (let route in routes) {
|
||||||
|
route = routes[route]
|
||||||
|
console.log(route)
|
||||||
|
let route_elem = document.createElement('li')
|
||||||
|
routes_elem.appendChild(route_elem)
|
||||||
|
|
||||||
|
let text = route[0].origin
|
||||||
|
for (let train in route) {
|
||||||
|
train = route[train]
|
||||||
|
console.log(train)
|
||||||
|
text += " (" + train.departure + ") --> (" + train.arrival + ") " + train.destination + ", "
|
||||||
|
}
|
||||||
|
route_elem.textContent = text
|
||||||
|
}
|
||||||
|
})
|
||||||
})
|
})
|
||||||
</script>
|
</script>
|
||||||
</html>
|
</html>
|
||||||
|
|
Loading…
Reference in New Issue