mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2025-07-09 15:42:23 +03:00
Update make the search section
This commit is contained in:
parent
dd5eca9754
commit
d41442df1c
|
@ -633,7 +633,10 @@ and put next into it:
|
||||||
|
|
||||||
def search_repositories(self, term, limit):
|
def search_repositories(self, term, limit):
|
||||||
"""Search for repositories and return formatted data."""
|
"""Search for repositories and return formatted data."""
|
||||||
repositories = self._github_client.search_repositories(term, **{'in': 'name'})
|
repositories = self._github_client.search_repositories(
|
||||||
|
query=term,
|
||||||
|
**{'in': 'name'},
|
||||||
|
)
|
||||||
return [
|
return [
|
||||||
self._format_repo(repository)
|
self._format_repo(repository)
|
||||||
for repository in repositories[:limit]
|
for repository in repositories[:limit]
|
||||||
|
@ -703,12 +706,12 @@ Edit ``containers.py``:
|
||||||
Make the search
|
Make the search
|
||||||
---------------
|
---------------
|
||||||
|
|
||||||
Now we are ready to make the search work.
|
Now we are ready to make the search work. Let's use the ``SearchService`` in the ``index`` view.
|
||||||
|
|
||||||
Edit ``views.py``:
|
Edit ``views.py``:
|
||||||
|
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
:emphasize-lines: 5,8,11,16
|
:emphasize-lines: 5,8,10
|
||||||
|
|
||||||
"""Views module."""
|
"""Views module."""
|
||||||
|
|
||||||
|
@ -718,9 +721,8 @@ Edit ``views.py``:
|
||||||
|
|
||||||
|
|
||||||
def index(search_service: SearchService):
|
def index(search_service: SearchService):
|
||||||
search_term = request.args.get('search_term')
|
search_term = request.args.get('search_term', 'Dependency Injector')
|
||||||
|
repositories = search_service.search_repositories(search_term, limit=10)
|
||||||
repositories = search_service.search_repositories(search_term)
|
|
||||||
|
|
||||||
return render_template(
|
return render_template(
|
||||||
'index.html',
|
'index.html',
|
||||||
|
@ -728,6 +730,8 @@ Edit ``views.py``:
|
||||||
repositories=repositories,
|
repositories=repositories,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
Now let's inject the ``SearchService`` dependency into the ``index`` view.
|
||||||
|
|
||||||
Edit ``containers.py``:
|
Edit ``containers.py``:
|
||||||
|
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
|
@ -769,16 +773,11 @@ Edit ``containers.py``:
|
||||||
search_service=search_service,
|
search_service=search_service,
|
||||||
)
|
)
|
||||||
|
|
||||||
Edit ``config.yml``:
|
Make sure the app is running or use ``flask run`` and open ``http://127.0.0.1:5000/``.
|
||||||
|
|
||||||
.. code-block::
|
You should see:
|
||||||
:emphasize-lines: 3-5
|
|
||||||
|
|
||||||
github:
|
.. image:: flask_images/screen_02.png
|
||||||
request_timeout: 10
|
|
||||||
search:
|
|
||||||
default_term: "Dependency Injector"
|
|
||||||
default_limit: 5
|
|
||||||
|
|
||||||
Tests
|
Tests
|
||||||
-----
|
-----
|
||||||
|
|
BIN
docs/tutorials/flask_images/screen_02.png
Normal file
BIN
docs/tutorials/flask_images/screen_02.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 642 KiB |
|
@ -4,6 +4,8 @@ Flask Dependency Injection Example
|
||||||
Application ``githubnavigator`` is a `Flask <https://flask.palletsprojects.com/>`_ +
|
Application ``githubnavigator`` is a `Flask <https://flask.palletsprojects.com/>`_ +
|
||||||
`Dependency Injector <http://python-dependency-injector.ets-labs.org/>`_ application.
|
`Dependency Injector <http://python-dependency-injector.ets-labs.org/>`_ application.
|
||||||
|
|
||||||
|
.. image:: screenshot.png
|
||||||
|
|
||||||
Run
|
Run
|
||||||
---
|
---
|
||||||
|
|
||||||
|
|
|
@ -2,4 +2,4 @@ github:
|
||||||
request_timeout: 10
|
request_timeout: 10
|
||||||
search:
|
search:
|
||||||
default_term: "Dependency Injector"
|
default_term: "Dependency Injector"
|
||||||
default_limit: 5
|
default_limit: 10
|
||||||
|
|
|
@ -13,7 +13,10 @@ class SearchService:
|
||||||
|
|
||||||
def search_repositories(self, term, limit):
|
def search_repositories(self, term, limit):
|
||||||
"""Search for repositories and return formatted data."""
|
"""Search for repositories and return formatted data."""
|
||||||
repositories = self._github_client.search_repositories(term, **{'in': 'name'})
|
repositories = self._github_client.search_repositories(
|
||||||
|
query=term,
|
||||||
|
**{'in': 'name'},
|
||||||
|
)
|
||||||
return [
|
return [
|
||||||
self._format_repo(repository)
|
self._format_repo(repository)
|
||||||
for repository in repositories[:limit]
|
for repository in repositories[:limit]
|
||||||
|
|
BIN
examples/miniapps/ghnav-flask/screenshot.png
Normal file
BIN
examples/miniapps/ghnav-flask/screenshot.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 642 KiB |
Loading…
Reference in New Issue
Block a user