Skip to content

Update kernel.py #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ dist/
MANIFEST

\.vscode/
env
11 changes: 7 additions & 4 deletions postgres_kernel/kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@


def log(val):
return # comment out line for debug
return # comment out line for debug
with open('kernel.log', 'a') as f:
f.write(str(val) + '\n')
return val
Expand Down Expand Up @@ -57,7 +57,8 @@ def __init__(self, **kwargs):
self._conn_string = os.getenv('DATABASE_URL', '')
self._autocommit = True
self._conn = None
self._start_connection()
if self._conn_string:
self._start_connection()

@property
def language_version(self):
Expand Down Expand Up @@ -154,8 +155,10 @@ def change_autocommit_mode(self, switch):

def do_execute(self, code, silent, store_history=True,
user_expressions=None, allow_stdin=False):
if code.strip() in ['quit', 'quit()', 'exit', 'exit()']:
# self.do_shutdown(True)
return {'status': 'ok', 'execution_count': self.execution_count, 'payload': [{"source": "ask_exit"}]}
print(code)

connection_string = self.CONN_STRING_COMMENT.findall(code)
autocommit_switch = self.AUTOCOMMIT_SWITCH_COMMENT.findall(code)
if autocommit_switch:
Expand Down Expand Up @@ -204,7 +207,7 @@ def do_execute(self, code, silent, store_history=True,
self.iopub_socket, 'stream', {
'name': 'stdout',
'text': str(notice)
})
})
self._conn.notices = []

if header is not None and len(rows) > 0:
Expand Down
17 changes: 17 additions & 0 deletions 补充说明.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# 补充说明

本项目为[bgschiller/postgres_kernel](https://github.com/bgschiller/postgres_kernel)的fork,解决了不先设置环境变量`DATABASE_URL`就无法使用的问题,以及jupyter console中无法通过quit或者exit退出的问题.

本项目已经提交了pull-request,但介于原项目已经3年多没更新了估计也不会被merge了.需要使用jupyter连pg的朋友可以有两种途径

1. 直接安装本项目
```bash
pip install git+https://github.com/Basic-Components/postgres_kernel.git
```
2. 安装我的另一个项目[postgresql_kernel](https://github.com/Python-Tools/postgresql_kernel)

```bash
pip install postgresql_kernel
```

这个项目使用[metakernel](https://github.com/Calysto/metakernel)构造,目前已经完全实现了postgres_kernel的功能,而且用法一样.后续我则会进一步扩展这个kernel以实现一些自己的需求.