Spaces:
Runtime error
Runtime error
import gradio as gr | |
import requests | |
import json | |
main_directory = "https://services.swpc.noaa.gov/" | |
html = """ | |
<div> | |
PAGE_LINK | |
</div> | |
""" | |
def load_json(url1="",url2="",url3="",url4=""): | |
get_url=f'{main_directory}{url1}{url2}{url3}{url4}' | |
print(f'{get_url}') | |
get_url=get_url.split(".json")[0]+".json" | |
if get_url.endswith('.json'): | |
feed1 = requests.get(get_url) | |
return feed1.text | |
return None | |
def make_tree(url1="",url2="",url3="",url4=""): | |
link_box=[] | |
html_out="" | |
get_url=f'{main_directory}{url1}{url2}{url3}{url4}' | |
print(f'######### :: {get_url}') | |
if not get_url.endswith('.json'): | |
feed1 = requests.get(get_url) | |
spl = feed1.text.split("href=") | |
for line in spl: | |
spl2 = line.split(">")[0] | |
print(spl2) | |
if spl2.endswith('/"') or spl2.endswith('.json"'): | |
fin=line.split(">")[0].strip('""') | |
link_box.append(fin) | |
#html_out=html_out+html.replace("PAGE_LINK",fin) | |
return gr.update(choices=[l for l in link_box],interactive=True) | |
else: | |
return None | |
def get_images(): | |
html_out="<div>" | |
get_url=f'{main_directory}images/' | |
feed1 = requests.get(get_url) | |
spl = feed1.text.split("href=") | |
for line in spl: | |
spl2 = line.split(">")[0].strip('""') | |
print(spl2) | |
html_out+=f'<div><img src={get_url}{spl2}></div>' | |
html_out+="</div>" | |
return html_out | |
def run(): | |
out=make_tree() | |
im_html=get_images() | |
return out, im_html | |
with gr.Blocks() as app: | |
with gr.Tab("Images"): | |
html_im=gr.HTML() | |
with gr.Tab("Raw"): | |
with gr.Row(): | |
drop1=gr.Dropdown() | |
drop2=gr.Dropdown() | |
drop3=gr.Dropdown() | |
drop4=gr.Dropdown() | |
load_btn=gr.Button("Load JSON") | |
links=gr.JSON() | |
###### Images ########## | |
####### Raw ############ | |
load_btn.click(load_json,[drop1,drop2,drop3,drop4],links) | |
drop1.change(make_tree,drop1,[drop2]) | |
drop2.change(make_tree,[drop1,drop2],[drop3]) | |
drop3.change(make_tree,[drop1,drop2,drop3],[drop4]) | |
###################### | |
app.load(run,None,[drop1,html_im]) | |
app.launch() |