Skip to content

Commit

Permalink
Merge pull request #2 from mysociety/charting-improvement
Browse files Browse the repository at this point in the history
Charting improvement
  • Loading branch information
ajparsons authored Dec 16, 2021
2 parents c9a3ea6 + 3d7314e commit 3effec1
Show file tree
Hide file tree
Showing 20 changed files with 837 additions and 548 deletions.
16 changes: 8 additions & 8 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
FROM python:3.8-buster

ENV DEBIAN_FRONTEND noninteractive

COPY packages_setup.bash /
RUN chmod +x /packages_setup.bash && /packages_setup.bash

COPY base_requirements.txt /
FROM python:3.8-buster

ENV DEBIAN_FRONTEND noninteractive

COPY packages_setup.bash /
RUN chmod +x /packages_setup.bash && /packages_setup.bash

COPY base_requirements.txt /
RUN pip install -r /base_requirements.txt
41 changes: 32 additions & 9 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

import builtins as __builtin__
import datetime
import re
import unicodedata
from dataclasses import dataclass
from functools import partial
from pathlib import Path
Expand All @@ -14,9 +16,15 @@
import pandas as pd
from IPython.display import Markdown as md

from .charting import (Chart, ChartEncoding, altair_sw_theme, altair_theme,
enable_sw_charts)
from .df_extensions import space, viz, common
from .charting import (
Chart,
ChartEncoding,
altair_sw_theme,
altair_theme,
enable_sw_charts,
ChartTitle
)
from .df_extensions import common, space, viz
from .helpers.pipe import Pipe, Pipeline, iter_format
from .management.exporters import render_to_html, render_to_markdown
from .management.settings import settings
Expand All @@ -32,15 +40,17 @@


def page_break():
return md("""
return md(
"""
```{=openxml}
<w:p>
<w:r>
<w:br w:type="page"/>
</w:r>
</w:p>
```
""")
"""
)


def notebook_setup():
Expand All @@ -51,7 +61,20 @@ def Date(x):
return datetime.datetime.fromisoformat(x).date()


comma_thousands = '{:,}'.format
percentage_0dp = '{:,.0%}'.format
percentage_1dp = '{:,.1%}'.format
percentage_2dp = '{:,.2%}'.format
def slugify(value):
"""
Converts to lowercase, removes non-word characters (alphanumerics and
underscores) and converts spaces to hyphens. Also strips leading and
trailing whitespace.
"""
value = (
unicodedata.normalize("NFKD", value).encode("ascii", "ignore").decode("ascii")
)
value = re.sub("[^\w\s-]", "", value).strip().lower()
return re.sub("[-\s]+", "-", value)


comma_thousands = "{:,}".format
percentage_0dp = "{:,.0%}".format
percentage_1dp = "{:,.1%}".format
percentage_2dp = "{:,.2%}".format
44 changes: 27 additions & 17 deletions apis/google_api.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import socket
import sys
from pathlib import Path
Expand All @@ -8,49 +7,58 @@
from googleapiclient.discovery import build
from googleapiclient.http import MediaFileUpload

SCOPES = ['https://www.googleapis.com/auth/drive', "https://www.googleapis.com/auth/documents",
'https://www.googleapis.com/auth/script.projects']
SCOPES = [
"https://www.googleapis.com/auth/drive",
"https://www.googleapis.com/auth/documents",
"https://www.googleapis.com/auth/script.projects",
]

url_template = "https://docs.google.com/document/d/{0}/edit"


class DriveIntegration:

def __init__(self, data):
self.creds = Credentials.from_authorized_user_info(data, SCOPES)
self.api = build('drive', 'v3', credentials=self.creds)
self.api = build("drive", "v3", credentials=self.creds)

def upload_file(self, file_name, file_path, folder_id, drive_id):

body = {'name': file_name, 'driveID': drive_id, "parents": [
folder_id], 'mimeType': 'application/vnd.google-apps.document'}
body = {
"name": file_name,
"driveID": drive_id,
"parents": [folder_id],
"mimeType": "application/vnd.google-apps.document",
}

