Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New examples #229

Merged
merged 29 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
eb6b63b
Comments out Cost of reoffending example
danmilne1 Oct 4, 2023
1936bc5
Creates a minimal penguins example
danmilne1 Oct 4, 2023
27ff895
Creates an example showing how to write a workbook with a cover
danmilne1 Oct 4, 2023
deb4a6c
Creates an example showing how to do notes in gptables
danmilne1 Oct 4, 2023
3da569e
Creates an example demonstrating the use of the additional_formatting…
danmilne1 Oct 4, 2023
0614b17
Removes old iris examples
danmilne1 Oct 4, 2023
f0ed2f1
Changes docs to display new examples
danmilne1 Oct 4, 2023
5959027
Fixes example headers
danmilne1 Oct 4, 2023
e1c2442
Relaces iris theme example with penguins theme example
danmilne1 Oct 4, 2023
25a3016
Fixes theme example header
danmilne1 Oct 4, 2023
e3ddf08
Renames iris theme to penguins
danmilne1 Oct 4, 2023
6168853
Fixes typos in minimal example
danmilne1 Oct 16, 2023
45af7c0
Fixes typos in cover example
danmilne1 Oct 16, 2023
546ccfd
Rewrites header for notes example
danmilne1 Oct 16, 2023
8f7b2c6
Tidies up the theme example
danmilne1 Oct 16, 2023
0e315af
Fixes typos in additional_formatting example
danmilne1 Oct 16, 2023
b018a70
Fixes display lines for examples
danmilne1 Oct 16, 2023
959ca26
Adds index_columns to cover example
danmilne1 Oct 16, 2023
634c7f7
Provides source for penguins dataset
danmilne1 Jun 28, 2024
4139b13
Removes old examples
danmilne1 Jun 28, 2024
0b3c087
Removes multiple sheets example from cover example
danmilne1 Jun 28, 2024
4b84afe
Adds separate multiple sheets example
danmilne1 Jun 28, 2024
d1c1da6
Removes additional things from minimal example
danmilne1 Jun 28, 2024
1fea012
Replaces penguins theme with default theme
danmilne1 Jun 28, 2024
f0f06a5
Removes R example
danmilne1 Jun 28, 2024
04a9528
Adds new examples
danmilne1 Jun 28, 2024
ab680ea
Minor changes to multiple sheets example
danmilne1 Jul 2, 2024
d3fb932
Re-adds specific example theme
danmilne1 Jul 2, 2024
27ca98b
Fixes merge conflicts
danmilne1 Jul 3, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 23 additions & 14 deletions docs/source/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,41 @@ examples_ directory of the package.
.. todo:: Replace datasets in examples with open online datasets


.. automodule:: gptables.examples.iris
.. automodule:: gptables.examples.penguins_minimal

.. literalinclude:: ../../gptables/examples/iris.py
.. literalinclude:: ../../gptables/examples/penguins_minimal.py
:language: python
:lines: 15-

:lines: 16-

.. automodule:: gptables.examples.iris_cover
.. automodule:: gptables.examples.penguins_theme

.. literalinclude:: ../../gptables/examples/iris_cover.py
.. literalinclude:: ../../gptables/examples/penguins_theme.py
:language: python
:lines: 12-

:lines: 16-

.. automodule:: gptables.examples.iris_additional_formatting
.. automodule:: gptables.examples.penguins_cover

.. literalinclude:: ../../gptables/examples/iris_additional_formatting.py
.. literalinclude:: ../../gptables/examples/penguins_cover.py
:language: python
:lines: 24-
:lines: 14-

.. automodule:: gptables.examples.penguins_notes

.. automodule:: gptables.examples.cor_multiple_sheets
.. literalinclude:: ../../gptables/examples/penguins_notes.py
:language: python
:lines: 15-

.. automodule:: gptables.examples.penguins_additional_formatting

.. literalinclude:: ../../gptables/examples/cor_multiple_sheets.py
.. literalinclude:: ../../gptables/examples/penguins_additional_formatting.py
:language: python
:lines: 16-
:lines: 25-

.. .. automodule:: gptables.examples.cor_multiple_sheets

.. .. literalinclude:: ../../gptables/examples/cor_multiple_sheets.py
.. :language: python
.. :lines: 16-


R Usage
Expand Down
93 changes: 0 additions & 93 deletions gptables/examples/iris_cover.py

This file was deleted.

89 changes: 0 additions & 89 deletions gptables/examples/iris_personal_theme.py

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""
Iris - Additional Formatting Example
Penguins - Additional Formatting Example
------------------------------------

This example demonstrates additional formatting that is not supported in
Expand All @@ -13,7 +13,7 @@
As with all formatting, supported arguments are desribed in the
`XlsxWriter documentation <https://xlsxwriter.readthedocs.io/format.html#format-methods-and-format-properties>`_.

