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.
- 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 like cherrypy can installed using easy-install or pip.
easy_install Jinja2 pip install Jinja2
Language : Python 3.5, HTML
Platform : Backend and Fronted Web
IDE : PyCharm
Topics : Object Oriented Python, Cherrypy JSON,Python List
- Cherrypy convert list from python code to json.
- CherryPy web development example
- Python web development.
These are the third party CSS and JS used in this project.
- Here's the screenshot of the project.
- Here's the structure of the project.
Lets have a look at the source code.
- 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())
- For detailed explanation as well as demo please check Video version of this tutorial below.
- You can Download the full Project below:
- Download the project above.
- You'll get a zipped file,extract it.
- Extract and import into your IDE and run.
- Visit our channel for more examples like these.
- Lets share tips and ideas in our Facebook Page.