Python Web S1E3 : CherryPy - List to JSON

In the previous tutorial we looked at how to render a list in a material list using cherrypy. Cherrypy is an object oriented minimalistic framework that allows us make web apps easily. In this example, let's see how to serve json data from our app. We convert a python list to json data and server it so that client apps can consume it. Please watch the below video tutorial for more detail.

Cherrypy Installation

  • To Install Cherrypy use any of the following commands:
easy_install cherrypy
pip install cherrypy
You can also get it from github:
git clone https://github.com/cherrypy/cherrypy
cd cherrypy
python setup.py install
For more information on cherrypy installation please here is detailed guide.

Jinja Installation

  • Jinja like cherrypy can installed using easy-install or pip.
easy_install Jinja2
pip install Jinja2

Tools Used

Language : Python 3.5, HTML Platform : Backend and Fronted Web IDE : PyCharm Topics : Object Oriented Python, Cherrypy JSON,Python List

Questions this Project answers.

  • Cherrypy convert list from python code to json.
  • CherryPy web development example
  • Python web development.

Libraries Used

These are the third party CSS and JS used in this project.

Screenshot

  • Here's the screenshot of the project.
[caption id="" align="alignnone" width="1064"] JSON Data[/caption]

Project Structure

  • Here's the structure of the project.
[caption id="" align="alignnone" width="226"] Project Structure[/caption]

Source Code

Lets have a look at the source code. [su_tabs] [su_tab title="Index.py"]
  • Our Main class.
  • Import cherrypy and json.
  • Create a function that dumps or prints our list into json
import cherrypy
import json
class Index(object):

    @cherrypy.expose()
    def index(self):
        galaxies=["Messier 81","StarBurst","Black Eye","Cosmos Redshift","Sombrero","Hoags Object","Andromeda","Centarus A","Whirlpool","Canis Major Overdensity"]
        return json.dumps(galaxies)

if __name__ == '__main__':
       cherrypy.quickstart(Index())
 

Video/Demo

  • For detailed explanation as well as demo please check Video version of this tutorial below.
https://youtu.be/QDxbdgNyeaw

Download

  • You can Download the full Project below:
Download

How to Download and Run.

  1. Download the project above.
  2. You'll get a zipped file,extract it.
  3. Extract and import into your IDE and run.

More

  • Visit our channel for more examples like these.
  • Lets share tips and ideas in our Facebook Page.
Oclemy,Cheers.