python_icon

Create Interactive Websites with Python: A tutorial

Python is a popular coding language widely used by programmers for activities ranging from high-level and general-purpose coding to dynamic programming. Simply put, Python is a top choice because of its clear programs, code readability, and convenience, all of which add up to fewer lines of code required for programming. Its popularity and wide-spread use makes it important for all programmers to have at least introductory knowledge of the code. Throughout this tutorial you’ll learn various approaches to and uses for Python programming.

How to Run Python Code on Your Host/Web Server

Before you run Python code on a hosted server, you’ll need to determine two things. First, does the server support Python programming? Second, which version of Python should be used? The vast majority of servers provide support for Python, and many apps currently operating work excellently with Python 2.6.x or 2.7.x versions. The latest version of Python, 3.4.3, was released in May 2015, but it is not fully backwards-compatible with all Python 2.x features, whereas the Python community still offers deep support for Python 2.x because so many programmers and apps rely on it.

If you’ve determined that your server works with Python, it’s time to move forward with installation. Should you opt for Python 3, you’ll want to start by transferring the compression version of the files to your server and decompressing them. This creates a directory that automatically decompresses all of Python’s files into the appropriate spots.

With the files installed, you can unpack and get Python programming operational by running the command “make,” followed by “install.” This sets up your configuration tools and installs Python utilities that included the recommended installation packages and enables you to download, build, and uninstall Python programming packages.

As you wrap up the process of installation, it is important to add a path to your server environment. It is recommended that you use vi or Emacs, nano, or other pure text editors.

Programs such as Microsoft Word, LibreOffice Writer, and other word processing platforms add formatting codes that interfere with the successful use of Python for the web.

Last but not least, you may want to consider creating a virtual Python environment on your server. You can install virtualenv for all the users on your server or restrict access to the current user only. After you create and enter a new directory, you can name your Python environment and begin programming with Python.

The following is an example of the quick steps to create and edit Python script in cPanel:

   Login
   Go to Files => File Manager
   Chose to run script in cgi-bin folder or outside
   To create new scripts, click New File => specify the name and required extension, click “Create New File.”
   Change permission to 0755
   Open file, click “Edit,” and add your desired code.

How to Use Python to Create Simple Automatic Web Pages or Posts

Your life can quickly become consumed with endless web searches and post updates if you don’t make use of automatic searches with Python programming. Python for the web makes it possible to scrub the Internet for relevant posts, whether its news or sports related for your site, to gather and automatically post the information you want to share. The question is, how do you use Python programming to create those automatic web pages or posts?

First and foremost, there are a few important modules to familiarize yourself with. Each of the following modules enables easier web scraping and automatic creation with Python:

  • Webbrowser: comes with Python and opens a browser to a specific page.
  • Requests: downloads files and web pages from the Internet
  • Beautiful Soup: parses the HTML formatting language that web pages are written in.
  • Selenium: launches and controls a web browser, and is able to fill in forms and simulate mouse clicks in this browser.

While the latter two, Beautiful Soup and Selenium, offer a viable means for creating automatic web pages or posts, they are considered more complex than webbrowser and requests. For the sake of this tutorial, the focus will remain on the simpler processes involved in Python programming using webbrowser and requests.

The webbrowser module is the simplest option for creating automatic web pages or automated posts, but it also has the most limited functionality. As long as you have a URL, webbrowser allows you to cut out the step of opening a browser and arriving at a website. For example, you could use webbrowser to cut out various steps in launching addresses in a map service, such as Google Maps. Rather than highlighting an address, copying, opening a web browser, navigating to a page, interacting with the text field and pasting the address, webbrowser automates the process.

With webbrowser, you can establish a Python programming code that automates the process after you highlight and copy the address. By running a

mapIt.py

code, the rest of this process is automated and completed without further effort on your behalf.

Requests is a module that does not come with Python, so you’ll have to download and install it. The payoff is the ability to download files from the web without worrying about complicated issues such as network errors or connection problems. Once installed, you can use it to easily download URLs for automatic posts.

Using a few simple lines of code, you can select a strong of a URL to download, determine the request status from the web server, and download the text from the relevant URL. For example, if your site reposts articles from larger sites, you can use Python programming to search by title, specify key terms, and even establish short descriptions. This makes it easy to automatically scrap the Internet for content applicable to your site, collect the proper information, and download or repost to your site along with a description.

From time to time you may run into errors in downloading or automatically posting. Using the “raise_for_status()” code you can ensure that a program halts its progress if a bad search and download occurs. In so doing, you ensure that the download/search has worked before allowing your Python program to continue with its automated task.

 

 

 

