Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

api in python

import requests
import json

r = requests.get("URL")
j=r.json()
print(j)
Comment

create a python api

import flask
from flask import request, jsonify

app = flask.Flask(__name__)
app.config["DEBUG"] = True

# Create some test data for our catalog in the form of a list of dictionaries.
books = [
    {'id': 0,
     'title': 'A Fire Upon the Deep',
     'author': 'Vernor Vinge',
     'first_sentence': 'The coldsleep itself was dreamless.',
     'year_published': '1992'},
    {'id': 1,
     'title': 'The Ones Who Walk Away From Omelas',
     'author': 'Ursula K. Le Guin',
     'first_sentence': 'With a clamor of bells that set the swallows soaring, the Festival of Summer came to the city Omelas, bright-towered by the sea.',
     'published': '1973'},
    {'id': 2,
     'title': 'Dhalgren',
     'author': 'Samuel R. Delany',
     'first_sentence': 'to wound the autumnal city.',
     'published': '1975'}
]


@app.route('/', methods=['GET'])
def home():
    return '''<h1>Distant Reading Archive</h1>
<p>A prototype API for distant reading of science fiction novels.</p>'''


@app.route('/api/v1/resources/books/all', methods=['GET'])
def api_all():
    return jsonify(books)


@app.route('/api/v1/resources/books', methods=['GET'])
def api_id():
    # Check if an ID was provided as part of the URL.
    # If ID is provided, assign it to a variable.
    # If no ID is provided, display an error in the browser.
    if 'id' in request.args:
        id = int(request.args['id'])
    else:
        return "Error: No id field provided. Please specify an id."

    # Create an empty list for our results
    results = []

    # Loop through the data and match results that fit the requested ID.
    # IDs are unique, but other fields might return many results
    for book in books:
        if book['id'] == id:
            results.append(book)

    # Use the jsonify function from Flask to convert our list of
    # Python dictionaries to the JSON format.
    return jsonify(results)

app.run()
Comment

PREVIOUS NEXT
Code Example
Python :: calculate pointbiseral correlation scipy 
Python :: install google cloud python 
Python :: python get last element of array 
Python :: how to hide ticks in python 
Python :: check type of variable in python 
Python :: log log grid python 
Python :: python image layers 
Python :: python prime number sum 
Python :: python check for alphanumeric characters 
Python :: logarithms python 
Python :: random seed generator minecraft 
Python :: lemmatization 
Python :: calculate quartil python 
Python :: plt tickpad 
Python :: python processpoolexecutor 
Python :: python mongodb schema 
Python :: how to get pytroch model layer name 
Python :: random chars generator python 
Python :: multiple plot in one figure python 
Python :: python pandas how to select range of data 
Python :: how to parse timestamp in python 
Python :: python turtle shapes 
Python :: create new column with mask pandas 
Python :: Converting Dataframe from list Using a list in the dictionary 
Python :: python set match two list 
Python :: python int binary 
Python :: cmd to get ip address python 
Python :: TypeError: Direct assignment to the forward side of a many-to-many set is prohibited. Use .set() instead 
Python :: save pillow image to database django 
Python :: how to get timezone in python 
ADD CONTENT
Topic
Content
Source link
Name
8+1 =