Spaces:
Runtime error
Runtime error
File size: 3,077 Bytes
511891d 6f2a024 0b73570 6f2a024 bd24d05 288aecc 5b2b36d bd24d05 c721296 7839cc0 bd24d05 67949b6 25ec869 6f2a024 0d0db28 5b2b36d c721296 7839cc0 c721296 bd24d05 01f064c 1cb17a2 01f064c eea38a1 121c044 a582447 6e5e3c6 07eb494 01f064c 0b73570 cb3b6d4 01f064c 6f2a024 e8ad872 01f064c bd24d05 288aecc 01f064c e8ad872 |
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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
import gradio as gr
import requests
import json
main_directory = "https://services.swpc.noaa.gov/"
html = """
<div>
PAGE_LINK
</div>
"""
css="""
.img_box{
display: flex;
flex-direction: column;
flex-wrap: wrap;
height: 20000px;
}
.img_class{
background: #ffffff;
max-width: 48%;
font-family: monospace;
border-top: #9300ff;
border-style: inset;
margin-top: 5px;
}
"""
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("None")[0]
if get_url.endswith('.json'):
feed1 = requests.get(get_url)
return None, feed1.text
elif get_url.endswith(".png") or get_url.endswith(".gif") or get_url.endswith(".jpg"):
html_out=f"<style>{css}</style><div>"
html_out+=f'<div class="img_class"><a href="{get_url}" target="_blank">{get_url}</a><br><img src={get_url}></div></div>'
return html_out, None
return None,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=f"<style>{css}</style><div class='img_box'>"
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('""')
if spl2.endswith(".png") or spl2.endswith(".gif") or spl2.endswith(".jpg"):
print(spl2)
html_out+=f'<div class="img_class"><a href="{get_url}{spl2}" target="_blank">{get_url}{spl2}</a><br><img src={get_url}{spl2}></div>'
else:
print(spl2)
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],[html_im,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() |