How to assign python variable to Javascript variable in web2py

You might be using following statement to assign python variable to javascript variable on older versions of web2py (<2.9.11), may be because of this question on stackoverflow:

Above statement was working on web2py 2.9.11 and before because of behavioral bug in web2py, which got fixed in web2py 2.9.11. Now response.json()  sets content-type = application/json . So because of above line, on web2py >2.9.11 html code is displayed in browser instead of rendering it as html.

Correct way:

First convert python variable to json in controller and then pass it to view. And in view, use XML() to assign it to javascript variable so that it is not escaped



Other solution is use ASSIGNJS html helper




How to change admin app url in web2py?

This question was asked here How to rename “/admin” to other URL in web2py? For all the web2py applications, web2py admin url is same, i. e. or One should prevent access to admin interface  to public or … [Continue reading]

How to do file validation in web2py and write custom validator to validate files?

In this article, I will explain built in validator [crayon-6330c89098043415666482-i/]  and [crayon-6330c8909804e058709863-i/], also a custom validator. It is important to validate file uploaded on server to restrict malicious files like .py .php, … [Continue reading]

How to write custom download controller in web2py?

Suppose we have employee table with resume as uplaod field. And we want to download file on the basis of employee id. Download  url is like [crayon-6330c89098342992185475-i/] or [crayon-6330c8909834a842275417-i/] example: … [Continue reading]

How to password protect PDF file using Python?

We will use python library PyPDF2 to set password to pdf file. To install PyPDF2: [crayon-6330c89098574063649237/] We are using encrypt function of PyPDF2. encrypt(user_password, owner_password=None, use_128bit=True) user_password  – The … [Continue reading]

Memory profiling in Python using memory_profiler

To install memory_profiler: [crayon-6330c89098c30334415148/] Profile function/script: Add following line in script to import memory profiler: [crayon-6330c89098c38032722102/] Decorate the function you would like to profile with … [Continue reading]

Time profiling in python using cProfile

In this article we will cover following points:  Profile single function Profile complete python script How to read .prof file Profile web2py application To install cprofile on ubuntu use following … [Continue reading]

How to measure performance of code ? – Basic methods

In this article, we will cover some very basic methods to calculate performance of script. In next article we will cover advance tool like cProfile and memory_profiler. What is profiling? Profiling is program analysis that measures, for … [Continue reading]

Size Contest (SPOJ Challenge problem 378)

Given the set of integers, find the sum of all positive integers in it. Solutions can be sent in any language supported by SPOJ except Whitespace. Input t – number of test cases [t < 1000] On each of next t lines given a integer N [-1000 <= … [Continue reading]

Factorial (SPOJ problem 11)

View problem on SPOJ The most important part of a GSM network is so called Base Transceiver Station (BTS). These transceivers form the areas called cells (this term gave the name to the cellular phone) and every phone connects to the BTS with the … [Continue reading]