Create Covid-19 data list using Python in 5 minute


This tutorial will tell how you can create a quick list of Covid-19 pandemic real time data list using simple Python Scrip.

For this we are using data API provided by CovidTracking.com. Our example relay on the JSON data file which offer an up to dated covid-19 data set.

Web Request and urllib2

First we need a web request and bring all the JSON object from the internet. Using urllib2 standard Python library we can fetch the web content as follows.

urldata = 'https://covidtracking.com/api/v1/states/current.json'
    weburl = urllib2.urlopen(urldata)
    print("result code :" + str(weburl.getcode()))
    if (weburl.getcode() == 200):
        data = weburl.read()
        printData(data)
    else:
        print "Some server related error occurs"
PYTHON SPIDER IDE

Reading and Writing Json objects in C#


JSON objects are fast to read. From Python to C# JSON objects are widely used for the database as well as data transaction purposes, some of them are simple, some others are complicated.

So let begins with a simple C# Projects

JSON STRINGS in C#
SERIALIZATION OF JSON STRINGS in C#

Todo

  1. Build a class for constructing JSON string
  2. Write to JSON text file
  3. Read from the file back to JSON class

Read data from GitHub hosted json file in Flutter


This is my first flutter tutorial, which explain how you can host and read json data from GitHub repository.

First up all you need to host json in GitHub Page , here is the complete guide. After that you can read the data using http package in Flutter.

First up all you need to specify the pubspec.yml settings as follows

dependencies:
  flutter:
    sdk: flutter
  http: ^0.12.0+2

Go to your pubspec file and add the high lighted sections to use the network/web connection.

Now we need a Model class to read and hold the json object. Eventually the fetchpost Future will extract the data from GitHub.

Future<Post> fetchPost() async {
  final response = await http.get('https://jdata-server.github.io/data.json');
  if (response.statusCode == 200) {
    // If server returns an OK response, parse the JSON
    return Post.fromJson(json.decode(response.body));
  } else {
    // If that response was not OK, throw an error.
    throw Exception('Failed to load post');
  }
}

A special function called FutureBuilder help us to feched post object.Here is the complete main.dart file

import 'package:flutter/material.dart';
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'dart:async';
Future<Post> fetchPost() async {
  final response = await http.get('https://jdata-server.github.io/data.json');
  if (response.statusCode == 200) {
    // If server returns an OK response, parse the JSON
    return Post.fromJson(json.decode(response.body));
  } else {
    // If that response was not OK, throw an error.
    throw Exception('Failed to load post');
  }
}
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}
class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;
  @override
  _MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
  Future<Post> post;
  void initState() {
    super.initState();
    post = fetchPost();
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
          child: FutureBuilder<Post>(
        future: post,
        builder: (context, snapshot) {
          if (snapshot.hasData) {
            return new ListView(children: <Widget>[
              new Text("Q:" + snapshot.data.question),
              new Text("A:" + snapshot.data.answer)
            ]);
          } else if (snapshot.hasError) {
            return Text("${snapshot.error}");
          }
          // By default, show a loading spinner
          return CircularProgressIndicator();
        },
      )),
      floatingActionButton: FloatingActionButton(
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}

class Post {
  final String question;
  final String answer;
  final String catagory;
  Post({this.question, this.answer, this.catagory});
  factory Post.fromJson(Map<String, dynamic> json) {
    return Post(
        question: json['question'],
        answer: json['answer'],
        catagory: json['catagory']);
  }
}

Create and read simple JSON_STRING in Android Studio


JSON_STRING strings are similar to Dictionary and List in Python, they can be searched quickly. JSON_STRING can be used to store data for your application.

This post will teach you how to create and read JSON_STRING objects in Android Studio/Eclipse Android apps.

  1. Go ahead and create a simple activity app
  2. Switch to Android View
  3. Create simple JSON_STRING as follows in the MainActivity.Java class
    String JSON_STRING = "{\"DSLR\":{\"Model\":\"Sony Alpha 58\",\"Sensor\":APC}}";

    [Drop the above code at the beginning of the OnCreate method]

  4. Open main_activity.xml [can find under res/layout]
  5. Drag and Drop two TextView control to the Form.
  6. Change the name of the controls in the content_main.xml file as ‘model’ and ‘sensor’ respectively
  7. Now create variable to read a string from JSON and assign the values to controls
