- 전체
- 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/
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.
댓글 0
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 수 |
|---|---|---|---|---|
| 10 |
[Kivy (python)] Kivy에서 Hello World
| 졸리운_곰 | 2024.12.20 | 358 |
| 9 |
[Kivy (python)] Kivy 시작하기 1 (1.11.1 버전 - 설치하기)
| 졸리운_곰 | 2024.12.20 | 401 |
| 8 | [Kivy (python)] Kivy 앱 배포: Android APK 생성하기 | 졸리운_곰 | 2024.12.20 | 347 |
| 7 |
[kivy (python)] [Python] 파이썬 키비(kivy) 앱 개발 - Screen Manager
| 졸리운_곰 | 2024.12.20 | 509 |
| 6 |
[kivy (python)] [Python] 파이썬 키비(kivy) 앱 개발 - 머티리얼 디자인, KivyMD
| 졸리운_곰 | 2024.12.20 | 392 |
| 5 |
[kivy (python)] [Python] 파이썬 키비(kivy) 앱 개발 - 레이아웃 개요와 입력창
| 졸리운_곰 | 2024.12.20 | 495 |
| 4 |
[kivy (python)] [Python] 파이썬 키비(Kivy) buildozer로 APK 앱 빌드 in Linux
| 졸리운_곰 | 2024.12.20 | 383 |
| 3 |
[kivy (python)] [Python] 파이썬 키비(kivy) 앱 개발 - 설치와 예제 앱 분석
| 졸리운_곰 | 2024.12.20 | 427 |
| 2 |
[kivy (python)] [Python] 파이썬 키비(kivy) 앱 개발 - kv 파일과 한글 폰트
| 졸리운_곰 | 2024.12.20 | 385 |
| 1 |
[kivy (python)] <재정리> 파이썬 키비(Kivy) buildozer로 APK 앱 완벽 변환하기 in Linux Ubuntu, 변환 환경 제공!
| 졸리운_곰 | 2024.12.20 | 598 |

