Sns 1 Design A Simple Web Page
Sns 1 Design A Simple Web Page
EX.NO: 01
DESIGN OWN SOCIAL MEDIA APPLICATION
DATE:
AIM:
To implement social media application.
OBJECTIVE:
SOFTWARE REQUIRED:
Python, Flask
ALGORITHM:
Step1: Create a new directory for your project. Inside this directory, create the following subdirectories
and files.
Step2: Open a terminal and navigate to your project directory. Create a virtual environment (optional but
recommended), activate it, and install Flask
Step3: Flask : the web framework used for building the web application. render_template : A function
from Flask that renders HTML templates. Graph, Namespace, Literal, URIRef : These are classes from
the rdflib library, used for working with RDF (Resource Description Framework). RDF is a framework
for representing information about resources on the web.
Step4: create an instance of the Flask class, representing the web application
Step5: social_graph: An instance of the RDF Graph used to store social data. FOAF: A Namespace
object representing the Friend of a Friend (FOAF) vocabulary. FOAF is commonly used for describing
people and relationships on the web.
Step6: URIRef: Represents a URI reference. Sample user data is added to the RDF graph, including user
URIs and their names.
Step7: Adds a friendship relationship between user1 and user2 in the RDF graph.
Step8: Defines a route for the root URL (/). When a user accesses this URL, the index function is called.
The index function retrieves a list of users from the RDF graph and renders the 'index.html' template,
passing the users, social graph, and FOAF namespace to the template.
Page No.:
Step9: Defines a route for the '/profile/<user_id>' URL pattern. The <user_id> part is a dynamic
parameter. The profile function takes the user_id as a parameter, retrieves the user's information from
the RDF graph, and renders the 'profile.html' template, passing the user's name, friends, social graph,
and FOAF namespace to the template.
Step10: Checks if the script is being run directly (not imported as a module). If so, it starts the Flask
development server with debugging enabled.
Step11: Stop
PROGRAM:
index.html
<!-- templates/index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Social Media App</title>
<link rel="stylesheet" href="{{ url_for('static', filename='styles.css') }}">
</head>
<body>
<div style="text-align: center;">
<h1>Users</h1>
</div>
<hr>
<div class="image-container">
{% for user in users %}
<a href="{{ url_for('profile', user_id=user.split('/')[-1]) }}">
<img src="https://tse1.mm.bing.net/th?
id=OIP.eoBtu339Epu84pJA0EY_QwAAAA&pid=Api&P=0&h=180" alt="User Image"
class="avatar">
<div class="overlay">{{ social_graph.value(user, FOAF.name) }}</div>
</a>
{% endfor %}
</div>
</body>
</html>
profile.html
<!-- templates/profile.html -->
Page No.:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>User Profile</title>
<link rel="stylesheet" href="{{ url_for('static', filename='styles.css') }}">
</head>
<body>
<h1>User Profile</h1>
<p>Name: {{ user_name }}</p>
<h2>Friends</h2>
<ul>
{% for friend in friends %}
<li>{{ social_graph.value(friend, FOAF.name) }}</li>
{% endfor %}
</ul>
</body>
</html>
styles.css
/* static/styles.css */
body {
font-family:'Times New Roman', Times, serif;
margin: 20px;
background-color:aliceblue;
}
h1, h2 {
color: #333;
}
ul {
list-style-type: none;
padding: 0;
}
li {
margin-bottom: 10px;
}
/* Define a basic styling for the image container */
.image-container {
display: flex;
Page No.:
justify-content:space-evenly;
max-width: 800px; /* Adjust the max-width based on your design */
margin: auto; /* Center the container */
}
Page No.:
.image-container a {
text-decoration: none; /* Remove underlines from links */
color: inherit; /* Inherit text color from the parent */
}
python.py
# Install necessary libraries
# pip install Flask rdflib
app = Flask(__name__)
# Define Namespace
FOAF = Namespace("http://xmlns.com/foaf/0.1/")
@app.route('/')
def index():
# Display a list of users
users = social_graph.subjects(predicate=FOAF.name)
return render_template('index.html', users=users, social_graph=social_graph, FOAF=FOAF)
Page No.:
@app.route('/profile/<user_id>')
def profile(user_id):
# Display user profile and friends
user = URIRef(f"http://example.com/users/{user_id}")
user_name = social_graph.value(user, FOAF.name)
friends = social_graph.objects(subject=user, predicate=FOAF.knows)
return render_template('proile.html', user_name=user_name, friends=friends,
social_graph=social_graph, FOAF=FOAF)
if __name__ == '__main__':
app.run(debug=True)
OUTPUT:
Page No.:
RESULT:
The Program to design the social media application using python was executed successfully.
Page No.: