This is a sample Python and Flask application designed to show how easy it is to use Visual Studio Code and GitHub Codespaces.
The Python code is of the Tower of Hanoi puzzle that is often used to teach recursion in computer science courses.
Your instant, ready to go, development box available anywhere, anytime, any device with Visual Studio Code in the browser or your machine with two extensions and nothing else.
The presentation in the presentation folder gives a brief overview and if you have five minutes to spare check out my video on it.
- Request access to Codespaces for your github account. Access should be given in a few hours.
- After getting access to Codespaces; login to github and navigate to https://github.com/klabranche/pytoh.
- Select
Use this template. While you can create a Codespace right from<> code,use this templatewill create a copy of the repository in your profile.
- Fill out the form and select
Create repository from template. - On your new repository select the
<> Codemenu; thenCodespacestab; thenCreate codespace on main.
Prerequisite: Create your Codespace by following the See it in action section.
The default action when creating or starting a Codespace from GitHub is to use the browser.
With your Codespace running:
- In the terminal (CTRL+Shift+`) type
python console.pyto run the command line version. - In the terminal type
flask runand either click on the url in the text* Running on http://127.0.0.1:5000 (Press CTRL+C to quit)or select theOpen in Browserin the popup message to run the web application version. - In the terminal press
CTRL+Cto stop the web application. - Close the tab that was running Visual Studio Code in the browser.
Now stop the Codespace:
- Go back to your repository in GitHub; Select the
<> Codemenu; thenCodespacestab; thenManage all. - Select
...to the right of the Codespace; SelectStop Codespace.
We stopped the Codespace for two reasons:
- To ensure we are only charged for what we use. At this time, individual accounts aren't charged but it's still good practice to stop the Codespace when you are done. By default, it will automatically turn off after 30 minutes of inactivity.
- To ensure the port we opened for Flask is ready for our next example.
👍 No installs, no setup and configuration. It just ran. That's what Codespaces with Visual Studio Code in the browser does for you.
Visual Studio Code on your machine requires two extensions to work with GitHub Codespaces:
You will be prompted to install the extensions when opening a Codespace if they are not installed.
You will be prompted to login into GitHub if you have not already done so in Visual Studio Code.
- In Visual Studio Code open the command palette by typing
CTRL+SHIFT+P; Typecodespacesand selectConnect to CodeSpaces. Select the Codespace from the drop down list.- Every Codespace created by GitHub is given a random name. Select the randomly named Codespace;
With your Codespace running:
- Open the terminal (CTRL+Shift+`); type
python console.pyto run the console sample. - In the terminal; type
flask runand either click on the url in the text* Running on http://127.0.0.1:5000 (Press CTRL+C to quit)or selectOpen in Browserin the popup message to run the web application version.- If you get:
- Address already in use Port 5000 is in use by another program. Either identify and stop that program, or start the server with a different port. OR
- 504 Gateway Timeout. The port forwarding timed out while trying to create a connection.
- Stop the Codespace and restart it. This is due to running the Flask example in another session for the same Codespace.
- If you get:
- In the terminal; press
CTRL+Cto stop the web application. - To stop the Codespace session; click
Codespacesin the lower left hand corner of Visual Studio Code. SelectStop current codespacein the drop down menu opened by the command palette.
You can also use Visual Studio Code's command palette (CTRL+Shift+P) to manage your Codespaces. To see all the available options type codespaces in the command palette window.
You can debug with the browser and desktop version of Visual Studio Code with Codespaces.
- Select
console.pyfile in the explorer (left hand icon menu).
- Put a breakpoint on line 11 by clicking to the left of the line number.
- Select Run and Debug (left hand icon menu).
- Select
Python: Current Filein the drop down menu at the top right of the Run and Debug window.
- Press
F5to start a debugging session. - At the prompt in the terminal enter something other than a number.
- The breakpoint should now be hit. Press
F5to continue.
- Select
app.pyin the explorer (left hand icon menu). - Put a breakpoint on line 9 by clicking to the left of the line number.
- Select Run and Debug icon (left hand icon menu).
- Select
Python: Flaskin the drop down menu at the top right of the Run and Debug window. - Press
F5to start a debugging session. - Either click on the url in the text
* Running on http://127.0.0.1:5000 (Press CTRL+C to quit)or selectOpen in Browserin the popup message to run the web application version of the Tower of Hanoi sample. - The web page should now be spinning without anything showing yet. That's because the breakpoint has been hit before we have returned the home page.
- In Visual Studio Code; press
F5to continue and the home page should now load. - Press
Shift+F5to end the debugging session.
By default, ports used by the container are private to your session only. However, you can make it public to show your work to others. This is ideal for a simple demo.
❗ Don't do this for anything other than a demo. This is not meant for production work or replacement of a server or web hosting.
- Select
PORTStab that is in the terminal window. - Right click port 5000; select
Port Visibilityand thenPublic.
- Select
TERMINALtab again and then type at the command promptflask run.
Now when you open the browser you will have a new url. You can share this URL with others.
How is this working? The GitHub Codespace is in the cloud and has setup this link to the outside world with the container. Remember, the connection is to the container in the cloud not your local computer.
Let's go ahead and undo that now. It's great for a quick pinch demo but we don't recommend it for much more than that.
- Select
PORTStab that is in the terminal window. - Right click port 5000; select
Port Visibilityand thenPrivate.
The default container from GitHub has an assortment of compilers, runtimes and tools on it. For example, console.py would run immediately without any customization since Python is installed on the default container.
Flask, however is not. In order for app.py to run without any extra manual steps we created the devcontainer.json file in the .devcontainer folder.
We added a post create command to run pip to install our requirements.txt. Line 50 of the devcontainer.json reads:
"postCreateCommand": "pip3 install --user -r requirements.txt",
This installs Flask whenever a new container is created.
Creating this file in Visual Studio Code is a breeze by opening the command palette (CTRL+Shift+P) and typing remote-container: Add Development Container Configuration Files and follow the prompts.
There is a lot more you can do with customizing your container.
You can delete the Codespace in GitHub or in Visual Studio Code.
⚠️ Deleting a Codespace deletes all work that was being done in the container. Be sure topushyour changes up to GitHub before deleting a Codespace.
- Select the
<> Codemenu. - Select
manage all. - For each Codespace in the list of Codespaces you want to delete select
...menu and then selectDelete.
- Click
Codespacesin the lower left hand corner of Visual Studio Code.
- Select
Stop Current Codespacein the drop down menu opened by the command palette. - Click
Codespacesagain; SelectDelete Codespacein the drop down menu opened by the command palette. - Select the codespace you want to delete from the drop down list.
- Select
deletewhen asked to confirm deletion.
There is so much more to learn about Visual Studio Code and GitHub Codespaces. To learn more check out GitHub Codespaces docs.
See this application running in Azure's free website as a service tier.
Check out Azure web app for containers to learn more about deploying containerized web apps.
Azure App Service Docs is a great jumping off point to learn more about Azure App Services.