How to Use Python to Create Interactive Websites

There are various Python web formats available to help you build an interactive website, one which features dropdown menus, polls, or information that automatically refreshes to keep readers in the know, such as Django, Grok, WebPy, and TurboGears. There are various benefits to each program, including the following:

  • Grok: Advanced feature sets that make it one of the top choices for creating interactive content and dynamic websites with Python programming.
  • WebPy: Lightweight framework that is easy to start with, but does have fewer features.
  • TurboGears: Overcame an early reputation for poor documentation to boast improved sustainability in recent years.
  • Django: Widely regarded as the top choice for creating interactive content with Python programming, Django offers in-depth documentation and a massive number of features, while remaining easy to use.

Regardless of the interactive content or website you’re building, you need to start by developing a Django project to enter your Python coding. There are four simple steps to follow to create a project:

Cd ~/Documents/Projects
Django-admin.py startproject “project name here”
Cd “project name”
Ls

These steps help you start building a website with interactive content using Python programming. You’ll establish a foundation that recognizes each folder as a Python package, allows you to manage the functions of your site, control settings, and map URLs to the pages on your site.

A key facet of Django is the fact that it is a so-called DRY program. In short, this means “Do Not Repeat Yourself.” Using Django, you should never write the same code to perform a singular task more than one time. For example, a Python programming code that picks a random article from your archives can be implemented on multiple pages, but should not be written multiple times. Instead, you write the code once and use it on the different pages rather than rewriting it every time.

Django relies on the creation of apps to make a functional website. The steps laid out above merely form the foundation for your interactive content. Apps offer the environment wherein you’ll actually write the codes that make your website function. Each project you would create can contain multiple apps. Even better, those multiple apps can be reused later in different projects. For example, if you wanted to use a similar Python programming approach to pull random articles from the archives of a different site you’re creating, you won’t have to rewrite that code all over again.

Instead, you can use the app you created to cull and post that information in another project. The code is already written, so all you have to do is import it into that new project. However, it’s important to note that as a result, each of the apps you build during the course of a project should serve just one specific purpose. For example, if you were to write all of your Python programming code into one app and wanted to reuse only a portion of that in a later project, you still have to import all of those features into the new project, creating waste and confusion.

So, if you make one app for random article pulling and a separate one for a blog publishing system (as an example), you can pull the individual apps that you created for use on later projects without bringing over all of the functions. The biggest benefit of apps in Django though is the ability to keep codes well organized.

If you want to alter one feature of an app, you won’t search through your entire project/website to do so. Instead, you browse through only the relevant app and makes the changes necessary to accommodate individual projects without interfering with functionality of the app on a different project. In order to load your created website to a server, you’ll need to verify Django is installed on that server, or install it yourself. You can use any file-transfer client to load your newly-created site to the server by placing the files/apps in a private folder.

Next, you can create a MySQL database by running syncdb. After creating an account for the admin control panel (a one-time task) you’ll be ready to take your dynamic website live. You can run the program on the server and see your site using the command

python manage.py runserver 0.0.0.0:8000

Once your dynamic website has been created, what’s an example of dynamic graphics or other interactive content you can add using Python programming?

How to Use Python to Add Graphics, Dynamic, or Interactive Content

One of the most common examples of graphics or interactive content is an online poll on a website or blog. These are the perfect example of interactive and dynamic content, as the results update every time viewers vote and engage readers. You can use Django to create a project app, just as you would at the start to create your dynamic website, this time using a service such as Microsoft Azure to publish to your new site. After creating a project in the management console within Django, you can build your poll using a sample poll format that allows you to enter specific information and values, including the poll question and possible answers.

From here you’d need to create a MySQL database to configure the project for your website and publish the app to your site. After you login, you’ll use the navigation panel to click NEW, DATA+Storage > MySQL Database. Next you’ll need to configure the database with a new resource group and select the proper location for it. Once created, you can click the properties tap in the database blade and enter a connection string on the clipboard.

Last but not least, you’ll want to configure the web app for use in the MySQL database so your dynamic content displays correctly.

You’ll start by opening settings.py in the Projectname folder and temporarily paste the connection string from the previous step into the editor. In the following steps you’ll be asked to set values for the name, user password, and host from the connection string. After entering that information, you’re ready to install the Python package. The easy-install option will walk you through this process. Finally, you right-click on your project node and publish through the Solution Explorer.

Click on Microsoft Azure Web Apps => New, and fill in information such as the web app name, app service plan, resource group, and region, then click Create.

You can simply accept the other defaults and click Publish, and your browser will open to the new app on your site.

Leave a Reply

Your email address will not be published. Required fields are marked *