Hello readers! Welcome back to our #90DaysOfDevOps Series. Ready to embark on a journey that combines the power of web development and version control? This tutorial will guide you through building a simple web application, managing code changes with Git, and mastering the power of branching, merging, and remote repositories. Get ready to unleash your inner developer!
Prerequisites:
Basic knowledge of Python
Access to an Ubuntu server
Installed SSH client (e.g., PuTTY)
Git installed on your server
Setting Up the Environment:
Connect to your Ubuntu server: Use your preferred SSH client to connect to your server.
Install Python and Flask: Run the following commands to install Python and Flask:
sudo apt update
sudo apt install python3 python3-pip
pip install Flask
Building Your First Flask App:
1. Create a project directory: Create a new directory for your project:
mkdir my-app && cd my-app
2. Initialize a Git repository: Start tracking your code changes with Git:
git init
3. Create the application file: Create a file named app.py and add the following code:
from flask import Flask, render_template
app = Flask(__name__)
@app.route("/")
def index():
return render_template("index.html")
if __name__ == "__main__":
app.run(debug=True)
4. Create the template file: Create a file named index.html and add some basic HTML:
<!DOCTYPE html>
<html>
<head>
<title>My First Flask App</title>
</head>
<body>
<h1>Welcome to my awesome app!</h1>
</body>
</html>
5. Run the application: Start your Flask app to see it in action:
python app.py
Open your web browser and navigate to http://localhost:5000 to see your "Welcome to my awesome app!" message.
Git in Action:
Add files to staging: Tell Git about the files you want to track:
git add .
Commit your changes: Save your work with a descriptive message:
git commit -m "Initial commit"
Create a remote repository: Create a new repository on GitHub or use the existing if you have one:
git remote add origin https://github.com/v-pundit/myfirstrepo.git
Push changes to remote: Share your local work with the world:
git push -u origin master
Branching, Merging, and Collaboration:
Create a branch: Create a new branch to work on a new feature:
git checkout -b my-new-feature
Work on your feature: Make changes to your code in the my-new-feature branch.
Commit your changes and push to remote: Share your feature branch with the remote repository:
git add .
git commit -m "Added new feature"
git push origin my-new-feature
Merge your feature branch: Combine your changes with the main branch:
git checkout master
git merge my-new-feature
git push origin master
Congratulations! You have successfully built a simple web application with Flask and learned the fundamentals of Git on Ubuntu. Remember, this is just the beginning.
Explore branching strategies, learn about virtual environments, and dive deeper into the world of web development!
Thank you for reading!
*** Explore | Share | Grow ***
コメント