https://github.com/khuyentran1401/strip_interactive
Strip and execute interactive Python string in a Python script
https://github.com/khuyentran1401/strip_interactive
python
Last synced: 3 months ago
JSON representation
Strip and execute interactive Python string in a Python script
- Host: GitHub
- URL: https://github.com/khuyentran1401/strip_interactive
- Owner: khuyentran1401
- Created: 2021-05-09T17:06:04.000Z (almost 4 years ago)
- Default Branch: master
- Last Pushed: 2021-06-06T15:47:29.000Z (over 3 years ago)
- Last Synced: 2024-11-20T03:27:00.982Z (3 months ago)
- Topics: python
- Language: Python
- Homepage:
- Size: 43 KB
- Stars: 6
- Watchers: 3
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Strip Interactive Python String
Have you ever come across an online tutorial that shows interactive Python code like this:
```python
>>> import numpy as np
>>> print(np.array([1,2,3]))
array([1, 2, 3])
```and wished to run only the inputs like below?
```python
import numpy as np
print(np.array([1,2,3]))
```That is when strip-interactive comes in handy.
## Usage
To use strip-interactive, simply add the code you want to run to `run_interactive` method.```python
from strip_interactive import run_interactivecode = """
>>> import numpy as np
>>> print(np.array([1,2,3]))
array([1, 2, 3])
"""outputs = run_interactive(code)
```Output:
```bash
[1 2 3]
```
You can also get the clean code (without inputs and `>>>`) using `get_clean_code` method.```python
from strip_interactive import get_clean_codecode = """
>>> import numpy as np
>>> print(np.array([1,2,3]))
array([1, 2, 3])
"""inputs = get_clean_code(code)
print(inputs)
```
Output:
```bash
import numpy as np
print(np.array([1,2,3]))
```
## Installation
```bash
pip install strip-interactive
```