-
Notifications
You must be signed in to change notification settings - Fork 3
/
jsonp_bootstrapped.html
151 lines (128 loc) · 5.39 KB
/
jsonp_bootstrapped.html
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="author" content="">
<link rel="shortcut icon" href="../../assets/ico/favicon.png">
<title>Populate the page using JSONP</title>
<!-- Bootstrap core CSS -->
<link href="static/style/bootstrap.min.css" rel="stylesheet">
<!-- Custom styles for this template -->
<link href="static/style/starter-template.css" rel="stylesheet">
<script type="text/javascript" src="brython.js"></script>
<script type="text/python">
import html
import json
def processJSON(jsondata):
try:
del doc["tablebody"]
doc["mytable"] <= html.TBODY(Id = "tablebody")
except:
doc["mytable"] <= html.TBODY(Id = "tablebody")
api = doc['apiform'].value
if api == "Flickr":
for item in jsondata.items:
row = html.TR()
desc = html.P(html.H1(item.title))
desc = desc + html.P('Taken by: ' + html.EM(item.author))
desc = desc + html.P('Shot on: ' + html.EM(item.date_taken))
link = html.A(html.IMG(src = item.media.m), href = item.link)
row <= html.TD(desc) + html.TD(link)
doc["tablebody"] <= row
if api == "Reddit":
for item in jsondata.data.children:
data = item.data
row = html.TR()
desc = html.P(html.H1(html.A(data.title, href = data.url)))
score = str(data.score)
ups = str(data.ups)
downs = str(data.downs)
text = html.EM(score + '(' + ups + '/' + downs + ')')
desc = desc + html.P('Score: ' + text)
reddit = "http://www.reddit.com"
link = html.A(data.num_comments, href = reddit + data.permalink)
desc = desc + html.P('# comments: ' + link)
row <= html.TD(desc)
doc["tablebody"] <= row
def jsonp_call():
tag = doc['tagform'].value
api = doc['apiform'].value
try:
del doc["api_call"]
except:
pass
myscript = html.SCRIPT(Id = "api_call")
myscript.type = "text/javascript"
if api == "Flickr":
url = 'http://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=processJSON&tags=%s&tagmode=any&format=json' % tag
if api == "Reddit":
url = 'http://www.reddit.com/r/Python/search.json?q=%s&restrict_sr=on&sort=top&t=week&jsonp=processJSON' % tag
doc['request'].text = ''
doc['request'] <= html.P('requesting ' + url)
myscript.src = url
doc[html.HEAD][0] <= myscript
</script>
</head>
<body onload="brython()">
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="./jsonp_bootstrapped.html">JSONP-call-Brython</a>
</div>
<div class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li><a href="http://www.brython.info">Brython home page</a></li>
<li><a href="https://github.com/kikocorreoso/Brython-PyConES-2013/blob/master/apps/jsonp_bootstrapped.html">App source</a></li>
<li><a href="https://bitbucket.org/olemis/brython/src">Brython source</a></li>
</ul>
</div><!--/.nav-collapse -->
</div>
</div>
<div class="container">
<div class="starter-template">
<h1>JSONP call example made with Brython</h1>
<p class="lead">This simple app has been tested only in Firefox.</p>
<form class="form-horizontal" role="form">
<div class="form-group">
<label for="tagform" class="col-lg-2 control-label">Tag</label>
<div class="col-lg-10">
<input id="tagform" type="text" class="form-control" placeholder="Tag to search">
</div>
</div>
<div class="form-group">
<label for="apiform" class="col-lg-2 control-label">API</label>
<div class="col-lg-10">
<select id="apiform" class="form-control">
<option>Flickr</option>
<option>Reddit</option>
</select>
</div>
</div>
<div class="form-group">
<div class="col-lg-offset-2 col-lg-10">
<button type="button" class="btn btn-default" onclick="jsonp_call()">Search</button>
</div>
</div>
</form>
<div id="request"></div>
<table id="mytable" class="table table-striped table-bordered table-hover table-responsive">
</table>
</div><!-- /.starter-template -->
</div><!-- /.container -->
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="static/js/jquery.min.js"></script>
<!-- Bootstrap core JavaScript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="static/js/bootstrap.min.js"></script>
<!-- Brython scripts
================================================== -->
</body>
</html>