# Now create the media file upload object and tell it what file to upload,
# in this case 'test.html'
media = MediaFileUpload(
file_path,
mimetype='application/vnd.openxmlformats-officedocument.wordprocessingml.document')
mimetype="application/vnd.openxmlformats-officedocument.wordprocessingml.document",
)

# Now we're doing the actual post, creating a new file of the uploaded type
uploaded = self.api.files().create(body=body, media_body=media,
supportsTeamDrives=True).execute()
uploaded = (
self.api.files()
.create(body=body, media_body=media, supportsTeamDrives=True)
.execute()
)
url = url_template.format(uploaded["id"])
return url


class ScriptIntergration:

def __init__(self, data):
self.creds = Credentials.from_authorized_user_info(data, SCOPES)
socket.setdefaulttimeout(600) # set timeout to 10 minutes
self.api = build('script', 'v1', credentials=self.creds)
self.api = build("script", "v1", credentials=self.creds)

def get_function(self, script_id, function_name):

def inner(*args):
request = {"function": function_name, "parameters": list(args)}
response = self.api.scripts().run(body=request,
scriptId=script_id).execute()
response = (
self.api.scripts().run(body=request, scriptId=script_id).execute()
)

return response

Expand All @@ -66,15 +74,17 @@ def trigger_log_in_flow(settings):
json_creds = creds.to_json()
print(f"GOOGLE_CLIENT_JSON={json_creds}")
raise ValueError("Add the following last line printed to the .env")


def test_settings(settings):
"""
Test we have all the bits we need to connect to the api
"""

if "GOOGLE_APP_JSON" not in settings or settings["GOOGLE_APP_JSON"] == "":
raise ValueError("Missing GOOGLE_APP_JSON settings. See the notebook setup page in the wiki for the correct settings.")
raise ValueError(
"Missing GOOGLE_APP_JSON settings. See the notebook setup page in the wiki for the correct settings."
)

if "GOOGLE_CLIENT_JSON" not in settings or settings["GOOGLE_CLIENT_JSON"] == "":
trigger_log_in_flow(settings)
6 changes: 3 additions & 3 deletions base_requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ altair==4.1.0
altair_saver==0.5.0
jupyter==1.0.0
pylint==2.9.1
pycodestyle==2.7.0
autopep8==1.5.7
ruamel.yaml==0.17.10
pypandoc==1.5
flake8==3.9.2
Expand All @@ -24,4 +22,6 @@ papermill==2.3.3
google-api-python-client==2.21.0
google-auth-httplib2==0.1.0
google-auth-oauthlib==0.4.6
ptitprince==0.2.5
ptitprince==0.2.5
pylint==2.12.2
black[jupyter]==21.12b0
23 changes: 11 additions & 12 deletions charting/__init__.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
from . import theme as altair_theme
from . import sw_theme as altair_sw_theme
from .chart import Chart, Renderer, ChartEncoding
from .chart import Chart, Renderer, ChartEncoding, ChartTitle
from .saver import MSSaver, SWSaver, render, sw_render

import altair as alt

alt.themes.register('mysoc_theme', lambda: altair_theme.mysoc_theme)
alt.themes.enable('mysoc_theme')
alt.themes.register("mysoc_theme", lambda: altair_theme.mysoc_theme)
alt.themes.enable("mysoc_theme")

alt.renderers.register('mysoc_saver', render)
alt.renderers.enable('mysoc_saver')
alt.renderers.register("mysoc_saver", render)
alt.renderers.enable("mysoc_saver")


def enable_sw_charts():
alt.themes.register('societyworks_theme', lambda: altair_sw_theme.sw_theme)
alt.themes.enable('societyworks_theme')

alt.renderers.register('sw_saver', sw_render)
alt.renderers.enable('sw_saver')
Renderer.default_renderer = SWSaver

alt.themes.register("societyworks_theme", lambda: altair_sw_theme.sw_theme)
alt.themes.enable("societyworks_theme")

alt.renderers.register("sw_saver", sw_render)
alt.renderers.enable("sw_saver")
Renderer.default_renderer = SWSaver
Loading

0 comments on commit 3effec1

Please sign in to comment.