Blog

  • Flask and JSON

    It is possible to use the Flask framework to play with json data. The programme looks like:

    # import main Flask class and request object
    from flask import Flask, request
    import json
    
    # create the Flask app
    app = Flask(__name__)
    
               
    @app.route('/json-example', methods=['POST'])
    def json_example():
        request_data = request.get_json()
    
        language = None
        framework = None
        python_version = None
        example = None
        boolean_test = None
    
        if request_data:
            if 'language' in request_data:
                language = request_data['language']
    
            if 'framework' in request_data:
                framework = request_data['framework']
    
            if 'version_info' in request_data:
                if 'python' in request_data['version_info']:
                    python_version = request_data['version_info']['python']
    
            if 'examples' in request_data:
                if (type(request_data['examples']) == list) and (len(request_data['examples']) > 0):
                    example = request_data['examples'][0]
    
            if 'boolean_test' in request_data:
                boolean_test = request_data['boolean_test']
    
        return '''
               The language value is: {}
               The framework value is: {}
               The Python version is: {}
               The item at index 0 in the example list is: {}
               The boolean value is: {}'''.format(language, framework, python_version, example, boolean_test)
    
        # Just render it
        return {} 
        
        
        
    if __name__ == '__main__':
        # run app in debug mode on port 5000
        app.run(debug=True, port=5000)

    This can be invoked by: python index.py

    It can be started with

    curl -i -H “Content-Type: application/json; charset=utf-8” -X POST –data “{\”language\”:\”Read a book\”, \”framework\”:\”Geile Beer\”}” 127.0.0.1:5000/json-example

  • A webserver from the command line

    Python has a module (Flask) that allows to run a Python module from the commmand line. The code looks like:

    # import main Flask class and request object
    from flask import Flask, request
    
    # create the Flask app
    app = Flask(__name__)
    
    @app.route('/form-example', methods=['GET', 'POST'])
    def form_example():
        if request.args.get('new') == 'NEW':
            return '''
                    <form method="POST" action ="http://127.0.0.1:5000/form-example">
                            <div><label>Language: <input type="text" name="language"></label></div>
                            <div><label>Framework: <input type="text" name="framework"></label></div>
                            <input type="submit" value="Submit">
                        </form>'''
        language = request.form.get('language')
        framework = request.form.get('framework')
        return '''
                    <h1>The language value is: {}</h1>
                    <h1>The framework value is: {}</h1>'''.format(language, framework)
               
    

    This can be run from the command line with: python index.py, which leads to a service that is started.

    Look at the code. If we provide an URL like “http://127.0.0.1:5000/form-example?new=NEW”, we see that two input fields are provided:

    If the submit button is pushed, we see that URL “http://127.0.0.1:5000/form-example” is used. This provides
  • Use the node.js server as restful app server

    One may use the node.js server as a means to get acquainted with restful services. Let us assume this server is installed and started with

    json-server "C:\Users\tomva\SynologyDrive\python\json server\db.json" --port 8000
    

    We can then see the json database within the browser “http://localhost:8000/extra”. We may add some information by:

    import requests
    import json
    api_url = "http://localhost:8000/extra"
    todo = {"userId": 1, "title": "Buy nore sugar", "completed": False}
    headers =  {"Content-Type":"application/json"}
    response = requests.post(api_url, data=json.dumps(todo), headers=headers)
    print(response.json())
    print(response.status_code)
    

    Data is added with ‘id’: ‘8701’. This id is important if one would like to change some data. Look at the put methode that is used below:

    import requests
    import json
    
    url = 'http://localhost:8000/extra/8701'
    data = {"id": 3,"userId": 2, "title": "drink beer",  "body": "drinking beer is nice"}
    headers = {'Content-Type':'application/json; charset=UTF-8'}
    response = requests.put(url, data=json.dumps(data), headers=headers)
    
    print(response.status_code)
    print(response.text)
    
    

    The data can be retrieved with the get method:

    import requests
    api_url = "http://localhost:8000/extra/8701"
    response = requests.get(api_url)
    response.json()
    print(response.text)
    print(response.status_code)
    
    

    The response.text contains the data:

    Finally, we may delete this entry by:
    import requests
    import json
    
    url = 'http://localhost:8000/extra/8701'
    headers = {'Content-Type': 'application/json; charset=UTF-8'}
    response = requests.delete(url, headers=headers)
    print(response.status_code) # 200
    print(response.ok) # True
  • Reading a CSV file and translate into dataframe

    Below, a script is provided that reads a CSV file and translate the outcome into a dataframe:

    import csv
    import pandas as pd
    
    with open(r"C:\Users\tomva\SynologyDrive\python\pandas\Incomplete\banking.csv","r") as csv_file:
        csv_reader = csv.reader(csv_file, delimiter = ',')
        print(type(csv_reader))
        df = pd.DataFrame()  
        i = 0
        for row in csv_reader:
            if i == 0:
                cols = row
            if i > 0:
                for j in range(len(row)):
                    df.loc[i, j] = row[j]
            if i%1000 == 0:
                print(i)
            i = i + 1
        print(cols)
        df = df.rename(columns={0: 'age', 1: 'job', 2: 'marital', 3: 'education', 4: 'default', 5: 'housing', 6: 'loan', 7: 'contact', 8: 'month', 9: 'day_of_week', 10: 'duration', 11: 'campaign', 12: 'pdays', 13: 'previous', 14: 'poutcome', 15: 'emp_var_rate', 16: 'cons_price_idx', 17: 'cons_conf_idx', 18: 'euribor3m', 19: 'nr_employed', 20: 'y'})    
        df.info()
    print('********* Klaar ********************')
    
  • Plotting in pandas

    Below, you see a snippet on how to create a plot in Pandas.

    import pandas as pd
    
    data = pd.read_csv(r"C:\Users\tomva\SynologyDrive\python\pandas\Incomplete\banking.csv", header = 0)
    
    deel = data.sample(frac =.005) 
    ax1 = deel.plot.scatter(x='age',
                          y='duration',
                          c='DarkBlue')
    

  • Logistic regression with Pandas

    Below, you see a logistic regression with Panda

    import pandas as pd
    import numpy as np
    from sklearn import preprocessing
    import matplotlib.pyplot as plt 
    plt.rc("font", size=14)
    from sklearn.linear_model import LogisticRegression
    from sklearn.model_selection import train_test_split
    import seaborn as sns
    sns.set(style="white")
    sns.set(style="whitegrid", color_codes=True)
    
    data = pd.read_csv(r"C:\Users\tomva\SynologyDrive\python\pandas\Incomplete\banking.csv", header = 0)
    data['education']=np.where(data['education'] =='basic.9y', 'Basic', data['education'])
    data['education']=np.where(data['education'] =='basic.6y', 'Basic', data['education'])
    data['education']=np.where(data['education'] =='basic.4y', 'Basic', data['education'])
    print(data['education'].unique())
    
    print(data['y'].value_counts())
    
    cat_vars=['education']
    for var in cat_vars:
        cat_list='var'+'_'+var
        cat_list = pd.get_dummies(data[var], prefix=var)
        data1=data.join(cat_list)
        data=data1
    
    cols=['education_Basic', 'education_high.school', 'education_professional.course', 'education_university.degree'] 
    X=data[cols]
    y=data['y']
    
    import statsmodels.api as sm
    logit_model=sm.Logit(y,X)
    result=logit_model.fit()
    print(result.summary2())
    
    print(data.groupby('education_Basic')["y"].mean())
    print(data.groupby('education_high.school')["y"].mean())
    print(data.groupby('education_professional.course')["y"].mean())
    print(data.groupby('education_university.degree')["y"].mean())
    
  • Crosstabs in Python

    Find below a Python programme that shows a crosstab whereby continuous variables are translated into classes:

    import pandas as pd
    import sqlalchemy as sa
    import matplotlib.pyplot as plt
    import numpy as np
    
    table_name = 'MIGRATED_DIVORCE_SETTLEMENT__C'
    
    connection_string = "DRIVER={ODBC Driver 17 for SQL Server};SERVER=DESKTOP-8J58OIP\MSSQLSERVER_19;DATABASE=Speel;UID=sa;PWD=**"
    connection_url = sa.engine.URL.create("mssql+pyodbc", query={"odbc_connect": connection_string})
    engine = sa.engine.create_engine(connection_url)
    with engine.begin() as conn:
        df = pd.read_sql_query(sa.text("SELECT * FROM Rapportage." + table_name), conn)
    
    onderzoek = df[['Person_ID_1__c','Person_ID_2__c']].apply(pd.to_numeric).dropna()
    onderzoek['Person_ID_1__c_afgerond'] = (round(onderzoek['Person_ID_1__c'] / 2500000)*2500000)
    onderzoek['Person_ID_2__c_afgerond'] = (round(onderzoek['Person_ID_2__c'] / 2500000)*2500000)
    
    ct = pd.crosstab(onderzoek['Person_ID_1__c_afgerond'], onderzoek['Person_ID_2__c_afgerond']).to_excel(r"C:\Users\tomva\SynologyDrive\python\pandas\Incomplete\output.xlsx")  
    
    print('**********programma beindigd****')
    

    The output looks like:

  • Univariate descriptive statistics in python

    Below, a programme is given on the univariate statistics.

    import pandas as pd
    import sqlalchemy as sa
    import matplotlib.pyplot as plt
    import numpy as np
    
    table_name = 'MIGRATED_DIVORCE_SETTLEMENT__C'
    
    connection_string = "DRIVER={ODBC Driver 17 for SQL Server};SERVER=DESKTOP-8J58OIP\MSSQLSERVER_19;DATABASE=Speel;UID=sa;PWD=AAaa11!!"
    connection_url = sa.engine.URL.create("mssql+pyodbc", query={"odbc_connect": connection_string})
    engine = sa.engine.create_engine(connection_url)
    with engine.begin() as conn:
        df = pd.read_sql_query(sa.text("SELECT * FROM Rapportage." + table_name), conn)
    
    onderzoek = df[['Person_ID_1__c','Person_ID_2__c']].apply(pd.to_numeric).dropna()
    print(onderzoek.info())
    print(onderzoek.head())
    
    print(onderzoek.describe())
    
    onderzoek.boxplot(column=['Person_ID_1__c','Person_ID_2__c'], grid = False)
    
    print('**********programma beindigd****')
    
    
  • Descriptive statistics in python

    Below, a programme is provided that shows the point cloud with a line that is the best fit to it.

    import pandas as pd
    import sqlalchemy as sa
    import matplotlib.pyplot as plt
    import numpy as np
    
    table_name = 'MIGRATED_DIVORCE_SETTLEMENT__C'
    
    connection_string = "DRIVER={ODBC Driver 17 for SQL Server};SERVER=DESKTOP-8J58OIP\MSSQLSERVER_19;DATABASE=Speel;UID=sa;PWD=**"
    connection_url = sa.engine.URL.create("mssql+pyodbc", query={"odbc_connect": connection_string})
    engine = sa.engine.create_engine(connection_url)
    with engine.begin() as conn:
        df = pd.read_sql_query(sa.text("SELECT * FROM Rapportage." + table_name), conn)
    
    onderzoek = df[['Person_ID_1__c','Person_ID_2__c']].apply(pd.to_numeric).dropna()
    print(onderzoek.info())
    print(onderzoek.head())
    
    y = onderzoek['Person_ID_1__c']
    x = onderzoek['Person_ID_2__c']
    
    #add points to plot
    plt.scatter(onderzoek['Person_ID_2__c'], onderzoek['Person_ID_1__c'], color='purple')
    a, b = np.polyfit(onderzoek['Person_ID_2__c'], onderzoek['Person_ID_1__c'], 1)
    plt.plot(onderzoek['Person_ID_2__c'], a*onderzoek['Person_ID_2__c']+b)
    plt.text(1, 90, 'y = ' + '{:.3f}'.format(b) + ' + {:.3f}'.format(a) + 'x', size=12)
    
    print('**********programma beindigd****')
  • Ordinary Least Squares in Pandas

    Below, I read a table and I apply the ordinary least squares methode to it:

    import pandas as pd
    from sqlalchemy.engine import create_engine
    from sqlalchemy.engine import URL
    import sqlalchemy as sa
    import statsmodels.api as sm
    
    table_name = 'MIGRATED_DIVORCE_SETTLEMENT__C'
    
    connection_string = "DRIVER={ODBC Driver 17 for SQL Server};SERVER=DESKTOP-8J58OIP\MSSQLSERVER_19;DATABASE=Speel;UID=sa;PWD=**"
    connection_url = URL.create("mssql+pyodbc", query={"odbc_connect": connection_string})
    engine = create_engine(connection_url)
    with engine.begin() as conn:
        df = pd.read_sql_query(sa.text("SELECT * FROM Rapportage." + table_name), conn)
    
    onderzoek = df[['Person_ID_1__c','Person_ID_2__c']].apply(pd.to_numeric).dropna()
    print(onderzoek.info())
    print(onderzoek.head())
    
    y = onderzoek['Person_ID_1__c']
    x = onderzoek['Person_ID_2__c']
    x = sm.add_constant(x)
    
    print(type(x))
    print(type(y))
    
    model = sm.OLS(y, x).fit()
    
    #view model summary
    print(model.summary())
    
    print('**********programma beindigd****')