Skip to content

Commit c8c29e4

Browse files
tillahoffmannSaurabh Kumar
authored andcommitted
Add ipython magic.
- Use cwd. - Remove whitespace. - Remove unused import. - Add load to __all__. - Add docs Thanks @tillahoffmann closes theskumar#34
1 parent 13a8aa5 commit c8c29e4

3 files changed

Lines changed: 46 additions & 4 deletions

File tree

README.rst

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
(__)|_______||__| \__| \__/
99

1010

11-
python-dotenv | |Build Status| |Coverage Status| |PyPI version| |PyPI|
11+
python-dotenv | |Build Status| |Coverage Status| |PyPI version| |PyPI|
1212
=================================
1313

1414
Reads the key,value pair from ``.env`` and adds them to environment
@@ -20,8 +20,8 @@ production using `12-factor <http://12factor.net/>`__ principles.
2020
- `Usages <#usages>`__
2121
- `Installation <#installation>`__
2222
- `Command-line interface <#command-line-interface>`__
23+
- `iPython Support <#ipython-support>`__
2324
- `Setting config on remote
24-
servers <#setting-config-on-remote-servers>`__
2525
- `Releated Projects <#releated-projects>`__
2626
- `Contributing <#contributing>`__
2727
- `Changelog <#changelog>`__
@@ -135,6 +135,21 @@ update your settings on remote server, handy isn't it!
135135
set Store the given key/value.
136136
unset Removes the given key.
137137

138+
iPython Support
139+
---------------
140+
141+
You can use dotenv with iPython. You can either let the dotenv search for .env with `%dotenv` or provide the path to .env file explicitly, see below for usuages.
142+
143+
```
144+
%load_ext dotenv
145+
146+
# Use find_dotenv to locate the file
147+
%dotenv
148+
149+
# Specify a particular file
150+
%dotenv relative/or/absolute/path/to/.env
151+
```
152+
138153
Setting config on remote servers
139154
--------------------------------
140155

@@ -231,7 +246,7 @@ Changelog
231246

232247
dev
233248
----------
234-
- ...
249+
- Add iPython Suport (@tillahoffmann)
235250

236251
0.6.0
237252
----------
@@ -253,6 +268,7 @@ dev
253268
- cli: Added ``-q/--quote`` option to control the behaviour of quotes around values in ``.env``. (Thanks `@hugochinchilla`_).
254269
- Improved test coverage.
255270

271+
.. _@tillahoffmann: https://github.com/tillahoffmann
256272
.. _@hugochinchilla: https://github.com/hugochinchilla
257273
.. _@isms: https://github.com/isms
258274
.. _@iameugenejo: https://github.com/iameugenejo

dotenv/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from .cli import get_cli_string
22
from .main import load_dotenv, get_key, set_key, unset_key, find_dotenv
3+
from .ipython import load_ipython_extension
34

4-
__all__ = ['get_cli_string', 'load_dotenv', 'get_key', 'set_key', 'unset_key', 'find_dotenv']
5+
__all__ = ['get_cli_string', 'load_dotenv', 'get_key', 'set_key', 'unset_key', 'find_dotenv', 'load_ipython_extension']

dotenv/ipython.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from __future__ import print_function
2+
from .main import load_dotenv, find_dotenv
3+
4+
5+
def _magic(dotenv_path):
6+
"""
7+
dotenv [dotenv_path]
8+
9+
Search in increasingly higher folders for the `dotenv_path`
10+
"""
11+
# Locate the .env file
12+
dotenv_path = dotenv_path or '.env'
13+
try:
14+
dotenv_path = find_dotenv(dotenv_path, True, True)
15+
except IOError:
16+
print("cannot find .env file")
17+
return
18+
19+
# Load the .env file
20+
load_dotenv(dotenv_path)
21+
22+
23+
def load_ipython_extension(ipython):
24+
"""Register the %dotenv magic."""
25+
ipython.register_magic_function(_magic, magic_name='dotenv')

0 commit comments

Comments
 (0)