Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
AI-Assisted Programming for Web and Machine Learning

You're reading from   AI-Assisted Programming for Web and Machine Learning Improve your development workflow with ChatGPT and GitHub Copilot

Arrow left icon
Product type Paperback
Published in Aug 2024
Publisher Packt
ISBN-13 9781835086056
Length 602 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Authors (5):
Arrow left icon
Marina Fernandez Marina Fernandez
Author Profile Icon Marina Fernandez
Marina Fernandez
Ajit Jaokar Ajit Jaokar
Author Profile Icon Ajit Jaokar
Ajit Jaokar
Anjali Jain Anjali Jain
Author Profile Icon Anjali Jain
Anjali Jain
Christoffer Noring Christoffer Noring
Author Profile Icon Christoffer Noring
Christoffer Noring
Ayşe Mutlu Ayşe Mutlu
Author Profile Icon Ayşe Mutlu
Ayşe Mutlu
+1 more Show less
Arrow right icon
View More author details
Toc

Table of Contents (25) Chapters Close

Preface 1. It’s a New World, One with AI Assistants, and You’re Invited FREE CHAPTER 2. Prompt Strategy 3. Tools of the Trade: Introducing Our AI Assistants 4. Build the Appearance of Our App with HTML and Copilot 5. Style the App with CSS and Copilot 6. Add Behavior with JavaScript 7. Support Multiple Viewports Using Responsive Web Layouts 8. Build a Backend with Web APIs 9. Augment Web Apps with AI Services 10. Maintaining Existing Codebases 11. Data Exploration with ChatGPT 12. Building a Classification Model with ChatGPT 13. Building a Regression Model for Customer Spend with ChatGPT 14. Building an MLP Model for Fashion-MNIST with ChatGPT 15. Building a CNN Model for CIFAR-10 with ChatGPT 16. Unsupervised Learning: Clustering and PCA 17. Machine Learning with Copilot 18. Regression with Copilot Chat 19. Regression with Copilot Suggestions 20. Increasing Efficiency with GitHub Copilot 21. Agents in Software Development 22. Conclusion 23. Other Books You May Enjoy
24. Index

Adding JavaScript

To add JavaScript to a web page, there are two main approaches:

  • Using a script tag in the head element: In this version, you would add a script tag to an existing HTML page, for example, named index.html.
    <!-- Alt1 -->
     <script>
      // JavaScript here
     </script>
    
  • Pointing to a stand-alone file: In this case, you would write your JavaScript in an existing file, my-javascript-file.js:
    <!-- Alt2 -->
    <script src="my-javascript-file.js"></script>
    

The role of JavaScript

When JavaScript was first added to the browser, it played the role of adding interactivity. Today, JavaScript often has an extended role as part of application frameworks like Vue.js, React, and Angular. In such frameworks, a developer can now declaratively define templates that, with the help of JavaScript, can be turned into static HTML, with event handling added, to respond to user interactions. Ultimately, all this is then sent to the browser to be rendered by it.

Adding JavaScript to a page

Let’s show next how we can add JavaScript to a page by providing the following prompt:

[Prompt]

Create web page with a script tag.

[End of prompt]

We end up with a web page that also contains a script tag. Note how the prompt is added as a comment at the top of the page. That’s how GitHub Copilot knows to suggest a text completion:

[Prompt response]

<!-- create web page with a script tag -->
<html>
<head>
    <title>My First Web Page</title>
</head>
<body>
    <script>
        alert("Hello World!");
    </script>
</body>

[End of response]

In this case, the code calls alert("Hello World"), which will show a dialog box with the text “Hello World.”

A second example: adding a JavaScript library/framework

The first example might not be all that useful, but let’s add something you’re likely to do, like adding a framework like Vue.js. The reason for adding a framework like Vue.js is that you want to build a large application consisting of a lot of interactivity and many different pages. To keep things organized, and make it easy for other developers to contribute, you want a framework like Vue.js.

[Prompt]

Add Vue 3.

[End of prompt]

This time, we place ourselves in the header tag. See the HTML comment in the HEAD tag below to indicate the prompt we’re using as input to GitHub Copilot:

<head>
<!-- add Vue 3 -->
</head>

This prompt results in the following suggestion:

[Prompt response]

<script src="https://unpkg.com/vue@next"></script>

[End of response]

You can easily build on this by adding a prompt that asks to set up an entry point (most SPA frameworks work on the idea of having an entry point, an element where the app will be rendered):

[Prompt]

Add Vue 3 entry point.

[End of prompt]

This results in the following code:

[Prompt response]

<!-- add Vue 3 entry point -->
<div id="app">
    <h1>{{ message }}</h1>
</div>

[End of response]

Placing our cursor below the script tag that imported Vue.js will lead to our AI assistant generating instructions for what code we should add to further set up Vue.js. It’s not enough to just add script to Vue.js; some additional code is needed.

Luckily, with our cursor placement like below, GitHub Copilot understands from existing HTML comments (prompts) what code is missing and will suggest that code at the location of our cursor (if a Copilot suggestion doesn’t appear, try pressing the Spacebar key or the Return key):

[Prompt]

No prompt given

[End of prompt]

[Prompt response]

<script src="https://unpkg.com/vue@next"></script>
<!-- below is rendered by just placing cursor here -->
<script>
    // create Vue 3 instance
    const app = Vue.createApp({
        data() {
            return {
                message: 'Hello World!'
            }
        }
    })
    // mount Vue 3 instance to DOM element
    app.mount('#app')
</script>

[End of response]

It’s worth calling out again that GitHub Copilot had enough context to generate more suggestions by us only using Tab and Return key without the need to provide an extra prompt..

Running this in a browser returns “Hello World,” which is what’s expected given the above code. The reason this code works, in case you’re not familiar with Vue.js, is that it contains the bare minimum of what’s needed for it to be referenced and configured:

  • script tag, referencing Vue.js
  • Creation of an app instance that also defines a data function with a message property
  • A call to mount(), which ensures that the identified HTML tag (with id = app) is interpolated, and the HTML tag is replaced with the value of the property message

When building a more advanced Vue.js project, please use the Vue CLI over the approach chosen above. For smaller proofs of concept, this approach could be OK.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image