Merge branch 'master' of https://github.com/channelcat/sanic into add_specific_middleware
This commit is contained in:
commit
ce4e7437de
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -14,5 +14,3 @@ settings.py
|
||||||
docs/_build/
|
docs/_build/
|
||||||
docs/_api/
|
docs/_api/
|
||||||
build/*
|
build/*
|
||||||
access.log
|
|
||||||
error.log
|
|
||||||
|
|
|
@ -76,13 +76,6 @@ By default, log_config parameter is set to use sanic.config.LOGGING dictionary f
|
||||||
(Notice that in Docker you have to enable everything by yourself)
|
(Notice that in Docker you have to enable everything by yourself)
|
||||||
|
|
||||||
|
|
||||||
- accessTimedRotatingFile (using [logging.handlers.TimedRotatingFileHandler](https://docs.python.org/3/library/logging.handlers.html#logging.handlers.TimedRotatingFileHandler))<br>
|
|
||||||
For requests information logging to file with daily rotation support.
|
|
||||||
|
|
||||||
|
|
||||||
- errorTimedRotatingFile (using [logging.handlers.TimedRotatingFileHandler](https://docs.python.org/3/library/logging.handlers.html#logging.handlers.TimedRotatingFileHandler))<br>
|
|
||||||
For error message and traceback logging to file with daily rotation support.
|
|
||||||
|
|
||||||
And `filters`:
|
And `filters`:
|
||||||
|
|
||||||
- accessFilter (using sanic.log.DefaultFilter)<br>
|
- accessFilter (using sanic.log.DefaultFilter)<br>
|
||||||
|
|
|
@ -1,84 +1,24 @@
|
||||||
from sanic import Sanic
|
from sanic import Sanic
|
||||||
|
from sanic.response import html
|
||||||
from sanic_session import InMemorySessionInterface
|
|
||||||
from sanic_jinja2 import SanicJinja2
|
|
||||||
|
|
||||||
import json
|
|
||||||
import plotly
|
import plotly
|
||||||
|
import plotly.graph_objs as go
|
||||||
import pandas as pd
|
|
||||||
import numpy as np
|
|
||||||
|
|
||||||
app = Sanic(__name__)
|
app = Sanic(__name__)
|
||||||
|
|
||||||
jinja = SanicJinja2(app)
|
|
||||||
session = InMemorySessionInterface(cookie_name=app.name, prefix=app.name)
|
|
||||||
|
|
||||||
@app.middleware('request')
|
|
||||||
async def print_on_request(request):
|
|
||||||
print(request.headers)
|
|
||||||
await session.open(request)
|
|
||||||
|
|
||||||
@app.middleware('response')
|
|
||||||
async def print_on_response(request, response):
|
|
||||||
await session.save(request, response)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@app.route('/')
|
@app.route('/')
|
||||||
async def index(request):
|
async def index(request):
|
||||||
rng = pd.date_range('1/1/2011', periods=7500, freq='H')
|
trace1 = go.Scatter(
|
||||||
ts = pd.Series(np.random.randn(len(rng)), index=rng)
|
x=[0, 1, 2, 3, 4, 5],
|
||||||
|
y=[1.5, 1, 1.3, 0.7, 0.8, 0.9]
|
||||||
graphs = [
|
|
||||||
dict(
|
|
||||||
data=[
|
|
||||||
dict(
|
|
||||||
x=[1, 2, 3],
|
|
||||||
y=[10, 20, 30],
|
|
||||||
type='scatter'
|
|
||||||
),
|
|
||||||
],
|
|
||||||
layout=dict(
|
|
||||||
title='first graph'
|
|
||||||
)
|
)
|
||||||
),
|
trace2 = go.Bar(
|
||||||
|
x=[0, 1, 2, 3, 4, 5],
|
||||||
dict(
|
y=[1, 0.5, 0.7, -1.2, 0.3, 0.4]
|
||||||
data=[
|
|
||||||
dict(
|
|
||||||
x=[1, 3, 5],
|
|
||||||
y=[10, 50, 30],
|
|
||||||
type='bar'
|
|
||||||
),
|
|
||||||
],
|
|
||||||
layout=dict(
|
|
||||||
title='second graph'
|
|
||||||
)
|
)
|
||||||
),
|
|
||||||
|
|
||||||
dict(
|
data = [trace1, trace2]
|
||||||
data=[
|
return html(plotly.offline.plot(data, auto_open=False, output_type='div'))
|
||||||
dict(
|
|
||||||
x=ts.index, # Can use the pandas data structures directly
|
|
||||||
y=ts
|
|
||||||
)
|
|
||||||
]
|
|
||||||
)
|
|
||||||
]
|
|
||||||
|
|
||||||
# Add "ids" to each of the graphs to pass up to the client
|
|
||||||
# for templating
|
|
||||||
ids = ['graph-{}'.format(i) for i, _ in enumerate(graphs)]
|
|
||||||
|
|
||||||
# Convert the figures to JSON
|
|
||||||
# PlotlyJSONEncoder appropriately converts pandas, datetime, etc
|
|
||||||
# objects to their JSON equivalents
|
|
||||||
graphJSON = json.dumps(graphs, cls=plotly.utils.PlotlyJSONEncoder)
|
|
||||||
|
|
||||||
return jinja.render('index.html', request,
|
|
||||||
ids=ids,
|
|
||||||
graphJSON=graphJSON)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
|
@ -1,5 +1,2 @@
|
||||||
pandas==0.19.2
|
plotly>=2.0.7
|
||||||
plotly==2.0.7
|
sanic>=0.5.0
|
||||||
sanic==0.5.0
|
|
||||||
sanic-jinja2==0.5.1
|
|
||||||
sanic-session==0.1.3
|
|
|
@ -77,24 +77,6 @@ LOGGING = {
|
||||||
'filters': ['errorFilter'],
|
'filters': ['errorFilter'],
|
||||||
'formatter': 'simple'
|
'formatter': 'simple'
|
||||||
},
|
},
|
||||||
'accessTimedRotatingFile': {
|
|
||||||
'class': 'logging.handlers.TimedRotatingFileHandler',
|
|
||||||
'filters': ['accessFilter'],
|
|
||||||
'formatter': 'access',
|
|
||||||
'when': 'D',
|
|
||||||
'interval': 1,
|
|
||||||
'backupCount': 7,
|
|
||||||
'filename': 'access.log'
|
|
||||||
},
|
|
||||||
'errorTimedRotatingFile': {
|
|
||||||
'class': 'logging.handlers.TimedRotatingFileHandler',
|
|
||||||
'filters': ['errorFilter'],
|
|
||||||
'when': 'D',
|
|
||||||
'interval': 1,
|
|
||||||
'backupCount': 7,
|
|
||||||
'filename': 'error.log',
|
|
||||||
'formatter': 'simple'
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
'loggers': {
|
'loggers': {
|
||||||
'sanic': {
|
'sanic': {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user