简体   繁体   中英

sharing variables info between python and javascript in cherrypy

I am looking to share variable info from python environment to java script in cherrypy as following:

class test(object):

        @cherrypy.expose
        def index(self):
            variableX = 2016

            return """<!DOCTYPE html>
<html>
<head>
<title>Set interval with two inputs</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
<link rel="stylesheet" type="text/css" href="/static/codebase/dhtmlxcalendar.css"/>
<script src="/static/codebase/dhtmlxcalendar.js"></script>
<style>
    input#date_from, input#date_to {
        font-family: Tahoma;
        font-size: 12px;
        background-color: #fafafa;
        border: #c0c0c0 1px solid;
        width: 100px;
    }
    span.label {
        font-family: Tahoma;
        font-size: 12px;
    }
</style>
<script>
    var myCalendar;
    function doOnLoad() {
        myCalendar = new dhtmlXCalendarObject(["date_from","date_to"]);

        // getting today info (start code)
        var tomm = new Date();
        var today = new Date();
        var dd = today.getDate();
        var mm = today.getMonth()+1; //January is 0!
        var yyyy = today.getFullYear();
        if(dd<10) {dd='0'+dd};
        if(mm<10) {mm='0'+mm};
        today = yyyy+'-'+mm+'-'+dd;
        // getting today info (end code)


        myCalendar.setDate(today);
        myCalendar.hideTime();
        // init values
        var t = new Date();
        byId("date_from").value = variableX ;
        byId("date_to").value = variableX ;

    }

    function setSens(id, k) {
        // update range
        if (k == "min") {
            myCalendar.setSensitiveRange(byId(id).value, null);
        } else {
            myCalendar.setSensitiveRange(null, byId(id).value);
        }
    }
    function byId(id) {
        return document.getElementById(id);
    }
</script>



</head>
<body onload="doOnLoad();">
        <form method="get" action="generate">

<div style="position:relative;height:280px;">
    <span class="label">From</span> <input type="text" id="date_from" name="FirstDate" onclick="setSens('date_to', 'max');" readonly="true">
    <span class="label">Till</span> <input type="text" id="date_to" name="LastDate" onclick="setSens('date_from', 'min');" readonly="true">


              <button type="submit">Give it now!</button>
        </form>
</div>

</body>


</html>"""

I am trying to get the value of variableX (which is 2016) inside the javascript within html. Any idea?

You just need to create a new javascript variable with the value of the variableX .

As in your example add this line:

...
// getting today info (start code)
var variableX = """ + str(variableX) + """;
var tomm = new Date();
...

But I really recommend you to learn how to use a template engine like Jinja2 and then create a index.html template like this:

...
// getting today info (start code)
var variableX = {{variabeX}};
var tomm = new Date();
...

And your python code will look like this:

@cherrypy.expose
def index(self):
    return jinja2env.get_template('index.html').render(variableX=2016)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM