Skip to content

Instantly share code, notes, and snippets.

@edoakes
Created September 21, 2021 19:42
Show Gist options
  • Save edoakes/e48d2ede129adb87709bc9dc76b23f6c to your computer and use it in GitHub Desktop.
Save edoakes/e48d2ede129adb87709bc9dc76b23f6c to your computer and use it in GitHub Desktop.
import dash
from dash import dcc, html
from dash.dependencies import Input, Output
import pandas as pd
import plotly.graph_objs as obj
import uvicorn as uvicorn
from fastapi import FastAPI
from starlette.middleware.wsgi import WSGIMiddleware
app = dash.Dash(__name__, requests_pathname_prefix="/dash/")
years = list(range(1940, 2021, 1))
temp_high = [x / 20 for x in years]
temp_low = [x - 20 for x in temp_high]
df = pd.DataFrame({"Year": years, "TempHigh": temp_high, "TempLow": temp_low})
slider = dcc.RangeSlider(
id="slider",
value=[df["Year"].min(), df["Year"].max()],
min=df["Year"].min(),
max=df["Year"].max(),
step=5,
marks={
1940: "1940",
1945: "1945",
1950: "1950",
1955: "1955",
1960: "1960",
1965: "1965",
1970: "1970",
1975: "1975",
1980: "1980",
1985: "1985",
1990: "1990",
1995: "1995",
2000: "2000",
2005: "2005",
2010: "2010",
2015: "2015",
2020: "2020",
},
)
app.layout = html.Div(
children=[
html.H1(children="Data Visualization with Dash"),
html.Div(children="High/Low Temperatures Over Time"),
dcc.Graph(id="temp-plot"),
slider,
]
)
@app.callback(Output("temp-plot", "figure"), [Input("slider", "value")])
def add_graph(slider):
print(type(slider))
trace_high = obj.Scatter(x=df["Year"], y=df["TempHigh"], mode="markers", name="High Temperatures")
trace_low = obj.Scatter(x=df["Year"], y=df["TempLow"], mode="markers", name="Low Temperatures")
layout = obj.Layout(xaxis=dict(range=[slider[0], slider[1]]), yaxis={"title": "Temperature"})
figure = obj.Figure(data=[trace_high, trace_low], layout=layout)
return figure
if __name__ == "__main__":
server = FastAPI()
server.mount("/dash", WSGIMiddleware(app.server))
import ray
from ray import serve
ray.init(address="auto")
serve.start()
@serve.deployment(route_prefix="/")
@serve.ingress(server)
class MyServeWrapper:
pass
MyServeWrapper.deploy()
@edoakes
Copy link
Author

edoakes commented Sep 21, 2021

Output:

Traceback (most recent call last):
  File "waleed.py", line 76, in <module>
    class MyServeWrapper:
  File "/Users/eoakes/anaconda3/envs/ray/lib/python3.8/site-packages/ray/serve/api.py", line 1056, in decorator
    frozen_app = cloudpickle.loads(cloudpickle.dumps(app))
  File "/Users/eoakes/anaconda3/envs/ray/lib/python3.8/site-packages/ray/cloudpickle/cloudpickle_fast.py", line 73, in dumps
    cp.dump(obj)
  File "/Users/eoakes/anaconda3/envs/ray/lib/python3.8/site-packages/ray/cloudpickle/cloudpickle_fast.py", line 580, in dump
    return Pickler.dump(self, obj)
TypeError: cannot pickle 'weakref' object

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment