...

/

PyInstaller: Bundle Python Applications

PyInstaller: Bundle Python Applications

Learn and practice to create stand-alone applications with PyInstaller.

Let’s revisit the app from the User Form: Select Starting Point lesson that lets the user select the starting point and receive the shortest route.

Press + to interact
from ortools.constraint_solver import routing_enums_pb2
from ortools.constraint_solver import pywrapcp
from python_tsp.distances import great_circle_distance_matrix
import pandas as pd
df=pd.read_csv('MoscowMcD.csv')
userStartingPoint = pd.read_csv('UserSelection.csv', header=None)
# create a new list called 'StoreSelectionIndex' that contain the index of the rows in the 'df' data frame where the 'Store' column matches any of the values in the 'userStartingPoints' list
storeSelectionIndex = df[df["Store"].isin(userStartingPoint[0])].index.values.tolist()
userStartingPoints2list=int(storeSelectionIndex[0])
def create_data_model():
"""Stores the data for the problem."""
data = {}
sources=df[['lat','lon']].to_numpy()
destinations=df[['lat','lon']].to_numpy()
distance_matrix = great_circle_distance_matrix(sources)
data['distance_matrix']=distance_matrix
data['num_vehicles'] = 1
data['depot'] = userStartingPoints2list
return data
def print_solution(manager, routing, solution):
"""Prints solution on console."""
#print('Objective: {} miles'.format(solution.ObjectiveValue()))
print('Objective: {} kilometers'.format(solution.ObjectiveValue() * 0.00160934))
index = routing.Start(0)
plan_output = 'Route for vehicle 0:\n'
route_distance = 0
while not routing.IsEnd(index):
plan_output += ' {} ->'.format(manager.IndexToNode(index))
previous_index = index
index = solution.Value(routing.NextVar(index))
route_distance += routing.GetArcCostForVehicle(previous_index, index, 0)
plan_output += ' {}\n'.format(manager.IndexToNode(index))
print(plan_output)
plan_output += 'Route distance: {}miles\n'.format(route_distance)
def main():
"""Entry point of the program."""
# instantiate the data problem
data = create_data_model()
# create the routing index manager
manager = pywrapcp.RoutingIndexManager(len(data['distance_matrix']),
data['num_vehicles'], data['depot'])
# create Routing Model
routing = pywrapcp.RoutingModel(manager)
def distance_callback(from_index, to_index):
"""Returns the distance between the two nodes."""
# convert from routing variable Index to distance matrix NodeIndex
from_node = manager.IndexToNode(from_index)
to_node = manager.IndexToNode(to_index)
return data['distance_matrix'][from_node][to_node]
transit_callback_index = routing.RegisterTransitCallback(distance_callback)
# define cost of each arc
routing.SetArcCostEvaluatorOfAllVehicles(transit_callback_index)
# set first solution heuristic
search_parameters = pywrapcp.DefaultRoutingSearchParameters()
search_parameters.first_solution_strategy = (
routing_enums_pb2.FirstSolutionStrategy.PATH_CHEAPEST_ARC)
# solve the problem
solution = routing.SolveWithParameters(search_parameters)
if solution:
print_solution(manager, routing, solution)
if __name__ == '__main__':
main()

Wouldn’t it be cool to share this application with colleagues who don’t have Python installed so they can also use the app without having to worry about any Python package installations?

Introduction to PyInstaller

...