Repository: ei-grad/flask-shell-ipython Branch: main Commit: 9848a16baa2c Files: 9 Total size: 14.6 KB Directory structure: gitextract_7tzqi5c5/ ├── .github/ │ └── workflows/ │ └── release.yml ├── .gitignore ├── CHANGELOG.md ├── LICENSE ├── README.md ├── flask_shell_ipython.py ├── pyproject.toml ├── requirements-test.txt └── test_flask_shell_ipython.py ================================================ FILE CONTENTS ================================================ ================================================ FILE: .github/workflows/release.yml ================================================ name: Release on: push: pull_request: jobs: build: name: Build runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - uses: actions/setup-python@v5 - name: Install build run: pip install build - name: Build distribution run: python -m build . - name: File list run: tar tf dist/*.tar.gz && unzip -l dist/*.whl - name: Upload artifacts uses: actions/upload-artifact@v4 with: name: dist path: dist/ test: name: Run tests needs: build strategy: matrix: python-version: ["3.8", "3.9", "3.10", "3.11", "3.12", "3.13.0-rc.1"] os: [ubuntu-latest, macos-13, macos-latest, windows-latest] runs-on: ${{ matrix.os }} steps: - uses: actions/checkout@v4 - uses: actions/setup-python@v5 with: python-version: ${{ matrix.python-version }} - name: Download artifacts uses: actions/download-artifact@v4 - name: Install wheel shell: bash run: pip install dist/*.whl - name: Make it use package from wheel shell: bash run: rm flask_shell_ipython.py - name: Install test requirements run: pip install -r requirements-test.txt - name: Test if: runner.os != 'Windows' run: pytest --forked - name: Test (Windows) if: runner.os == 'Windows' shell: bash run: pytest --collect-only -q | grep ^test_ | while read testname; do pytest -q $testname; done pypi-publish-test: name: Release on Test PyPI needs: test runs-on: ubuntu-latest environment: name: Test PyPI url: "https://test.pypi.org/project/flask-shell-ipython/" permissions: id-token: write steps: - name: Download artifacts uses: actions/download-artifact@v4 - name: Publish package distributions to PyPI uses: pypa/gh-action-pypi-publish@release/v1 with: repository-url: "https://test.pypi.org/legacy/" github-release: name: Release on GitHub needs: test if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v') runs-on: ubuntu-latest environment: name: GitHub url: "https://github.com/ei-grad/flask-shell-ipython/releases/" permissions: attestations: write contents: write id-token: write steps: - uses: actions/checkout@v4 - name: Extract Version from Tag run: echo "VERSION_FROM_GIT_REF=${GITHUB_REF#refs/tags/v}" >> $GITHUB_ENV - name: Extract Version from pyproject.toml run: | pip install toml VERSION_FROM_PYPROJECT=$(python << EOF import toml print(toml.load('pyproject.toml')['project']['version']) EOF ) echo "VERSION_FROM_PYPROJECT=$VERSION_FROM_PYPROJECT" >> $GITHUB_ENV - name: Ensure version consistency run: | if [ "$VERSION_FROM_GIT_REF" != "$VERSION_FROM_PYPROJECT" ]; then echo "Error: Version from tag ($VERSION_FROM_GIT_REF) does not match version in pyproject.toml ($VERSION_FROM_PYPROJECT)" exit 1 fi echo VERSION=$VERSION_FROM_GIT_REF >> $GITHUB_ENV - name: Extract changelog for release notes run: | ( echo "CHANGELOG<> $GITHUB_ENV - name: Validate changelog content run: | if [ -z "$CHANGELOG" ] ; then echo "Missing CHANGELOG.md section for release $VERSION" exit 1 fi - name: Download artifacts uses: actions/download-artifact@v4 - name: Attest build provenance uses: actions/attest-build-provenance@v1 with: subject-path: dist/* - name: Create GitHub Release env: GITHUB_TOKEN: ${{ github.token }} run: >- gh release create "$VERSION" --draft --notes "$CHANGELOG" - name: Upload artifact signatures to GitHub Release env: GITHUB_TOKEN: ${{ github.token }} run: >- gh release upload "$VERSION" dist/** pypi-publish: name: Release on PyPI needs: github-release if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v') runs-on: ubuntu-latest environment: name: PyPI url: "https://pypi.org/project/flask-shell-ipython/" permissions: id-token: write steps: - name: Download artifacts uses: actions/download-artifact@v4 - name: Publish package distributions to PyPI uses: pypa/gh-action-pypi-publish@release/v1 ================================================ FILE: .gitignore ================================================ # Byte-compiled / optimized / DLL files __pycache__/ *.py[cod] *$py.class # C extensions *.so # Distribution / packaging .Python env/ build/ develop-eggs/ dist/ downloads/ eggs/ .eggs/ lib/ lib64/ parts/ sdist/ var/ *.egg-info/ .installed.cfg *.egg # PyInstaller # Usually these files are written by a python script from a template # before PyInstaller builds the exe, so as to inject date/other infos into it. *.manifest *.spec # Installer logs pip-log.txt pip-delete-this-directory.txt # Unit test / coverage reports htmlcov/ .tox/ .coverage .coverage.* .cache nosetests.xml coverage.xml *,cover .hypothesis/ # Translations *.mo *.pot # Django stuff: *.log local_settings.py # Flask stuff: instance/ .webassets-cache # Scrapy stuff: .scrapy # Sphinx documentation docs/_build/ # PyBuilder target/ # IPython Notebook .ipynb_checkpoints # pyenv .python-version # celery beat schedule file celerybeat-schedule # dotenv .env # virtualenv venv/ ENV/ # Spyder project settings .spyderproject # Rope project settings .ropeproject flask_shell_ipython-*/ ================================================ FILE: CHANGELOG.md ================================================ # Changelog All notable changes to this project will be documented in this file. ## [0.5.4] - 2024-09-10 ### Packaging - Check consistency between tag version and `pyproject.toml` version. - Added extraction of changelog content for release notes from `CHANGELOG.md`. - Switched to `actions/attest-build-provenance@v1` from `gh-action-sigstore-python`. ## [0.5.3] - 2024-09-06 ### Packaging - Updated `gh-action-sigstore-python` version to `v3.0.0` in GitHub Actions workflow. ## [0.5.2] - 2024-09-05 ### Packaging - Migrated to `pyproject.toml` and Hatchling build system. - Added Python 3.12 and 3.13.0-rc.1 to the test matrix. - Updated CI workflows and actions to the latest versions. - Enabled attestations for publishing. - Added publishing to Test PyPI and GitHub releases. - Dropped support for Python 3.6 and 3.7 due to end-of-life. ## [0.5.1] - 2023-04-27 ### Added - Reverted to original pre-2018 banner format in the IPython shell. - Minor fixes for banner formatting. ## [0.5.0] - 2023-04-27 ### Added - Implemented a new release format and workflow. - Drop Python 2.x support. - Updated dependencies in `setup.py`. ## [0.4.1] - 2022-05-06 ### Added - Universal wheel support added in `setup.cfg`. - Packaging improvements. ## [0.4.0] - 2019-12-06 ### Fixed - Improved consistency with built-in Flask shell banner format. - Replaced deprecated `_app_ctx_stack` with `current_app`. ## [0.3.1] - 2018-08-31 ### Fixed - Grammar corrections in docstrings. - Fixed minor issues with Python 3 compatibility. ## [0.3.0] - 2017-07-25 ### Changed - Migrated to `start_ipython()` for a fully functional shell instead of using an embedded IPython instance. - Added support for passing CLI arguments to IPython. ## [0.2.2] - 2016-12-06 ### Changed - Updated IPython version to `>=5.0.0` in dependencies. - Improved banner format to include IPython version. ## [0.2.1] - 2016-07-08 ### Removed - Reverted support for `PYTHONSTARTUP` script handling. ## [0.2.0] - 2016-07-08 ### Added - Added support for IPython `>=5.0.0`. - Introduced a more consistent banner format for the shell. ## [0.1.1] - 2016-06-20 ### Fixed - Improved the help command text. - Added proper package metadata in `setup.py`. ## [0.1.0] - 2016-06-20 ### Added - Initial release of `flask-shell-ipython`. - Replaces the default Flask shell command with IPython. ================================================ FILE: LICENSE ================================================ MIT License Copyright (c) 2016 Andrew Grigorev Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ================================================ FILE: README.md ================================================ # Flask-Shell-IPython `flask-shell-ipython` is a Python package that replaces the default `flask shell` command with a similar command that runs IPython. This provides an enhanced interactive Python shell with additional features like syntax highlighting, tab-completion, and more. ## Installation To install `flask-shell-ipython`, simply run: ```bash pip install flask-shell-ipython ``` ## Usage After installing `flask-shell-ipython`, the `flask shell` command will automatically use IPython instead of the default Python shell. There are no additional steps required. ```bash flask shell ``` You can also pass any valid IPython arguments after the `flask shell` command: ```bash flask shell --no-banner -i foo.py ``` ## Configuration You can configure IPython settings by adding an `IPYTHON_CONFIG` key to your Flask app's configuration. The value should be a dictionary containing the configuration options you'd like to set. For example: ```python app.config['IPYTHON_CONFIG'] = { 'InteractiveShell': { 'colors': 'Linux', 'confirm_exit': False, }, } ``` ## Testing To run tests for `flask-shell-ipython`, install the `pytest-forked` plugin, which enables running tests in isolated forked subprocesses to ensure running a clean IPython instance for each test case. ### Installing Dependencies Install testing dependencies from `requirements-test.txt`: ```bash pip install -r requirements-test.txt ``` ### Running Tests After installing the dependencies, run the test suite with the `--forked` option: ```bash pytest --forked ``` Please, note that does pytest-forked does not work on Windows. To test flask-shell-ipython on Windows run each test manually. ## License `flask-shell-ipython` is licensed under the MIT License. See the [LICENSE](LICENSE) file for more information. ## Contributing If you'd like to contribute to the project, feel free to submit a pull request on the GitHub repository at http://github.com/ei-grad/flask-shell-ipython. ================================================ FILE: flask_shell_ipython.py ================================================ import sys import click from flask.cli import with_appcontext @click.command(context_settings=dict(ignore_unknown_options=True)) @click.argument("ipython_args", nargs=-1, type=click.UNPROCESSED) @with_appcontext def shell(ipython_args): """Runs a shell in the app context. Runs an interactive Python shell in the context of a given Flask application. The application will populate the default namespace of this shell according to its configuration. This is useful for executing small snippets of management code without having to manually configure the application. """ import IPython from IPython.terminal.ipapp import load_default_config from traitlets.config.loader import Config from flask.globals import current_app as app if "IPYTHON_CONFIG" in app.config: config = Config(app.config["IPYTHON_CONFIG"]) else: config = load_default_config() config.TerminalInteractiveShell.banner1 = f"""Python {sys.version} on {sys.platform} IPython: {IPython.__version__} App: {app.import_name}{' [debug]' if app.debug else ''} Instance: {app.instance_path} """ IPython.start_ipython( argv=ipython_args, user_ns=app.make_shell_context(), config=config, ) ================================================ FILE: pyproject.toml ================================================ [build-system] requires = ["hatchling==1.25.0"] build-backend = "hatchling.build" [project] name = "flask-shell-ipython" version = "0.5.3" description = "Replace default `flask shell` command by similar command running IPython." readme = "README.md" readme-content-type = "text/markdown" requires-python = ">=3.8, <4.0" license = "MIT" # expect `license-files` field to break on any new version of hatch, because # PEP-639 is already changed from this structure as of the time of writing license-files = { paths = ["LICENSE"] } authors = [ { name = "Andrew Grigorev", email = "andrew@ei-grad.ru" } ] classifiers = [ "Development Status :: 5 - Production/Stable", "Environment :: Console", "Framework :: Flask", "Framework :: IPython", "Intended Audience :: Developers", "Programming Language :: Python :: 3", ] dependencies = [ "Flask>=1.0", "click", "IPython>=5.0.0" ] [project.urls] Homepage = "https://github.com/ei-grad/flask-shell-ipython" Changelog = "https://github.com/ei-grad/flask-shell-ipython/blob/main/CHANGELOG.md" [project.entry-points."flask.commands"] shell = "flask_shell_ipython:shell" [tool.hatch.build.targets.sdist] exclude = [ ".github", ] ================================================ FILE: requirements-test.txt ================================================ pytest pytest-forked; platform_system != "Windows" ================================================ FILE: test_flask_shell_ipython.py ================================================ import pytest from click.testing import CliRunner from flask import Flask from flask_shell_ipython import shell @pytest.fixture def app(): app = Flask(__name__) app.config["TESTING"] = True return app @pytest.fixture def runner(app): return CliRunner() def test_shell_command(runner, app): with app.app_context(): result = runner.invoke(shell) assert result.exit_code == 0 assert "IPython" in result.output def test_shell_command_no_banner(runner, app): with app.app_context(): result = runner.invoke(shell, ["--no-banner", "--no-confirm-exit"]) assert result.exit_code == 0 assert result.output == "\nIn [1]: " def test_shell_command_with_custom_config(runner, app): app.config["IPYTHON_CONFIG"] = { "InteractiveShell": {"confirm_exit": False}, "TerminalIPythonApp": {"display_banner": False} } with app.app_context(): result = runner.invoke(shell) assert result.exit_code == 0 assert result.output == "\nIn [1]: "