Skip to content

Commit

Permalink
Merge pull request #62 from datakind/implicitsession
Browse files Browse the repository at this point in the history
Add an implicit default session if no files are uploaded and fix up m…
  • Loading branch information
Zebreu authored Apr 21, 2024
2 parents 7396eb3 + 610ce01 commit 4a07652
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
14 changes: 10 additions & 4 deletions dkroutingtool/src/py/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,18 @@ def update_vehicle_or_map(session_id: str=''):
if vehicle_files:
for curr_file in vehicle_files:
os.replace(curr_file, os.path.join('/osrm-backend/profiles', os.path.basename(curr_file)))
veh_directory = '/'+os.path.basename(curr_file)[:-4] # removing the extension
if not os.path.exists(veh_directory):
os.makedirs(veh_directory)
build_profiles = True
# Look for osm.pbf file. Just pulls the first in sorted order if multiple are present.
map_files = sorted(glob.glob(f'/data{session_id}/*.pbf'))
if map_files:
os.environ['osm_filename'] = 'upload_map'
os.replace(map_files[0], '/upload_map.osm')
os.replace(map_files[0], '/upload_map.osm.pbf')
build_profiles = True
if build_profiles:
temporary_build_profiles()
temporary_build_profiles(osmpbf=True)

@app.get('/get_solution')
def get_solution(session_id: str=''):
Expand Down Expand Up @@ -133,7 +136,7 @@ def get_vehicles():
return desired_vehicles


def temporary_build_profiles():
def temporary_build_profiles(osmpbf=False):
desired_vehicles = get_vehicles()

print('Extracting-contracting networks per vehicle')
Expand All @@ -149,7 +152,10 @@ def temporary_build_profiles():
if vehicle_name not in desired_vehicles:
continue
print('Building', vehicle_file)
subprocess.run(['osrm-extract', '-p', vehicle_file, f'/{osm_filename}.osm'])
if not osmpbf:
subprocess.run(['osrm-extract', '-p', vehicle_file, f'/{osm_filename}.osm'])
else:
subprocess.run(['osrm-extract', '-p', vehicle_file, f'/{osm_filename}.osm.pbf'])
subprocess.run(f'mv {osm_filename}.osrm* {vehicle_name}/', shell=True)
subprocess.run(['osrm-contract', f'{osm_filename}.osrm'], cwd=vehicle_name)

Expand Down
8 changes: 6 additions & 2 deletions dkroutingtool/src/py/ui/dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
st.set_page_config(page_title='Container-based Action Routing Tool (CART)', layout="wide")

runtime = get_instance()
session_id = get_script_run_ctx().session_id
session_id = ''

host_url = 'http://{}:5001'.format(os.environ['SERVER_HOST'])

def download_solution(solution_path, map_path):
Expand Down Expand Up @@ -68,6 +69,9 @@ def adjust(adjusted_file):
return message, solution, map, solution_zip

def upload_data(files_from_streamlit):
global session_id
session_id = get_script_run_ctx().session_id # Only identifies a session if configuration files are uploaded

files = [('files', file) for file in files_from_streamlit]

headers = {
Expand Down Expand Up @@ -117,7 +121,7 @@ def main():
if len(uploaded_files) > 0:
response = upload_data(uploaded_files)
st.write(response)
vehicle_or_map_update_requested = st.button('If updated vehicles + updated build_paramters.yml or a *.osm.pbf map was uploaded, click here to update.')
vehicle_or_map_update_requested = st.button('If you uploaded modified *.lua, build_parameters.yml, or *.osm.pbf files, click here to update the network')
if vehicle_or_map_update_requested:
with st.spinner('Rebuilding based on updated vehicles/maps. This may take a few minutes, please wait...'):
update_vehicle_or_map()
Expand Down

0 comments on commit 4a07652

Please sign in to comment.