Lesson №3 "Code Generation with ChatGPT." Part 2
In the previous lesson, we explored how ChatGPT can assist us in generating code. In this lesson, we will continue to expand our knowledge and skills, using ChatGPT to create and style a button in our web application.
Let's start by looking at this process using an html example:
Creating a Button
To begin with, we need to create a button. Let's ask ChatGPT how to create a simple button in HTML. In this case, the request might be something like: "ChatGPT, how can I create a button in HTML?" ChatGPT will provide you with a sample code that you can use:
![](https://academy.bothub.chat/~gitbook/image?url=https%3A%2F%2F304182736-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252FbLgcwVoFtnJ1fCeF9OHz%252Fuploads%252Frt7lMXJyqo6ls4aatAwT%252F1.png%3Falt%3Dmedia%26token%3Db559fe20-cf88-40f0-bf7b-ff8412e5789c&width=768&dpr=4&quality=100&sign=fc92075d&sv=1)
Insert this code into your HTML file and refresh the page. Now you will see a button on your website.
Button Styling
Now that we have a button, let's work on its style. We want to change the color of this button to blue. Let's ask ChatGPT how to do that. The request could be something like: "ChatGPT, how can I change the button color to blue in HTML?"
ChatGPT will again provide us with a sample code:
![](https://academy.bothub.chat/~gitbook/image?url=https%3A%2F%2F304182736-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252FbLgcwVoFtnJ1fCeF9OHz%252Fuploads%252Fx5h27W19eE7LIzAObHws%252F2.png%3Falt%3Dmedia%26token%3De23e10fb-2577-4d09-aad5-cd31aa73685c&width=768&dpr=4&quality=100&sign=56aaedd7&sv=1)
Insert this code into your HTML file and refresh the page. Now your button is blue. If you want to use a different shade of blue, you can continue to consult with ChatGPT, and it will suggest various color code options for you.
Now let’s see how we can achieve the same in React:
Creating a Button in React
Let’s start with creating a button. We can ask ChatGPT how to create a simple button in React. In this case, the request might be something like: "ChatGPT, how can I create a button in React?"
ChatGPT will provide you with a code example that you can use:
![](https://academy.bothub.chat/~gitbook/image?url=https%3A%2F%2F304182736-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252FbLgcwVoFtnJ1fCeF9OHz%252Fuploads%252FhTmpwsL1NmigFD5RaLVZ%252F3.png%3Falt%3Dmedia%26token%3D47e1f05c-229f-4b0e-8019-b3f405504b81&width=768&dpr=4&quality=100&sign=2e600e1f&sv=1)
![](https://academy.bothub.chat/~gitbook/image?url=https%3A%2F%2F304182736-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252FbLgcwVoFtnJ1fCeF9OHz%252Fuploads%252FfPx2IeZf1RoencWBlf9W%252F3-1.png%3Falt%3Dmedia%26token%3D2bd4d7f7-d991-485b-ac00-e90f7c1dcb36&width=768&dpr=4&quality=100&sign=e05ce8d9&sv=1)
Insert this code into your React file. Now you will see a button on your web application.
Button Styling in React
Now that we have a button, let's work on its styling. We want to change the color of this button to blue. Let's ask ChatGPT how to do it. The request might look something like this: "ChatGPT, how can I change the button color to blue in React?"
ChatGPT will then provide us with a code example:
![](https://academy.bothub.chat/~gitbook/image?url=https%3A%2F%2F304182736-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252FbLgcwVoFtnJ1fCeF9OHz%252Fuploads%252FTorMTV7PxwvgsBGt40qs%252F4.png%3Falt%3Dmedia%26token%3Db6138f8b-5077-4ff0-adf1-ec2df8202cc0&width=768&dpr=4&quality=100&sign=597ac640&sv=1)
Insert this code into your React file. Now your button is blue. If you want to use a different shade of blue, you can continue to consult with ChatGPT, and it will suggest various color code options for you.
It's important to remember that ChatGPT does not maintain context between different sessions, so if you want to continue the discussion with the context preserved, make sure you do it in the same session.
And now, try it out yourself!
Practical Assignment
Task: Try creating a React web application with two buttons of different colors, using ChatGPT to generate the code.
Instructions:
Ask ChatGPT to explain how to create an application named my-app using vite.
Create a new React application using the command "npm create vite my-app" in the command line.
Choose React and then select JavaScript.
In the terminal, enter "cd my-app".
Next, enter "npm install" and "npm run dev".
Use ChatGPT and give it the request, "Add two buttons 'Hello' and 'Goodbye' to the main screen of the application."
![](https://academy.bothub.chat/~gitbook/image?url=https%3A%2F%2F304182736-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252FbLgcwVoFtnJ1fCeF9OHz%252Fuploads%252FDaVTMpjpqHnNK9oTv8VU%252F5.png%3Falt%3Dmedia%26token%3Daf905f77-8913-4ce9-bf5d-cf9cce0e43b5&width=768&dpr=4&quality=100&sign=eadd9d5c&sv=1)
![](https://academy.bothub.chat/~gitbook/image?url=https%3A%2F%2F304182736-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252FbLgcwVoFtnJ1fCeF9OHz%252Fuploads%252FMJBaPqcpXd0jHegmta6f%252F5-1.png%3Falt%3Dmedia%26token%3D060a4b8d-81e2-48b7-80f4-3308b3d5a4d9&width=768&dpr=4&quality=100&sign=8fec02bb&sv=1)
And then ask them to make one button green and the other one red.
![](https://academy.bothub.chat/~gitbook/image?url=https%3A%2F%2F304182736-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252FbLgcwVoFtnJ1fCeF9OHz%252Fuploads%252FTMIPkaCmkWoQwkfhWI7f%252F6.png%3Falt%3Dmedia%26token%3Da6ec9058-082b-4099-b622-8f243daedc2e&width=768&dpr=4&quality=100&sign=95712b47&sv=1)
If necessary, ask him for clarification.
Next, we'll ask ChatGPT to make it so that when you click on the first button, it displays the text "Hello", and on the second button, it says "Goodbye":
![](https://academy.bothub.chat/~gitbook/image?url=https%3A%2F%2F304182736-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252FbLgcwVoFtnJ1fCeF9OHz%252Fuploads%252FXH2YZ83hCITZGZxXUwgS%252F7.png%3Falt%3Dmedia%26token%3Ded9e636a-b414-4039-acb1-61a039103e63&width=768&dpr=4&quality=100&sign=fc84aacb&sv=1)
![](https://academy.bothub.chat/~gitbook/image?url=https%3A%2F%2F304182736-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252FbLgcwVoFtnJ1fCeF9OHz%252Fuploads%252FNeVk01blV1kJI9jfEz83%252F7-1.png%3Falt%3Dmedia%26token%3Dc0fe6e26-0e81-4901-b942-694e65ab3fd3&width=768&dpr=4&quality=100&sign=69eaf7be&sv=1)
In this example, we created two buttons: one blue and one red. Remember that your buttons can be any color that you choose.
Conclusion
As we have seen, using Chat GPT allows for the generation of code, suggesting styles and solutions for website creation, acting as a kind of developer's assistant. And although the chatbot can sometimes make mistakes, it can be an invaluable tool when used properly and when the right questions are asked.
Last updated