Preheat the oven to 350°F (175°C).
If using a frozen pie crust, follow package instructions and bake it until golden brown, usually around 10 minutes; if homemade, roll out and fit it into a pie dish.
In a medium saucepan, whisk together the sugar, cocoa powder, flour, and salt, then gradually stir in the milk and cook over medium heat, continually stirring until the mixture thickens and begins to bubble.
Remove from heat, whisk the egg yolks in a bowl, then slowly pour in a bit of the hot chocolate mixture to temper them, and once combined, whisk it all back into the saucepan and bring it back to low heat for about two minutes.
Remove from heat and stir in the butter and vanilla extract until fully melted and combined.
Pour the chocolate filling into the pre-baked pie crust, smoothing the top, and bake for an additional 15–20 minutes, or until the filling is set but still jiggly.
Allow the pie to cool completely, then refrigerate for at least 3 hours before serving, and garnish with whipped cream and chocolate shavings for an elegant touch.