I understand you should not use return but you should build a response, for
example:
def index():
import requests
import json
url = "https://10.10.10.1/wapi/2/"
object_type = "config_templates/search"
data = requests.get(url + object_type, verify=False, auth=('myuser',
'mypwd'))
json_input = data.text
try:
decoded = json.loads(json_input)
names = [entry['name'] for entry in decoded['config_templates']]
return dict(names=names)
except (ValueError, KeyError, TypeError):
return "JSON format error"
On Monday, 21 July 2014 17:21:39 UTC-5, Sif Baksh wrote:
>
> Massimo,
> Thanks for replying, I'm both new to Python and web2py (loving it so far)
>
> Here is an example of the JSON file this is showing one device config
> return:
> {
> "config_templates": [
> {
> "_class": "ConfigTemplate",
> "created_at": "2012-02-24 11:37:53",
> "created_by": "admin",
> "description": "C3750 config template",
> "device_type": "Switch-Router",
> "id": 1,
> "model": "",
> "name": "cisco_catalyst37xxstack",
> "risk_level": 3,
> "template_text": "version 12.2\nno service pad\nservice
> timestamps debug datetime msec\nservice timestamps log datetime msec\nno
> service password-encryption\n!\nhostname $hostname\n!\nenable password
> infoblox\n!\nboot-start-marker\nboot-end-marker\n!\n!\nusername admin
> privilege 15 password 0 infoblox\nno aaa new-model\nswitch 1 provision
> ws-c3750-48p\nsystem mtu routing 1500\nvtp domain cisco\nvtp mode
> transparent\nip subnet-zero\nip domain-name $domain_name\nno ip
> domain-lookup\n!\n!\n!\n! \n! \n! \n! \n!
> \n! \n! \nspanning-tree mode pvst\nspanning-tree
> extend system-id\n! \nvlan internal allocation policy ascending\n!
> \n! \n$config_vlans\n! \n!
> \n$config_interfaces\n!\n!\nno ip classless\nip default-gateway
> $gateway\nno ip http server\nno ip http secure-server\n!\n!\nlogging
> $syslog\nsnmp-server community infoblox RW\n!\ncontrol-plane\n!\n!\nline
> con 0\nline vty 0 4\n exec-timeout 300 0\n logging synchronous\n login
> local\n transport input telnet ssh\nline vty 5 15\n exec-timeout 300 0\n
> logging synchronous\n login local\n transport input telnet
> ssh\n!\nend\n\n",
> "template_type": "Device",
> "updated_at": "2014-03-13 17:27:03",
> "updated_by": "System",
> "vendor": "Cisco",
> "version": ""
> }
> ],
> "current": 1,
> "limit": 1000,
> "start": 0,
> "total": 1
> }
>
> But my total output is 52 of the above, I wanted to grab the name of each
> config template and display it on a WebPage.
>
> I might be doing something wrong, but I googled it and test it on the CLI
> and it worked fine :)
>
> cisco_catalyst37xxstack
> juniper_ex2200
> PSSwitchConfigTemplate
> Cisco-3620-12.4(25b)-RouterStandardConfig1
> intloopback
> CopyCiscoCatalyst3750
> ...
>
> Thanks,
> Sif
>
> On Sunday, July 20, 2014 9:30:51 AM UTC-6, Massimo Di Pierro wrote:
>>
>> I am not sure what you mean by:
>>
>> for entry in decoded['config_templates']:
>> return entry['name']
>>
>> why do you have a return in a loop? It will only return the first time.
>> The cli may do something unusual.
>>
>>
>> On Friday, 18 July 2014 16:15:11 UTC-5, Sif Baksh wrote:
>>>
>>>
>>> My very first besides the Hello World.
>>> It's pulling data from a RESTful Server and the output is JSON.
>>> We have 52 entries in our Config Templates and it just prints out one
>>> under web2py.
>>>
>>> When I run this in web2py it only return one result:
>>>
>>> def index():
>>>
>>> import requests
>>> import json
>>> url = "https://10.10.10.1/wapi/2/"
>>> object_type = "config_templates/search"
>>> data = requests.get(url + object_type, verify=False, auth=('myuser',
>>> 'mypwd'))
>>> json_input = data.text
>>> try:
>>> decoded = json.loads(json_input)
>>> for entry in decoded['config_templates']:
>>> return entry['name']
>>> except (ValueError, KeyError, TypeError):
>>> return "JSON format error"
>>>
>>>
>>> When I run the following from the cli it returns 52 entries:
>>>
>>> import requests
>>> import json
>>> url = "https://10.10.10.1/wapi/2/"
>>> object_type = "config_templates/search"
>>> data = requests.get(url + object_type, verify=False, auth=('myuser',
>>> 'mypwd'))
>>> json_input = data.text
>>> try:
>>> decoded = json.loads(json_input)
>>> for entry in decoded['config_templates']:
>>> return entry['name']
>>> except (ValueError, KeyError, TypeError):
>>> return "JSON format error"
>>>
>>> Do I need to do something different in web2py?
>>>
>>> Thanks in advance
>>> Sif
>>>
>>
--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.