Skip to content

Commit

Permalink
CSS for ipynb (#337)
Browse files Browse the repository at this point in the history
* test

* env

* warning

* Automated changes

* Automated changes

* header

* Automated changes

* Automated changes

* header

* Automated changes

* Automated changes

* pencil

* Automated changes

* Automated changes

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
linogaliana and github-actions[bot] committed Dec 26, 2022
1 parent 1fe65ac commit 129b001
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 10 deletions.
94 changes: 94 additions & 0 deletions build/pimp_notebook.py
Expand Up @@ -12,6 +12,94 @@ def transform_note_reference(re_find, content_note=False):
return note_html


def replace_pattern(
string, pattern,
rules_general,
rules_specific,
box_type = "warning",
put_in_pattern = True
):
rules = rules_general + [rules_specific[box_type]]
rules_inline = " ".join(rules)
empty=""
if put_in_pattern is True:
pattern_find = pattern.format(box_type, empty)
pattern_repl = pattern.format(box_type, f' style="{rules_inline}"')
else:
pattern_find = pattern.format(empty)
pattern_repl = pattern.format(f' style="{rules_inline}"')
return re.sub(pattern_find, pattern_repl, string)

def box_for_jupyter(tweak_md, btype):
rules_general_box = [
'color: rgba(0,0,0,.8);',
'background-color: white;',
'margin-top: 1em;',
'margin-bottom: 1em;',
'margin:1.5625emauto;',
'padding:0 .6rem .8rem!important;overflow:hidden;',
'page-break-inside:avoid;',
'border-radius:.25rem;',
'box-shadow:0 .2rem .5rem rgba(0,0,0,.05),0 0 .05rem rgba(0,0,0,.1);',
'transition:color .25s,background-color .25s,border-color .25s ;',
'border-right: 1px solid #dee2e6 ;',
'border-top: 1px solid #dee2e6 ;',
'border-bottom: 1px solid #dee2e6 ;'
]
rules_specific_box = {
"danger": "border-left: .2rem solid #ff0039;",
"warning": "border-left:.2rem solid #ffc10780;",
"info": "border-left:.2rem solid #007bff80;",
"success": "border-left:.2rem solid #3fb618;"
}
rules_general_heading = [
"margin-bottom: 15px !important;",
"margin-top: 0rem;",
"font-weight:600;",
"font-size: 1.1rem;",
"padding-top: .2em;",
"padding-bottom: .2em;"
]
rules_specific_heading = {
"danger": "background-color: #dc3545;",
"warning": "background-color: #fff6dd;",
"info": "background-color: #e7f2fa;",
"success": "background-color: #ecf8e8;"
}

pattern_box = r'<div class="alert alert-{}" role="alert"{}>'
pattern_heading = '<h3 class="alert-heading"{}>'
old_icons = {
"warning": "fa fa-lightbulb-o",
"success": "fa fa-pencil",
"info": "fa fa-comment",
"danger": "fa fa-exclamation-triangle"
}
new_icons = {
"warning": "fa-solid fa-lightbulb",
"success": "fa-solid fa-pencil",
"info": "fa-solid fa-comment",
"danger": "fa-solid fa-triangle-exclamation"
}

tweak_md2 = replace_pattern(
tweak_md,
pattern=pattern_box,
rules_general=rules_general_box,
rules_specific=rules_specific_box,
box_type=btype)
#tweak_md2 = replace_pattern(
# tweak_md2, pattern=pattern_heading,
# rules_general=rules_general_heading,
# rules_specific=rules_specific_heading,
# put_in_pattern=False,
# box_type=btype)
tweak_md2 = re.sub(
new_icons[btype], old_icons[btype],
tweak_md2
)
return tweak_md2

def change_box_markdown(fl):
content = read_file(fl)
content = re.sub(r"(“|”)",'"',content)
Expand All @@ -30,6 +118,12 @@ def change_box_markdown(fl):
tweak_md = [corresp_boxes[re.search('status=\"(.*?)\"',l).group(1)] if l.startswith("{{% box") else l for l in list_rows]
tweak_md = ["```{=html}\n</div>\n```\n:::" if l.startswith("{{% /box") else l for l in tweak_md]
tweak_md = "\n".join(tweak_md)
# FORCE CSS INSIDE DEFINITION
tweak_md = box_for_jupyter(tweak_md, "warning")
tweak_md = box_for_jupyter(tweak_md, "info")
tweak_md = box_for_jupyter(tweak_md, "success")
tweak_md = box_for_jupyter(tweak_md, "danger")

# FOOTNOTES
p = re.compile("\[\^[0-9]+\]:")
list_match = list(p.finditer(tweak_md))
Expand Down
21 changes: 11 additions & 10 deletions content/course/modelisation/0_preprocessing/index.qmd
Expand Up @@ -26,6 +26,7 @@ plotly: true
bibliography: ../../../../reference.bib
---


::: {.cell .markdown}
```{python}
#| echo: false
Expand Down Expand Up @@ -106,8 +107,8 @@ pour [réviser `pandas`](#pandas) :

::: {.cell .markdown}
```{=html}
<div class="alert alert-info" role="alert">
<h3 class="alert-heading"><i class="fa-solid fa-comment"></i> Exercice 1 : Importer les données des élections US</h3>
<div class="alert alert-success" role="alert">
<h3 class="alert-heading"><i class="fa-solid fa-pencil"></i> Exercice 1 : Importer les données des élections US</h3>
```

__Cet exercice est OPTIONNEL__
Expand Down Expand Up @@ -298,8 +299,8 @@ ainsi que les relations entre les variables explicatives.

::: {.cell .markdown}
```{=html}
<div class="alert alert-info" role="alert">
<h3 class="alert-heading"><i class="fa-solid fa-comment"></i> Exercice 2 : Regarder les corrélations entre les variables</h3>
<div class="alert alert-success" role="alert">
<h3 class="alert-heading"><i class="fa-solid fa-pencil"></i> Exercice 2 : Regarder les corrélations entre les variables</h3>
```

1. Créer un DataFrame `df2` plus petit avec les variables `winner`, `votes_gop`, `Unemployment_rate_2019`,
Expand Down Expand Up @@ -448,8 +449,8 @@ La standardisation consiste à transformer des données pour que la distribution

::: {.cell .markdown}
```{=html}
<div class="alert alert-info" role="alert">
<h3 class="alert-heading"><i class="fa-solid fa-comment"></i> Exercice 3: Standardisation</h3>
<div class="alert alert-success" role="alert">
<h3 class="alert-heading"><i class="fa-solid fa-pencil"></i> Exercice 3: Standardisation</h3>
```


Expand Down Expand Up @@ -551,8 +552,8 @@ Cette transformation est particulièrement utilisée en classification de texte

::: {.cell .markdown}
```{=html}
<div class="alert alert-info" role="alert">
<h3 class="alert-heading"><i class="fa-solid fa-comment"></i> Exercice 4 : Normalisation</h3>
<div class="alert alert-success" role="alert">
<h3 class="alert-heading"><i class="fa-solid fa-pencil"></i> Exercice 4 : Normalisation</h3>
```

1. Normaliser la variable `Median_Household_Income_2019` (ne pas écraser les valeurs !) et regarder l'histogramme avant/après normalisation.
Expand Down Expand Up @@ -636,8 +637,8 @@ Il a plutôt vocation à s'appliquer sur les *features* ($X$) du modèle

::: {.cell .markdown}
```{=html}
<div class="alert alert-info" role="alert">
<h3 class="alert-heading"><i class="fa-solid fa-comment"></i> Exercice 5 : Encoder des variables catégorielles</h3>
<div class="alert alert-success" role="alert">
<h3 class="alert-heading"><i class="fa-solid fa-pencil"></i> Exercice 5 : Encoder des variables catégorielles</h3>
```

1. Créer `df` qui conserve uniquement les variables `state_name` et `county_name` dans `votes`.
Expand Down

0 comments on commit 129b001

Please sign in to comment.