Skip to content

Commit

Permalink
Black + Pre-commit Hook (streamlit#81)
Browse files Browse the repository at this point in the history
* Black

* Pre-commit hook

* Make format.sh executable

* Pre-commit hook test

* Hook worked

* Make pytest happy

* Check code formating in CircleCI

* Update pandas

* Lock Pipfiles

* Ignore protobuf files

* Separate Python 2.x and 3.x packages in Pipfile

* Make sure Black exists before calling it

* Black only for Python 3.6.0+

* Update Makefile

* Install Black separately

* Lint
  • Loading branch information
kantuni authored Sep 13, 2019
1 parent 407a9ae commit 62588bf
Show file tree
Hide file tree
Showing 302 changed files with 6,527 additions and 6,166 deletions.
14 changes: 7 additions & 7 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -90,32 +90,32 @@ jobs:
- restore_cache:
name: Restore /opt/conda from cache
keys:
- v12-opt-conda-{{ checksum "~/python_version.md5" }}
- v12-opt-conda-{{ checksum "~/python_version.md5" }}

- restore_cache: &restore_virtualenv
name: Restore virtualenv from cache
keys:
- v12-python-venv-{{ checksum "~/python_version.md5" }}
- v12-python-venv-{{ checksum "~/python_version.md5" }}

- restore_cache: &restore_nvm
name: Restore nvm and node_modules from cache
keys:
- v12-nvm_node_modules-{{ checksum "~/yarn.lock.md5" }}
- v12-nvm_node_modules-{{ checksum "~/yarn.lock.md5" }}

- restore_cache:
name: Restore protobufs from cache
keys:
- v12-protobuf-{{ checksum "~/protobuf.md5" }}
- v12-protobuf-{{ checksum "~/protobuf.md5" }}

- restore_cache: &restore_make
name: Restore make from cache
keys:
- v12_make.bin-{{ checksum "~/make.md5" }}
- v12_make.bin-{{ checksum "~/make.md5" }}

- restore_cache: &restore_dot
name: Restore dot from cache
keys:
- v12_dot.bin-{{ checksum "~/dot.md5" }}
- v12_dot.bin-{{ checksum "~/dot.md5" }}

#################################################################
# Pre Make commands
Expand Down Expand Up @@ -158,7 +158,6 @@ jobs:
paths:
- dot.bin


#################################################################
# Run 'make init'
#################################################################
Expand Down Expand Up @@ -244,6 +243,7 @@ jobs:
name: Run linters.
command: |
make jslint
make pylint
- store_test_results:
path: frontend/test-reports
Expand Down
28 changes: 15 additions & 13 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Black magic to get module directories
PYTHON_MODULES := $(foreach initpy, $(foreach dir, $(wildcard lib/*), $(wildcard $(dir)/__init__.py)), $(realpath $(dir $(initpy))))

PY_VERSION := python-$(shell python -c 'import platform; print(platform.python_version())')
PY_VERSION := $(shell python -c 'import platform; print(platform.python_version())')

.PHONY: help
help:
Expand Down Expand Up @@ -31,7 +30,10 @@ build: react-build

.PHONY: setup
setup:
pip install pip-tools pipenv
pip install pip-tools pipenv ; \
if [[ $(PY_VERSION) == "3.6.0" || $(PY_VERSION) > "3.6.0" ]] ; then \
pip install black ; \
fi

.PHONY: pipenv-install
pipenv-install: lib/Pipfile
Expand All @@ -44,11 +46,11 @@ pipenv-lock: lib/Pipfile
@# Regenerates Pipfile.lock and rebuilds the virtualenv. This is rather slow.
# In CircleCI, dont generate Pipfile.lock This is only used for development.
ifndef CIRCLECI
cd lib; rm -f Pipfile.lock; pipenv lock --dev && mv Pipfile.lock Pipfile.locks/$(PY_VERSION)
cd lib; rm -f Pipfile.lock; pipenv lock --dev && mv Pipfile.lock Pipfile.locks/python-$(PY_VERSION)
else
echo "Running in CircleCI, not generating requirements."
endif
cd lib; rm -f Pipfile.lock; cp -f Pipfile.locks/$(PY_VERSION) Pipfile.lock
cd lib; rm -f Pipfile.lock; cp -f Pipfile.locks/python-$(PY_VERSION) Pipfile.lock
ifndef CIRCLECI
# Dont update lockfile and install whatever is in lock.
cd lib; pipenv install --ignore-pipfile --dev
Expand All @@ -59,14 +61,14 @@ endif
.PHONY: pylint
# Run Python linter.
pylint:
# Linting
# (Ignore E402 since our Python2-compatibility imports break this lint rule.)
cd lib; \
flake8 \
--ignore=E402,E128 \
--exclude=streamlit/proto/*_pb2.py \
$(PYTHON_MODULES) \
tests/
# It requires Python 3.6.0+ to run but you can reformat
# Python 2 code with it, too.
if command -v "black" > /dev/null; then \
black --check docs/ ; \
black --check examples/ ; \
black --check lib/streamlit/ --exclude=/*_pb2.py$/ ; \
black --check lib/tests/ --exclude=compile_error.py ; \
fi

.PHONY: pytest
# Run Python unit tests.
Expand Down
22 changes: 12 additions & 10 deletions docs/_ext/stoutput.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from docutils import nodes
from docutils.parsers.rst import Directive


class StOutput(Directive):
"""Insert Streamlit report into HTML doc.
Expand Down Expand Up @@ -28,21 +29,20 @@ def run(self):

src = self.arguments[0]

if not src.startswith('https://'):
if not src.startswith("https://"):
raise ValueError(
'Iframed URLs in docs should be HTTPS!\n'
'--> Culprit: %s' % src)
"Iframed URLs in docs should be HTTPS!\n" "--> Culprit: %s" % src
)

if len(self.arguments) > 1:
additional_styles = self.arguments[1]
else:
additional_styles = 'height: 10rem;'

additional_styles = "height: 10rem;"

node = nodes.raw(
rawsource='',
format='html',
text='''
rawsource="",
format="html",
text="""
<iframe
src="%(src)s&embed=true"
style="
Expand All @@ -54,9 +54,11 @@ def run(self):
<sup><a href="%(src)s" target="_blank">
(view standalone Streamlit report)
</a></sup>
''' % {'src': src, 'additional_styles': additional_styles})
"""
% {"src": src, "additional_styles": additional_styles},
)
return [node]


def setup(app):
app.add_directive('output', StOutput)
app.add_directive("output", StOutput)
4 changes: 1 addition & 3 deletions docs/api-examples-source/charts.area_chart.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
import pandas as pd
import numpy as np

chart_data = pd.DataFrame(
np.random.randn(20, 3),
columns=['a', 'b', 'c'])
chart_data = pd.DataFrame(np.random.randn(20, 3), columns=["a", "b", "c"])

st.area_chart(chart_data)
13 changes: 9 additions & 4 deletions docs/api-examples-source/charts.audio.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import streamlit as st
import urllib


@st.cache
def read_file_from_url(url):
return urllib.request.urlopen(url).read()


file_bytes = read_file_from_url(
'https://streamlit.io/media/Muriel-Nguyen-Xuan-Chopin-valse-opus64-1.ogg')
"https://streamlit.io/media/Muriel-Nguyen-Xuan-Chopin-valse-opus64-1.ogg"
)

st.audio(file_bytes, format='audio/ogg')
st.audio(file_bytes, format="audio/ogg")

st.write('''
st.write(
"""
#### Audio credit:
Performer: _Muriel Nguyen Xuan_ and _Stéphane Magnenat_
Expand All @@ -23,4 +27,5 @@ def read_file_from_url(url):
URL:
https://upload.wikimedia.org/wikipedia/commons/c/c4/Muriel-Nguyen-Xuan-Chopin-valse-opus64-1.ogg
''')
"""
)
4 changes: 1 addition & 3 deletions docs/api-examples-source/charts.bar_chart.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import streamlit as st
import pandas as pd

chart_data = pd.DataFrame(
[[20, 30, 50]],
columns=['a', 'b', 'c'])
chart_data = pd.DataFrame([[20, 30, 50]], columns=["a", "b", "c"])

st.bar_chart(chart_data)
4 changes: 2 additions & 2 deletions docs/api-examples-source/charts.bokeh_chart.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
x = [1, 2, 3, 4, 5]
y = [6, 7, 2, 4, 5]

p = figure(title='simple line example', x_axis_label='x', y_axis_label='y')
p = figure(title="simple line example", x_axis_label="x", y_axis_label="y")

p.line(x, y, legend='Trend', line_width=2)
p.line(x, y, legend="Trend", line_width=2)

st.bokeh_chart(p)
19 changes: 6 additions & 13 deletions docs/api-examples-source/charts.deck_gl_charts1.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,14 @@
import numpy as np

df = pd.DataFrame(
np.random.randn(1000, 2) / [50, 50] + [37.76, -122.4],
columns=['lat', 'lon'])
np.random.randn(1000, 2) / [50, 50] + [37.76, -122.4], columns=["lat", "lon"]
)

# TODO: Use this instead of the example below. Need to autodetect viewport
# first, thought.
#st.deck_gl_chart(df)
# st.deck_gl_chart(df)

st.deck_gl_chart(
viewport={
'latitude': 37.76,
'longitude': -122.4,
'zoom': 11,
'pitch': 50,
},
layers=[{
'type': 'ScatterplotLayer',
'data': df,
}])
viewport={"latitude": 37.76, "longitude": -122.4, "zoom": 11, "pitch": 50},
layers=[{"type": "ScatterplotLayer", "data": df}],
)
36 changes: 16 additions & 20 deletions docs/api-examples-source/charts.deck_gl_charts2.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,21 @@
import numpy as np

df = pd.DataFrame(
np.random.randn(1000, 2) / [50, 50] + [37.76, -122.4],
columns=['lat', 'lon'])
np.random.randn(1000, 2) / [50, 50] + [37.76, -122.4], columns=["lat", "lon"]
)

st.deck_gl_chart(
viewport={
'latitude': 37.76,
'longitude': -122.4,
'zoom': 11,
'pitch': 50,
},
layers=[{
'type': 'HexagonLayer',
'data': df,
'radius': 200,
'elevationScale': 4,
'elevationRange': [0, 1000],
'pickable': True,
'extruded': True,
}, {
'type': 'ScatterplotLayer',
'data': df,
}])
viewport={"latitude": 37.76, "longitude": -122.4, "zoom": 11, "pitch": 50},
layers=[
{
"type": "HexagonLayer",
"data": df,
"radius": 200,
"elevationScale": 4,
"elevationRange": [0, 1000],
"pickable": True,
"extruded": True,
},
{"type": "ScatterplotLayer", "data": df},
],
)
26 changes: 13 additions & 13 deletions docs/api-examples-source/charts.graphviz_chart.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@

# Create a graphlib graph object
graph = graphviz.Digraph()
graph.edge('run', 'intr')
graph.edge('intr', 'runbl')
graph.edge('runbl', 'run')
graph.edge('run', 'kernel')
graph.edge('kernel', 'zombie')
graph.edge('kernel', 'sleep')
graph.edge('kernel', 'runmem')
graph.edge('sleep', 'swap')
graph.edge('swap', 'runswap')
graph.edge('runswap', 'new')
graph.edge('runswap', 'runmem')
graph.edge('new', 'runmem')
graph.edge('sleep', 'runmem')
graph.edge("run", "intr")
graph.edge("intr", "runbl")
graph.edge("runbl", "run")
graph.edge("run", "kernel")
graph.edge("kernel", "zombie")
graph.edge("kernel", "sleep")
graph.edge("kernel", "runmem")
graph.edge("sleep", "swap")
graph.edge("swap", "runswap")
graph.edge("runswap", "new")
graph.edge("runswap", "runmem")
graph.edge("new", "runmem")
graph.edge("sleep", "runmem")

st.graphviz_chart(graph)
14 changes: 9 additions & 5 deletions docs/api-examples-source/charts.image.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,21 @@
from io import BytesIO
from PIL import Image


@st.cache
def read_file_from_url(url):
return urllib.request.urlopen(url).read()


file_bytes = read_file_from_url(
'https://streamlit.io/media/photo-1548407260-da850faa41e3.jpeg')
"https://streamlit.io/media/photo-1548407260-da850faa41e3.jpeg"
)
image = Image.open(BytesIO(file_bytes))

st.image(image, caption='Sunrise by the mountains',
use_column_width=True)
st.image(image, caption="Sunrise by the mountains", use_column_width=True)

st.write('''
st.write(
"""
#### Image credit:
Creator: User _fxxu_ from _Pixabay_.
Expand All @@ -25,4 +28,5 @@ def read_file_from_url(url):
URL:
https://pixabay.com/en/videos/star-long-exposure-starry-sky-sky-6962/
''')
"""
)
4 changes: 1 addition & 3 deletions docs/api-examples-source/charts.line_chart.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
import pandas as pd
import numpy as np

chart_data = pd.DataFrame(
np.random.randn(20, 3),
columns=['a', 'b', 'c'])
chart_data = pd.DataFrame(np.random.randn(20, 3), columns=["a", "b", "c"])

st.line_chart(chart_data)
4 changes: 2 additions & 2 deletions docs/api-examples-source/charts.map.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import numpy as np

df = pd.DataFrame(
np.random.randn(1000, 2) / [50, 50] + [37.76, -122.4],
columns=['lat', 'lon'])
np.random.randn(1000, 2) / [50, 50] + [37.76, -122.4], columns=["lat", "lon"]
)

st.map(df)
5 changes: 2 additions & 3 deletions docs/api-examples-source/charts.plotly_chart.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@
# Group data together
hist_data = [x1, x2, x3]

group_labels = ['Group 1', 'Group 2', 'Group 3']
group_labels = ["Group 1", "Group 2", "Group 3"]

# Create distplot with custom bin_size
fig = ff.create_distplot(
hist_data, group_labels, bin_size=[.1, .25, .5])
fig = ff.create_distplot(hist_data, group_labels, bin_size=[0.1, 0.25, 0.5])

# Plot!
st.plotly_chart(fig)
Loading

0 comments on commit 62588bf

Please sign in to comment.