DEV Community

loading...

Build a web app that gets a random meme from Reddit

Madhusudhan
・3 min read

Hey folks,

In this article, I will be showing you how to build a very simple web app that makes use of PRAW(Python Reddit API Wrapper) to fetch a random meme from r/memes subreddit.

By understanding how this works, you will be able to build your own customized apps based on your interests such as fetching a random horror story from r/nosleep or fetching cute pictures of pets from r/aww and the possibilities are endless.

So, what are you waiting for? LET'S GET STARTED !

STEP 1: Create a Reddit App

Reddit provides an API to scrape its contents which allows up to 30 requests in a minute. And PRAW is an acronym for 'Python Reddit API Wrapper' which is basically a python package that makes it easy to access reddit API. In order to be able to use the reddit API, we will first have to create a reddit app in our reddit account.

  • Log into reddit and visit https://old.reddit.com/prefs/apps/
  • Click on Create App and fill the necessary details.
  • Once the app has been created, you can use the tokens that has been generated for the app in your program.

STEP 2: Install necessary packages

  • You will first need to install PRAW package before you can use it to access Reddit API.
  • And you will need to install a python web framework since we are building a web app. I have decided to use "Bottle". It is a very simple and lightweight web framework for python that is easy to use.

Installing any package in Python is a Walk In the Park using pip python package manager. So fire up your terminal,

To install PRAW:

pip install praw 

To install Bottle:

pip install bottle 

Step 3: Time to CODE

  • Create a new python file called "app.py". First and foremost, we import the packages.
from bottle import route, template, run
import praw
  • Next, we create an instance of reddit. The tokens obtained in Step 1 while creating the reddit app will be used here.
reddit = praw.Reddit(client_id='your_app_id',
                     client_secret='your_app_secret', 
                     password="your_reddit_password",
                     user_agent='your_app_name',
                     username='your_redditusername')

Be sure to fill in your reddit account and app details.

  • Define the route and the logic for that route
@route('/')
def hello_world():
    subreddit = reddit.subreddit("memes")
    meme = subreddit.random()
    return template("<img src={{submission}}>",submission=meme.url)

This piece of code makes sure that whenever the user requests the route "/" (homepage), @route('/') , it invokes this function hello_world().
subreddit = reddit.subreddit("memes") creates a subreddit instance of r/memes.
meme = subreddit.random() fetches a random post submitted in r/memes.
return template returns a html template and therefore since we want to view the image i.e meme, i have passsed a img tag and since meme.url contains the url of the uploaded meme, we are passing that to the src attribute.

  • Serve the webpages in localhost environment
run(host='localhost', port=8080, debug=True)

The overall app.py should look like this:

from bottle import route, template, run
import praw

reddit = praw.Reddit(client_id='your_app_id',
                     client_secret='your_app_secret', 
                     password="your_reddit_password",
                     user_agent='your_app_name',
                     username='your_redditusername')

@route('/')
def hello_world():
    subreddit = reddit.subreddit("memes")
    meme = subreddit.random()
    return template("<img src={{submission}}>",submission=meme.url)

run(host='localhost', port=8080, debug=True)

Now lets run this program. Open your terminal in the directory that contains app.py and type

python app.py

Alt Text

Your website is now ready and is available at http://localhost:8080
You should now see a new meme every time you reload it.

Alt Text

Alt Text

Thats all folks ! This was a very simple usage of PRAW. Dive deep into PRAW and you will be able to make many interesting apps, or to make a full fledged website and moreeeeeeeeeeee !

Discussion (3)

Collapse
nirupanbhandari profile image
Nirupan Bhandari

It says Only scripts may use password auth. Please Help

Collapse
thomassahil profile image
ThomasSahil

If I wanted to style the website how exactly would I do it?

Collapse
thereal_madhu profile image
Madhusudhan Author

If u want to style that website, you will need to learn CSS