Any formatting not possibly through this means can be achieved using
Any formatting not possible through this means can be achieved using
``XlsxWriter`` `Workbook <https://xlsxwriter.readthedocs.io/workbook.html>`_
and `Worksheet <https://xlsxwriter.readthedocs.io/worksheet.html>`_ functionality.
A ``gptable.GPWorkbook`` object is returned when using the
Expand All @@ -30,96 +30,76 @@
## Read data and arrange
parent_dir = Path(__file__).parent

iris_data = pd.read_csv(parent_dir / "iris.csv")
penguins_data = pd.read_csv(parent_dir / "penguins.csv")

iris_data.rename(
columns={
"class": "class",
"sepal_length": "Sepal Length",
"petal_length": "Petal Length",
"petal_width": "Petal Width",
"sepal_width": "Sepal Width",
},
inplace=True,
)

iris_data["class"] = iris_data.apply(lambda row: row["class"][5:].capitalize(), axis=1)

# Calculate summaries
subtables = []
funcs = [np.mean, np.median]
for func in funcs:
subtables.append(iris_data.groupby("class").agg(func))
subtables.append(pd.DataFrame(iris_data.iloc[:,0:4].agg(func).rename("All")).T)
iris_summary = pd.concat(subtables)
iris_summary["Average"] = ["Mean"] * 4 + ["Median"] * 4

# Reshape
iris_summary = iris_summary.reset_index()
iris_summary = iris_summary.melt(["index", "Average"], var_name="Iris feature")
iris_summary = iris_summary.pivot_table(
index=["Iris feature", "Average"], columns="index", values="value"
).reset_index()
#Any data processing could go here as long as you end with a Pandas dataframe that you want to write in a spreadsheet
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Much better to replace the wrangling with a comment, thank you for doing this


## Define table elements
table_name = "iris_statistics"
title = "Iris flower dimensions"
subtitles = [
"1936 Fisher, R.A; The use of multiple measurements in taxonomic problems",
penguins_table_name = "penguins_statistics"
penguins_title = "Penguins"

#Individual words/phrases can have formatting applied without the use of the additional_formatting argument
penguins_subtitles = [
"The first subtitle",
[{"bold": True}, "Just", " another subtitle"]
]
units = {key: "cm" for key in range(2,6)}
scope = "Iris"
index = {1: 0, 2: 1}
penguins_units = {key: "mm" for key in range(2,5)}
penguins_scope = "Penguins"

## Define additional formatting
# Columns can be references by name or number
# Columns can be referenced by name or number
# Rows may only be referenced by number
# Column and row numbers refer to the table elements, including indexes and column headings
additional_formatting = [
penguins_additional_formatting = [
{
"column": {
"columns": ["Setosa", "Versicolor"], # str, int or list of either
"format": {"align": "center"},
"columns": ["Species", "Island"], # str, int or list of either
"format": {"align": "center","italic":True}, #The "Species" and "Island" columns are centre-aligned and made italic
}
},
{"column": {"columns": [3], "format": {"left": 1}}},
{"column": {"columns": [3], "format": {"left": 1}}}, #Gives the fourth column a left border
{
"row": {
"rows": -1, # Numbers only, but can refer to last row using -1
"format": {"bottom": 1}, # Underline row
"format": {"bottom": 1, "indentation":2}, # Give the last row a border at the bottom of each cell and indents two levels
}
},
]

# or just use kwargs
kwargs = {
"table_name": table_name,
"title": title,
"subtitles": subtitles,
"units": units,
"scope": scope,
"table_name": penguins_table_name,
"title": penguins_title,
"subtitles": penguins_subtitles,
"units": penguins_units,
"scope": penguins_scope,
"source": None,
"index_columns": index,
"additional_formatting": additional_formatting,
"additional_formatting": penguins_additional_formatting,
}

## Define our GPTable
iris_table = gpt.GPTable(table=iris_summary, **kwargs)
penguins_table = gpt.GPTable(table=penguins_data, **kwargs)

## Use produce workbook to return GPWorkbook
if __name__ == "__main__":
output_path = parent_dir / "python_iris_additional_formatting_gptable.xlsx"
output_path = parent_dir / "python_penguins_additional_formatting_gptable.xlsx"
wb = gpt.produce_workbook(
filename=output_path, sheets={"Iris Flower Dimensions": iris_table}
filename=output_path, sheets={"Penguins": penguins_table}
)

# Carry out additional modifications on the GPWorkbook or GPWorksheets
# This supports all `XlsxWriter` package functionality
ws = wb.worksheets()[0]
ws.set_row(0, 30) # Set the height of the first row

#To format cells using the set_row or set_column functions we must use a workbook to create a format object
italic_format=wb.add_format({"italic":True})
ws.set_column(2,3,10,italic_format) #Sets the width of the third and fourth column and makes them italic

#Note that the first two arguments of set_column are the first and last columns (inclusive) you want to format as opposed
#to set_row which only affects a single row at a time (the first argument).

# Finally use the close method to save the output
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really helpful addition


wb.close()
print("Output written at: ", output_path)

Expand Down
Loading
Loading