- 전체
- Python 일반
- Python 수학
- Python 그래픽
- Python 자료구조
- Python 인공지능
- Python 인터넷
- Python SAGE
- wxPython
- TkInter
- iPython
- wxPython
- pyQT
- Jython
- django
- flask
- blender python scripting
- python for minecraft
- Python 데이터 분석
- Python RPA
- cython
- PyCharm
- pySide
- kivy (python)
Python 인터넷 [python][jupyter notebook][JSON API] Building a JSON API Using Jupyter Notebooks in Under 5 Minutes
2021.03.28 17:32
[python][jupyter notebook][JSON API] Building a JSON API Using Jupyter Notebooks in Under 5 Minutes
In the post Making a Simple Database to Act as a Lookup for the ONS Register of Geographic Codes, I idly pondered creating a simple API for looking up ONS geographic codes.
Popping out to the shop for a pint of milk, I recalled one of the many things on my to do list was too look at the Jupyter Kernel Gateway, as described in the IBM Emerging Technologies blogpost on Jupyter Notebooks as RESTful Microservices.
The kernel_gateway project looks to be active – install it using something like pip3 install jupyter_kernel_gateway – and set up a notebook, such as simpleAPI.ipynb (the following code blocks represent separate code cells).
Import some helper packages and connect the geographic codes db I created in the previous post.
|
1
2
3
4
|
import jsonimport pandas as pdimport sqlite3con = sqlite3.connect("onsgeocodes.sqlite") |
Create a placeholder for the global REQUEST object that will be created when the API is invoked:
|
1
2
3
4
|
REQUEST = json.dumps({'path' : {},'args' : {}}) |
Now let’s define the API.
For example, suppose I want to return some information about a particular code:
|
1
2
3
4
5
6
|
# GET /ons/:coderequest = json.loads(REQUEST)code = request['path'].get('code')q='SELECT * FROM codelist WHERE "GEOGCD"="{code}"'.format(code=code)print('{"codes":%s}' % pd.read_sql_query(q, con).to_json(orient='records')) |
Or suppose I want to look up what current codes might exist that partially match a particular place name:
|
1
2
3
4
5
6
7
8
9
10
|
# GET /ons/current/:namerequest = json.loads(REQUEST)name = request['path'].get('name')q='''SELECT * FROM codelist JOIN metadataWHERE "GEOGNM"="{name}" AND codeAbbrv=sheet AND codelist.STATUS="live"'''.format(name=name)print('{"codes":%s}' % pd.read_sql_query(q, con).to_json(orient='records')) |
On the command line in the same directory as the notebook (for example, SimpleAPI.ipynb), I can now run the command:
jupyter kernelgateway --KernelGatewayApp.api='kernel_gateway.notebook_http' --KernelGatewayApp.seed_uri='./SimpleAPI.ipynb'
to start the server.
And as if by magic, an API appears:

The original blogpost also describes a simple docker container for running the API. Which makes me thing: this is so easy… And using something like LaunchBot, it’d be easy enough to have a simple manager for local API servers running on a personal desktop?
Here’s the complete notebook.
[출처] https://blog.ouseful.info/2017/09/06/building-a-json-api-using-jupyer-notebooks-in-under-5-minutes/
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.

