space_weather / app.py
Omnibus's picture
Update app.py
e4e5afb verified
raw
history blame
3.18 kB
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"') or spl2.endswith('.png"') or spl2.endswith('.gif"') or spl2.endswith('.jpg"'):
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")
html_raw=gr.HTML()
links=gr.JSON()
###### Images ##########
####### Raw ############
load_btn.click(load_json,[drop1,drop2,drop3,drop4],[html_raw,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()