1

I'm creating with gradio, a programs that connects to switches via api and can do various operations. The code is working fine, but I would like that the output text inbox to be 'fed' in real-time, Meaning, I don't want to see the results when the whole loop is done. I want each time there is a new line in the destination files to see their output in the output text area.

import gradio as gr
import ipaddress
import requests
from requests.auth import HTTPBasicAuth


###SWITCH###
def switch_ver(ip):
    ip_addr = ip.split()
    for i in ip_addr:
        ip_addr = list(ipaddress.ip_network(i))
        try:
            basic=HTTPBasicAuth('some','password')
            login = requests.post('http://'+i+':80/rest/v7/login-sessions', auth=basic)
            get_ver = requests.get('http://'+i+':80/rest/v7/system/status')
            get_ver = get_ver.json()
            get_ver = get_ver['firmware_version']
            get_ver = get_ver
            with open('switches_good_results.txt', 'a+') as sw:
                results = 'Switch version for {} is: {} \n'.format(i, get_ver)
                sw.write(results)
            #return 'Switch version for {} is: {} \n'.format(i, get_ver)
                
        except requests.exceptions.ConnectTimeout:
            timeout = 'Could not connect to switch: '+i+' REQUEST TIMED OUT\n'
            with open('switches_bad_results.txt', 'a+') as sw:
                sw.write(timeout)

    with open('switches_good_results.txt','r') as switches_bad, open('switches_bad_results.txt', 'r') as switches_good:
       summary = switches_bad.read() + switches_good.read()


    return (summary),['switches_good_results.txt', 'switches_bad_results.txt']

            
        
###IPBlockerK###
def block_ip(ip):
    ip_addr = ip.split()
    for i in ip_addr:
        ip_addr = list(ipaddress.ip_network(i))
        with open('fortigate.txt', 'a+') as f:
            f.write('ping ' + i + '\n')
            if i in f.read():
                return i + ' ' ' is a duplicate!'

    with open('fortigate.txt', 'r') as f:
       return f.read()


with gr.Blocks(title = 'Switcher') as switches_ver:
    gr.Markdown('Welcome to IPBlocker')
    with gr.Tab(label = 'IPBlocker'):
        with gr.Row():
            ips_to_block = gr.Textbox(label = " ", lines = 10, PlaceHolder=('Please fill Ips to block'))
    with gr.Tab(label = 'Switcher'):
        with gr.Row():   
            with gr.Column():
                switch_box = gr.Textbox(label = 'Switches', lines = 10, placeholder='Please fill switches IPs...')
                show_ver = gr.Button('Show current switches version')
                upgrade_ver = gr.Button('Upgrade selected switches')
            output_textbox = gr.Textbox(label='Results',lines = 10)
        with gr.Column():
            output_file = gr.File(['switches_good_results.txt', 'switches_bad_results.txt'])
            show_ver.click(fn=switch_ver, inputs = switch_box, outputs = [output_textbox, output_file])

switches_ver.launch()

Thank you!

LittleWing
  • 21
  • 6

1 Answers1

0

maybe there's something to do with the "every" parameter documented here.

docs_of_every_parameter

  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 14 '23 at 05:00