arXiv-reader / app.py
broadfield's picture
Update app.py
590e222 verified
raw
history blame
2.12 kB
import gradio as gr
import xmltodict
import requests
import json
style="""
.title_div{
font-size: x-large;
font-weight: 700;
margin-bottom: 10px;
color: white;
}
.card_div{
background: #050523;
color: white;
margin: 10px;
padding: 15px;
border-radius: 5px;
}
.x_btn{
font-size: xx-large;
font-weight: 900;
background: chocolate;
width: fit-content;
padding: 0 10px;
border-radius: 10px 10px 0 0;
float: right;
}
.frame_class{
display:none;
}
"""
head = """
<script>
function run(inn) {
console.log(inn);
var frame_on = document.getElementById("frame" + String(inn));
frame_on.style.display = 'block';
}
function closefn(inn) {
console.log(inn);
var frame_off = document.getElementById("frame" + String(inn));
frame_off.style.display = 'none';
}
</script>
"""
def search(q,rn):
api="http://export.arxiv.org/api/query?search_query=SQ&start=0&max_results=MR"
r=requests.get(api.replace("SQ",q).replace("MR",str(rn)))
cont=xmltodict.parse(r.content)['feed']['entry']
html=""
for i,c in enumerate(cont):
pdflink=c['id'].replace('/abs/','/pdf/')
html+=f"""<div class='card_div' id='id{i}' onclick="run({i})">
<div class='title_div'>{c['title']}</div>
<div style='color:white;'>{c['summary']}</div>
<div><a href='{pdflink}' target='_blank'>{pdflink}</a></div>
</div>
<div id=frame{i} class='frame_class'>
<div id=close{i} class='x_btn' onclick='closefn({i})'>X</div>
<iframe src="https://docs.google.com/viewer?url={c['id'].replace('/abs/','/pdf/')}&embedded=true" frameborder="0" height="1200px" width="100%"></iframe></div>"""
return(json.dumps(cont,indent=4)),html
with gr.Blocks(css=style,head=head) as b:
with gr.Group():
with gr.Row():
query=gr.Textbox(label="Query")
num=gr.Number(label="Count",step=1,value=10)
sub=gr.Button()
html_out=gr.HTML()
json_out=gr.JSON()
sub.click(search,[query,num],[json_out,html_out])
b.launch()