This is an old revision of the document!


Lab 02 - Teletypes & frame buffers

Objectives

  • Getting used to image manipulation with PIL
  • Learn about Teletypes and Pseudo-Terminal Slaves
  • Interact with the frame buffer

Contents

Proof of Work

Today we're going to start off by reinventing the wheel, a little bit. The first exercise will have you reimplementing the screenshot functionality in a Python3 script. Before you start gleaming with joy, thinking that there's probably a module / library out there doing just this, know that 1) you're probably right and 2) we won't be using them :). In stead, we'll take the hard road and fetch our pixels right from the frame buffer… Now that I'm thinking about it, when you consider the state of pip and module dependency hell in Python, which one even is the hard road ultimately?

Anyway, in the second half of the lab, we'll do the exact opposite of what we'll have done in the first. In stead of taking a screenshot, our second script will overwrite the frame buffer (and consequently what's being displayed on our screen) with the content of a randomly selected image.

The submission for this lab must contain the two scripts, a screenshot taken with the first script demonstrating that the second script works, and optionally proof that you've completed the feedback form (for the bonus 10p). As usual, check out the moodle assignment. The deadline is set for 23:55 this Sunday evening. Everyone must upload their work, regardless of whether they were present for the lab or not.

Tasks

00. [10p] Getting Started

First, download the skeleton archive (.zip) and unzip it.

It has the following structure:

├── initial_design.html  # initial HTML template
├── public/
│   ├── bootstrap/ # bootstrap sources
│   ├── images/
│   └── style.css  # main stylesheet
├── server.py      # server-side application
└── templates/     # Jinja templates

To test, open initial_design.html in a browser. It should look similar (almost: minor text differences) to the following screenshot:

Our website uses the Bootstrap frontend library – a CSS framework for building responsive web designs! Check out its documentation for a list of features.

Also, there are other open-source CSS toolkits, google them before starting a website and choose something you like before starting a design project.

Also, it would be a good idea to test your Python / Flask setup now:

python3 server.py
# it should say that the server is running on http://127.0.0.1:5000/

Note: the initial Python Flask app doesn't display this webpage just yet, it will only give you a TODO message! You will make it yourself later this lab :P

01. [30p] Minor Design Changes

Our customer wants to make some changes to the website's design:

  • Add a header image with our logo at the top
    • Several candidates are present inside public/images/;
    • You could either use the CSS background property, or just the old-school <img> tag;
    • Hint: check out style.css for existing definitions!
  • Also change the background color of the <header> to match the chosen image on its margins (maybe something blue?);
  • Make the content box have rounded borders (try 15px);
    • border-radius *wink*
    • Use the browser's Web Developer tools to find the HTML tag to select!
  • Insert some dummy content text (e.g., use a Lorem Ipsum generator);

Hint: search for TODOs inside HTML and CSS!

For specifying URLs to other web resources (e.g., images for this task): recall Unix relative paths (./path/to/file.jpg)? HTML also uses them (those URLs will be relative to your current file – either the .html or the .css)!

In some cases (webpage has sub-paths, e.g., /account/details.html), you may also use absolute URLs (path begins with a / representing the server's root directory).

02. [20p] Hyper-linking multiple pages

Time to add a second HTML page:

  • As expected, copy + paste your initial HTML with a new name, e.g.: second.html;
  • Link together the two .html pages (in each HTML page, find the <a class=”…” href=”..”> links inside the #navbarToggle div and edit the hrefs to point to each-other);

Use relative paths, either pagename.html or ./pagename.html will do! DO NOT USE: absolute paths, e.g., C:\Users\…\pagename.html for obvious portability reasons!

  • Test by opening the HTML files in your browser. The user should be able to navigate between the two pages seamlessly!
  • One final touch: we want the menu to highlight (i.e., change foreground / background color and font style) the currently active menu button (different for each page)!

Hint: create a CSS class (e.g., .active) with your desired properties and append it to the appropiate menu link element: <a class=“nav-item nav-link” …> (also note: different class attribute values are separated by space!).

03. [30p] Firing up the Flask backend

First, let's understand how Flask serves files:

  • Download a random photo from the Internet (e.g., your favorite snake species) and place it into public/myimage.jpg (or use whatever name you wish, but keep this directory!);
  • Start up the Flask server (python3 server.py) and point your browser to http://localhost:5000/public/myimage.jpg, it should display your image.
  • Copy/rename or download another image directly to your project's root, alongside server.py; start the web server, point your browser to http://localhost:5000/myimage2.jpg – it won't work… why?

Now, we want to move the html page from earlier tasks to be served by the Flask web server:

  • Copy the initial_design.html to the templates/ directory (we're using the default Flask convention);
  • Modify server.py to call render_template (see official quickstart guide) and actually serve your HTML file.
  • Fire it up!

Don't forget to import the render_template symbol from the Flask library!

  • Finally for this task, make a /google endpoint which automatically redirects to your favorite (or not) search engine!

04. [10p] Serving dynamic content

Finally, we shall see how dynamic content can be interpolated

  • Edit the website's second HTML page, enter {{mycontent}} (this literal text!) as content somewhere inside the main content block:
    <div class="content-box main-content">
    ...
      {{mycontent}}
    ...
  • Now modify the server.py code (the second_page function) to take in the desired value as a request parameter and pass it to the mycontent template variable, e.g.:
    def second_page():
      render_template("second.html", mycontent=request.args.get("mycontent", "<not specified>"))
  • Open the endpoint URL and pass the mycontent value as a GET URL parameter (e.g., ?parameter=value) and see if the content changes dynamically after the request!
  • This is a template engine called Jinja, seamlessly integrated with Flask. More about this: next time!
ii/labs/s2/02.1679081168.txt.gz · Last modified: 2023/03/17 21:26 by florin.stancu
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0