String model, sensor;
TextView Model, Sensor;
Model = (TextView) findViewById(R.id.model);
Sensor = (TextView) findViewById(R.id.sensor);
try {
 // get JSONObject from JSON file
 JSONObject obj = new JSONObject(JSON_STRING);
 // fetch JSONObject named employee
 JSONObject dslr = obj.getJSONObject("DSLR");
 // get employee name and salary
 model = dslr.getString("Model");
 sensor = dslr.getString("Sensor");
 // set employee name and salary in TextView's
 Model.setText("Model: "+model);
 Sensor.setText("Sensor: "+sensor);

} catch (JSONException e) {
 e.printStackTrace();
}

Alt+Enter to auto import libraries. Here the JSONObject will help you to fetch ‘DSLR’ String. In our example, we have one only one JSON string so that used two Text views only.

Remember JSON strings are case sensitive.

You can download the source code from GitHub

Create a simple Jason File


Like XML files JSON file can hold data in the form of  list or list of list. If you are familiar with Python list, it can be simplified. Using your favorite text editor you can create a Jason file.

JSON file can be used to store data for static web pages or can be used to transfer data between application. Let’s look at the very simple Jason file.

"institution":
[{
"name" : "RMHS MELATTUR",
"location" : "Melattur",
"catagory":"School",
"landmark":"Opp. GOVT. Hospital PMNA",
"distance":"12.km",
"Office phone":"947854545",
"district" :"Malappuram",
"web" :"http://rmhs.com",
"email" :"rmhs@outlook.com",
"course_offering" :["Plus Comp. Science","Plus Science"]
}]

In the above example we have a list called institution, and lots of keys and values, say location is a key and Melattur is the key value, you can identify the value with the key.  Each key is separated with a ‘.’ operator. and you can also have sub list which can be included within a ‘[‘ and ‘]’.

Can I hold more than one information in a Jason? yes. In the following format, within the same list.

{
"institutions":
[{
"name" : "RMHS MELATTUR",
"location" : "Melattur",
"catagory":"School",
"landmark":"Opp. GOVT. Hospital PMNA",
"distance":"12.km",
"Office phone":"947854545",
"district" :"Malappuram",
"web" :"http://rmhs.com",
"email" :"rmhs@outlook.com",
"course_offering" :["Plus Comp. Science","Plus Science"]
},
{
"name" : "NSS School",
"location" : "Manjeri",
"catagory":"School",
"landmark":"Opp. Municipality",
"distance":"12.km",
"Office phone":"947854545",
"district" :"Malappuram",
"web" :"http://rmhs.com",
"email" :"rmhs@outlook.com",
"course_offering" :["Plus Comp. Science","Plus Science"]
},
{
"name" : "MODEL School",
"location" : "Pandikkad",
"catagory":"School",
"landmark":"Opp.Buss stand",
"distance":"12.km",
"Office phone":"947854545",
"district" :"Malappuram",
"web" :"http://rmhs.com",
"email" :"rmhs@outlook.com",
"course_offering" :["Plus One Comp. Science","Plus One Science"]
},
{
"name" : "CENTRAL SCHOOL",
"location" : "Perinthelmanna",
"catagory":"School",
"landmark":"Opp. GOVT. Hospital PMNA",
"distance":"12.km",
"Office phone":"947854545",
"district" :"Malappuram",
"web" :"http://rmhs.com",
"email" :"rmhs@outlook.com",
"course_offering" :["Plus One Comp. Science","Plus One Science"]
},
{
"name" : "NSS COLLAGE",
"location" : "Palakkad",
"catagory":"Collage",
"landmark":"Opp. GOVT. Hospital PMNA",
"distance":"12.km",
"Office phone":"947854545",
"district" :"Palakkad",
"web" :"http://rmhs.com",
"email" :"rmhs@outlook.com",
"course_offering" :["BSc Comp. Science","BSc Physics,BSc Maths"]
},
{
"name" : "MES BED COLLAGE",
"location" : "Edathanattukara",
"catagory":"Collage",
"landmark":"Opp. GOVT. Hospital PMNA",
"distance":"12.km",
"Office phone":"947854545",
"district" :"Malappuram",
"web" :"http://rmhs.com",
"email" :"rmhs@outlook.com",
"course_offering" :["SS,MALAYALAM,ENGLISH"]
}
 ]
}

The file extension must be.json.

In the next post, we will learn how to read a Jason file in Python.