Let's say I have this index.html file in GitHub Codespaces:
<h1>Hello, World!</h1>
Test text
How can I preview the HTML file in GitHub Codespaces? Preferably in a Visual Studio Code tab.
Let's say I have this index.html file in GitHub Codespaces:
<h1>Hello, World!</h1>
Test text
How can I preview the HTML file in GitHub Codespaces? Preferably in a Visual Studio Code tab.
Just to complete the answer of maddes8cht so you understand better.
GitHub Codespaces (aka github.dev) creates a virtual space for your repository, so you can edit and commit your files. It's really just a remote instance of Visual Studio Code, but it doesn't by default serve your files on an endpoint URL.
The good news is, because it's a virtual space (located somewhere on GitHub servers) you can run any command like you'd do on your local machine.
That means yeah you can run a server there, and it's smart enough to know you've just run a server and does "port forwarding" for you (again it means that you can access this server remotely as you would in a localhost development scenario.)
Here's a simple solution:
Open the terminal, if not already opened (Ctrl + Shift + P to open the Command Palette, and then type "toggle terminal" and Enter to select)
Type npm i -g http-server in the newly-opened terminal (this will install the tool to easily create a server) 1
Next, you have to run the server. Type http-server in the same terminal.
You'll then be prompted with this notification
You can click Open in Browser and see the result there. If your index.html file is at the root of your project, it will be opened by default, or else you'll need to write the correct path in the URL bar.
Maybe you want the page to automatically refresh when you update your file. In the example above we installed http-server which is just a plain server that serves static files.
If you need hot reload you can install a server that supports it. Here are a few: vite, @web/dev-server, browser-sync, ...
1 Note that there are many other tools for creating development servers. This one is just serving static files and suffices if you only want to preview your files.
python -m http.server instead of npm and it worked.Do you mean a simple preview of an HTML file?
Go into your codespace and install any of the HTML preview extensions in the extensions sidebar, for example the preview extension by Haixin Chen.
With this, you can right-click your HTML in the file explorer and chose open preview.
This should work out of the box. For me it did.
You can also try to use the Microsoft Live Preview extension. As this is targeted at giving a preview provided by a web application running in your codespace, it requires a little bit more work, but if done, it can also preview simple HTML files.
First, install the extension.
For me, sometimes it only works after restarting the codespace (it doesn't allow me to be activated the first time - don't know why)
As you can see in the screenshot, it gives you a nice preview Icon on top of your editor window. Pressing it will most likely give you an empty / broken preview as seen on the left.
That is because you have to tell it how to access the codespaces webserver. You can find the necessary settings in the PORTS console in the Terminals section.
Depending on internals, you will find several ports preconfigured, but only one of them will give you a preview of your HTML content. Right-click on each line and try preview in editor window. When there is an index.html file in the root of your workspace, it will show up, or else it will show a directory index which allows you to navigate your workspace.
When you now again press the preview button of an HTML file in an editor window, it will show that page in a new preview window.
You can preview a single file, or an entire (vanilla) website via the PHP server.
It's already installed by default in GitHub Codespaces.
php -S localhost:3006 -t ./
And you can set a task (Visual Studio Code) for fast execution (an example).
previewextension by Haixin Chen. With this, you can right click your html in the file explorer and choseopen preview.