I needed to pass a list of error IP addresses from one Flask view to another. Flask views are just two different functions, they define the pages on the website.
The error IP list was created when on page devices/info and they are to be displayed on page devices/inventory as errors.
Method 1
This first method passes variables created in Python from one view to another.
Example Code
0 1 2 3 4 5 6 7 8 9 10 11 12 13 |
def function1(): ecs_list_parent = {'keys':'Values'} hbe1_list_parent = {'Morekeys':'MoreValues'} session["ecs_list_parent"]=ecs_list_parent session["hbe1_list_parent"]=hbe1_list_parent def collect_vars_from_function1(): ecs_list_parent = session.get("ecs_list_parent",None) ibe2_list_parent = session.get("ibe2_list_parent",None) |
In Action in Scrapli & Flask Project
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
@devices_blueprint.route('/info', methods = ['GET', 'POST']) def info(): startTime = time.time() form = DeviceForm() if form.validate_on_submit(): # Split string on comma and append to a list session['ip'] = form.ip.data ips = session.get('ip', None).replace(" ", "").replace("\r\n\r\n", "").replace("\r\n", "").lower().split(',') print(ips) error_ips = [] show_ver = CiscoCommands() # hostname,ip,sw_version,model,os_type,chassis_sn,uptime = show_ver.show_version(ip) genie_list, error_ips = show_ver.create_threads(ips) # Make sure inventory page can get the eror IPs session["error_ips"]=error_ips device_inventory = DBcontroller() for device in genie_list: device_inventory.update_device_inventory( device['Hostname'], device['IP'], device['Version'], device['Model'], device['OS'], device['Chassis'], device['Uptime']) device_inventory.close_db_conn() return redirect(url_for('devices.inventory')) else: form = DeviceForm() return render_template('info.html', form=form) @devices_blueprint.route('/inventory') def inventory(): all_devices = Devices.query.all() error_ips = session.get("error_ips",None) print(error_ips) return render_template('inventory.html', all_devices=all_devices, error_ips=error_ips) |
Method 2
This second method is slightly different as it is taking a variable created from WTForms.
WTForms allows the usage of forms in Flask. I have used them extensively.
The form has in this case IP addresses input. Upon submit those IP addresses are passed to a second view and printed in the console as a list.
0 1 2 3 |
127.0.0.1 - - [07/Nov/2022 14:56:20] "POST /devices/multicast HTTP/1.1" 302 - ['172.16.1.115', '172.16.1.116', '172.16.1.117', '172.16.1.118'] |
The code I have used in the views.py file is below. I needed to get the data, otherwise it is only the memory reference that is refurned. The .data is then passed via the user session.
I have then split the data on a comma so I have a list to itterate over.
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
@devices_blueprint.route('/multicast', methods = ['GET', 'POST']) def multicast(): form = MulticastForm() if form.validate_on_submit(): form_IPs = {'mcast_routers':[form.data]} session["form_IPs"]=form_IPs return redirect(url_for('devices.bg_get_mroute')) else: return render_template('multicast.html', form=form) @devices_blueprint.route('/bg_get_mroute', methods = ['GET', 'POST']) def bg_get_mroute(): """ Gets the multicast routing table and parses it """ # Get form values form_IPs = session.get("form_IPs", None) ips = form_IPs['mcast_routers'][0]['ip'].split(",") # print(f"Form IPs in BG: {form_IPs['mcast_routers'][0]['ip']}") print(ips) |