Skip to content

Commit

Permalink
Updated facet graph rendering and fixed models
Browse files Browse the repository at this point in the history
  • Loading branch information
theorm committed Oct 22, 2024
1 parent 8b4cd1c commit 174887a
Show file tree
Hide file tree
Showing 8 changed files with 2,439 additions and 1,796 deletions.
128 changes: 62 additions & 66 deletions examples/notebooks/basic.ipynb

Large diffs are not rendered by default.

366 changes: 326 additions & 40 deletions examples/notebooks/entities.ipynb

Large diffs are not rendered by default.

2,783 changes: 1,465 additions & 1,318 deletions examples/notebooks/search.ipynb

Large diffs are not rendered by default.

880 changes: 536 additions & 344 deletions examples/notebooks/textReuse.ipynb

Large diffs are not rendered by default.

16 changes: 13 additions & 3 deletions impresso/data_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,15 @@ def _repr_html_(self):
)
preview_img = self._get_preview_image_()

grid_template_style = (
"grid-template-columns: minmax(200px, 1fr) auto;"
if preview_img is not None
else ""
)

items = [
f'<div style="display: grid; {grid_template_style}">',
"<div>",
f"<h2>{response_type} result</h2>",
f"<div>Contains <b>{self.size}</b> items "
+ (
Expand All @@ -43,13 +51,15 @@ def _repr_html_(self):
if self.url
else None
),
"<h3>Data preview:</h3>",
df_repr,
"</div>",
(
f'<img src="data:image/png;base64,{preview_img}" style="width:100% !important;">'
f'<div style="align-content: center;"><img src="data:image/png;base64,{preview_img}" style="max-width: 800px; width: 100%;"></div>'
if preview_img
else None
),
"</div>",
"<h3>Data preview:</h3>",
df_repr,
]

return "\n".join([item for item in items if item])
Expand Down
8 changes: 4 additions & 4 deletions impresso/resources/content_items.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
FindContentItemResolveLiteral,
)
from impresso.api_client.types import UNSET, Unset
from impresso.api_models import Article, BaseFind
from impresso.api_models import ContentItem, BaseFind
from impresso.data_container import DataContainer
from impresso.resources.base import Resource
from impresso.util.error import raise_for_error
Expand All @@ -22,7 +22,7 @@
class ContentItemsResponseSchema(BaseFind):
"""Schema for the content items response."""

data: list[Article]
data: list[ContentItem]


class ContentItemsDataContainer(DataContainer):
Expand All @@ -43,7 +43,7 @@ def raw(self) -> dict[str, Any]:
return self._data.to_dict()

@property
def pydantic(self) -> Article:
def pydantic(self) -> ContentItem:
"""Return the data as a pydantic model."""
return self._pydantic_model.model_validate(self.raw)

Expand Down Expand Up @@ -98,6 +98,6 @@ def get(self, id: str):

return ContentItemDataContainer(
result,
Article,
ContentItem,
f"{self._get_web_app_base_url()}/issue/{issue_id}/view?articleId={article_id}",
)
52 changes: 32 additions & 20 deletions impresso/resources/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
SearchOrderByLiteral,
)
from impresso.api_client.types import UNSET, Unset
from impresso.api_models import Article, BaseFind, Filter, Q, SearchFacet
from impresso.api_models import ContentItem, BaseFind, Filter, Q, SearchFacet
from impresso.data_container import IT, DataContainer, T
from impresso.resources.base import DEFAULT_PAGE_SIZE, Resource
from impresso.structures import AND, OR, DateRange
Expand All @@ -34,9 +34,9 @@


class SearchResponseSchema(BaseFind):
"""Schema for the articles response."""
"""Schema for the content items response."""

data: list[Article]
data: list[ContentItem]


class SearchDataContainer(DataContainer):
Expand Down Expand Up @@ -112,7 +112,7 @@ def _get_preview_image_(self) -> str | None:


class SearchResource(Resource):
"""Search articles in the impresso database."""
"""Search content items in the impresso database."""

name = "search"

Expand Down Expand Up @@ -140,27 +140,27 @@ def find(
text_reuse_cluster_id: str | OR[str] | None = None,
) -> SearchDataContainer:
"""
Search for articles in Impresso.
Search for content items in Impresso.
Args:
q: Search term.
order_by: Order by aspect.
limit: Number of results to return.
offset: Number of results to skip.
with_text_contents: Return only articles with text contents.
title: Return only articles that have this term or all/any of the terms in the title.
front_page: Return only articles that were on the front page.
entity_id: Return only articles that mention this entity or all/any of the entities.
date_range: Return only articles that were published in this date range.
language: Return only articles that are in this language or all/any of the languages.
mention: Return only articles that mention an entity with this term or all/any of entities with the terms.
topic_id: Return only articles that are about this topic or all/any of the topics.
collection_id: Return only articles that are in this collection or all/any of the collections.
country: Return only articles that are from this country or all/any of the countries.
access_rights: Return only articles with this access right or all/any of the access rights.
partner_id: Return only articles that are from this partner or all/any of the partners.
text_reuse_cluster_id: Return only articles that are in this text reuse cluster or all/any of the clusters.
with_text_contents: Return only content items with text contents.
title: Return only content items that have this term or all/any of the terms in the title.
front_page: Return only content items that were on the front page.
entity_id: Return only content items that mention this entity or all/any of the entities.
date_range: Return only content items that were published in this date range.
language: Return only content items that are in this language or all/any of the languages.
mention: Return only content items that mention an entity with this term or all/any of entities with the terms.
topic_id: Return only content items that are about this topic or all/any of the topics.
collection_id: Return only content items that are in this collection or all/any of the collections.
country: Return only content items that are from this country or all/any of the countries.
access_rights: Return only content items with this access right or all/any of the access rights.
partner_id: Return only content items that are from this partner or all/any of the partners.
text_reuse_cluster_id: Return only content items that are in this text reuse cluster or all/any of the clusters.
Returns:
_type_: _description_
Expand Down Expand Up @@ -398,10 +398,22 @@ def render_dataframe_chart_base64(x, y) -> str:
else:
plt.plot(x, y)

# Remove axes, labels, and legend
plt.axis("off")
# Remove labels, and legend
# plt.legend().remove()

# Either
# Remove axes,
plt.axis("off")

# OR
# Remove Y axis and only show the first and last tick
# plt.gca().get_yaxis().set_visible(False)
# plt.xticks([x[0], x[-1]])

# Remove the box around the graph
for spine in plt.gca().spines.values():
spine.set_visible(False)

# Save the plot to a bytes buffer with a transparent background
buffer = io.BytesIO()
plt.savefig(
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ packages = [
]
readme = "README.md"
repository = "https://github.com/impresso/impresso-py"
version = "0.9.6"
version = "0.9.7"

[tool.poetry.urls]
Endpoint = "https://impresso-project.ch/public-api"
Expand Down

0 comments on commit 174887a

Please sign in to comment.