Skip to content

Commit c57a166

Browse files
committed
Add base.handlers.get_current_request()
This function allows to retrieve the current web request in async contexts. Retrieving the current context can be useful for example when customizing Enterprise Gateway RemoteProcessProxy, to retrieve authentication headers. The implementation makes uses of Python 3.7's new contextvars module. There are backports efforts for 3.6: - https://pypi.org/project/aiocontextvars/ - does not have automatic asyncio support - https://pypi.org/project/contextvars/ - which doesn't have it either. The tracking issue on this matter has been stale for 3 years now, and the conclusion of the discussion is "upgrade to a newer version of Python": MagicStack/contextvars#2 Since notebook has no control on the event loop it is running into, I preferred to drop Python 3.6 support.
1 parent a5ace13 commit c57a166

File tree

6 files changed

+23
-8
lines changed

6 files changed

+23
-8
lines changed

.github/workflows/docs.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jobs:
1111
fail-fast: false
1212
matrix:
1313
os: [ubuntu]
14-
python-version: [ '3.6' ]
14+
python-version: [ '3.7' ]
1515
steps:
1616
- name: Checkout
1717
uses: actions/checkout@v1

.github/workflows/python-nbconvert.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
strategy:
1717
fail-fast: false
1818
matrix:
19-
python-version: [ '3.6' , '3.7', '3.8', '3.9' ]
19+
python-version: [ '3.7', '3.8', '3.9' ]
2020
steps:
2121
- name: Checkout
2222
uses: actions/checkout@v1

.github/workflows/python.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jobs:
1111
fail-fast: false
1212
matrix:
1313
os: [ubuntu, macos, windows]
14-
python-version: [ '3.6' , '3.7', '3.8', '3.9' ] # Windows 3.9 fails due to the pywinpty dependency not working (Issue #5967)
14+
python-version: [ '3.7', '3.8', '3.9' ] # Windows 3.9 fails due to the pywinpty dependency not working (Issue #5967)
1515
steps:
1616
- name: Checkout
1717
uses: actions/checkout@v1

.github/workflows/selenium.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212
fail-fast: false
1313
matrix:
1414
os: [ubuntu, macos]
15-
python-version: [ '3.6', '3.7', '3.8', '3.9' ]
15+
python-version: [ '3.7', '3.8', '3.9' ]
1616
steps:
1717
- name: Checkout
1818
uses: actions/checkout@v2

notebook/base/handlers.py

+15
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import traceback
1515
import types
1616
import warnings
17+
import contextvars
1718
from http.client import responses
1819
from http.cookies import Morsel
1920

@@ -35,6 +36,10 @@
3536
from notebook.utils import is_hidden, url_path_join, url_is_absolute, url_escape, urldecode_unix_socket_path
3637
from notebook.services.security import csp_report_uri
3738

39+
40+
_current_request_var: contextvars.ContextVar = contextvars.ContextVar("current_request")
41+
42+
3843
#-----------------------------------------------------------------------------
3944
# Top-level handlers
4045
#-----------------------------------------------------------------------------
@@ -56,6 +61,9 @@ def log():
5661
class AuthenticatedHandler(web.RequestHandler):
5762
"""A RequestHandler with an authenticated user."""
5863

64+
def prepare(self):
65+
_current_request_var.set(self.request)
66+
5967
@property
6068
def content_security_policy(self):
6169
"""The default Content-Security-Policy header
@@ -932,6 +940,13 @@ def get(self):
932940
self.write(prometheus_client.generate_latest(prometheus_client.REGISTRY))
933941

934942

943+
def get_current_request():
944+
"""
945+
Get :class:`tornado.httputil.HTTPServerRequest` that is currently being processed.
946+
"""
947+
return _current_request_var.get(None)
948+
949+
935950
#-----------------------------------------------------------------------------
936951
# URL pattern fragments for re-use
937952
#-----------------------------------------------------------------------------

setup.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
name = "notebook"
1717

18-
if sys.version_info < (3, 6):
18+
if sys.version_info < (3, 7):
1919
pip_message = 'This may be due to an out of date pip. Make sure you have pip >= 9.0.1.'
2020
try:
2121
import pip
@@ -31,7 +31,8 @@
3131

3232

3333
error = """
34-
Notebook 6.3+ supports Python 3.6 and above.
34+
Notebook 6.4+ supports Python 3.7 and above.
35+
When using Python 3.6, please install Notebook < 6.4.
3536
When using Python 3.5, please install Notebook <= 6.2.
3637
When using Python 3.4 or earlier (including 2.7), please install Notebook 5.x.
3738
@@ -99,7 +100,6 @@
99100
'License :: OSI Approved :: BSD License',
100101
'Programming Language :: Python',
101102
'Programming Language :: Python :: 3',
102-
'Programming Language :: Python :: 3.6',
103103
'Programming Language :: Python :: 3.7',
104104
'Programming Language :: Python :: 3.8',
105105
'Programming Language :: Python :: 3.9'
@@ -131,7 +131,7 @@
131131
'test:sys_platform != "win32"': ['requests-unixsocket'],
132132
'json-logging': ['json-logging']
133133
},
134-
python_requires = '>=3.6',
134+
python_requires = '>=3.7',
135135
entry_points = {
136136
'console_scripts': [
137137
'jupyter-notebook = notebook.notebookapp:main',

0 commit comments

Comments
 (0)