FastAPI
Learn
Read “FastAPI for Flask Users” (10 minutes)
Watch “Build a machine learning API from scratch” by FastAPI’s creator. Live coding starts at 4:55, ends at 50:20. (25 minutes at 2x speed)
Example: Data visualization
Labs projects can use Plotly, a popular visualization library for both Python & JavaScript.
Follow the getting started instructions.
Edit app/main.py to add your API title and description.
app = FastAPI(
title='World Metrics DS API',
description='Visualize world metrics from Gapminder data',
docs_url='/'
)Prototype your visualization in a notebook.
import plotly.express as px
dataframe = px.data.gapminder().rename(columns={
'year': 'Year',
'lifeExp': 'Life Expectancy',
'pop': 'Population',
'gdpPercap': 'GDP Per Capita'
})
country = 'United States'
metric = 'Population'
subset = dataframe[dataframe.country == country]
fig = px.line(subset, x='Year', y=metric, title=f'{metric} in {country}')
fig.show()Define a function for your visualization. End with return fig.to_json()
Then edit app/viz.py to add your code.
Install plotly and test locally.
Your web teammates will re-use the data viz code & docs in our labs-spa-starter repo. The web app will call the DS API to get the data, then use react-plotly.js to render the visualization.
Plotly Python docs
Plotly JavaScript docs
BloomTech
labs-spa-starterdata viz code & docs
Example: Machine learning
Follow the getting started instructions.
Edit app/main.py to add your API title and description.
Edit app/ml.py to add a predict function that returns a naive baseline.
In a notebook, explore your data. Make an educated guess of what features you'll use.
Add a class in app/ml.py to use your features.
Install pandas if you haven't already.
Test locally. Now your web teammates can make POST requests to your API endpoint.
In a notebook, train your pipeline and pickle it. See these docs:
Get version numbers for every package you used in your pipeline. Install the exact versions of these packages in your virtual environment.
Edit app/ml.py to deserialize your model and use it in your predict function.
Now you are ready to re-deploy! 🚀
Last updated
Was this helpful?