2

I'm not understanding the documentation on using Mako templates in python. I have a python file and I have the following code:

import json, requests, urllib2
from mako.template import Template
from mako.lookup import TemplateLookup
from pylons.templating import render_mako as render

url="www.data&format=json"
response = urllib2.urlopen(url)
data = json.load(response)
return (Template("hello Luisito! Here is the data: ${data}!").render(json_data=data))

could someone provide me with some more detail on how to render this json data into a webpage using the mako template? do i need another file with the template? if so, how?

Luisito
  • 895
  • 1
  • 7
  • 9
  • What part of your question here needs an HTML template library that string formatting cannot achieve? – OneCricketeer Nov 14 '16 at 22:27
  • What web framework are you using? You should go read the documentation on that. The `def navigation(self):` on its own is doing nothing. – OneCricketeer Nov 14 '16 at 22:31
  • @cricket_007 essentially the input will be a json object and i would like to use a template to display the results of that object – Luisito Nov 14 '16 at 22:37
  • As HTML, or just a JSON string? You can `print(data)`. I do not see a need for Mako in your question. Do you even have a webserver running to display HTML onto a page? – OneCricketeer Nov 14 '16 at 22:42
  • @cricket_007 I've made edits to the code thus far, elimanating the navigation method. I'm not sure how to get the server running – Luisito Nov 14 '16 at 22:44
  • Mako is not a Server. I think you should [keep reading the documentation](http://docs.makotemplates.org/en/latest/usage.html#using-file-based-templates). Regarding the server, forget Mako, use [Jinja & Flask](http://flask.pocoo.org/docs/0.11/tutorial/views/) – OneCricketeer Nov 14 '16 at 22:45

2 Answers2

0

You need to add a variable for the json data:

url="www.data&format=json"
response = urllib2.urlopen(url)
data = json.load(response)   
from mako.template import Template
print(Template("hello Luisito! Here is the data: ${json_data}!").render(json_data=data))
rofls
  • 4,993
  • 3
  • 27
  • 37
0
from mako.template import Template
from mako.lookup import TemplateLookup
from mako import exceptions
from mako.exceptions import RichTraceback
import json

 var data = {
    "Records": [
        { 
            "pageName":"oracle.html", 
            "seoMetaData":"A page's description, usually one or two sentences.", 
            "logoImg":"./images/oracle-cloud-logo-400x336.png", 
            "logoImgUrl":"http://www.oracle.com",
            "description": "Oracle Cloud is a cloud computing service offered by Oracle Corporation providing servers, storage, network, applications and services through a global network of Oracle Corporation managed data centers. The company allows these services to be provisioned on demand over the Internet. Also, we provide the list of companies that use Oracle Cloud.",
            "product": "Oracle",
            "category": "CRM & Related",
            "customerAccount": "125,000 - 150,000"
        },
        { 
            "pageName":"microsoft.html", 
            "seoMetaData":"A page's description, usually one or two sentences.", 
            "logoImg":"./images/oracle-cloud-logo-400x336.png", 
            "logoImgUrl":"http://www.microsoft.com",
            "description": "Microsoft Cloud is a cloud computing service offered by Microsoft Corporation providing servers, storage, network, applications and services through a global network of Microsoft Corporation managed data centers. The company allows these services to be provisioned on demand over the Internet. Also, we provide the list of companies that use Microsoft Cloud.",
            "product": "Microsoft",
            "category": "CRM & Related",
            "customerAccount": "200,000 - 250,000"
        }
    ]
}

mylookup = TemplateLookup(directories=['./html'], output_encoding='utf-8', encoding_errors='replace')
uri = 'base.html'

def browseLocal(webpageText, filename):
    '''Start your webbrowser on a local file containing the text
    with given filename.'''
    import webbrowser, os.path
    strToFile(webpageText, filename)
    # webbrowser.open("file:///" + os.path.abspath(filename)) #elaborated for Mac.

def strToFile(text, filename):
    """Write a file with the given name and the given text."""
    output = open(filename,"w")
    output.write(text)
    output.close()

def fileToStr(fileName): # NEW
    """Return a string containing the contents of the named file."""
    fin = open(fileName); 
    contents = fin.read();  
    fin.close() 
    return contents

f = open("data.json", "r")
data = f.read()
jsonData = json.loads(data)
list1 = jsonData["Records"]
for val in list1:
    pageName = val["pageName"]
    seoMetaData = val["seoMetaData"]
    logoImg = val["logoImg"]
    logoImgUrl = val["logoImgUrl"]
    description = val["description"]
    product = val["product"]
    category = val["category"]
    customerAccount = val["customerAccount"]

    template = mylookup.get_template(uri)
    htmlContent = template.render_unicode(name=pageName, seoMetaData=seoMetaData, logoImg=logoImg, logoImgUrl=logoImgUrl, description=description, product=product, category=category, customerAccount=customerAccount)
    browseLocal(htmlContent, pageName)
f.close()