Table of contents
1.
Introduction
2.
Web2py as Teaching Tool
3.
Wiki Page
4.
Model
5.
Controller
6.
Views
7.
Frequently Asked Questions 
7.1.
Do web applications work well with Python?
7.2.
Describe the web2py framework.
7.3.
Should one learn web2py?
7.4.
How to run web2py?
7.5.
Is web2py an open-source project?
8.
Conclusion
Last Updated: Mar 27, 2024
Medium

How to Make a Simple Wiki Page in Web2py?

Author Kanak Rana
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Web2py is a free, open-source web framework that is written in Python and programmable in Python. web2py is a full-stack framework, which means it includes all of the components required to create fully functional web applications.

  • Web2py is intended to assist a web developer in adhering to good software engineering practices, such as the Model View Controller (MVC) pattern. web2py decouples data representation (the model) from data presentation (the view), as well as application logic and workflow (the controller).
     
  • Web2py provides libraries to assist developers in designing, implementing and testing each of these three components separately, as well as making them work together.
     

Although there are many web application frameworks, web2py has some standout qualities. 

web2py

Web2py as Teaching Tool

The following main reasons were the driving forces behind the creation of web2py as a teaching tool:

  • Server-side web development is simple for users to learn without sacrificing functionality. 
     
  • Web2py has no installation or configuration requirements, doesn't depend on anything (aside from the source code distribution, which needs Python 2.7 or 3.5+ and their standard library modules), and exposes the majority of its functionality via a Web interface, including an Integrated Development Environment with Debugger and database interface.
     
  • Because it uses a top-down approach to design- that is, its API was created before it was implemented—web2py has been stable from the start. 
     
  • Web2py has never broken backward compatibility even as new features have been added, and it won't break compatibility when new features are added in the future.

Wiki Page

Here, we create a specific wiki from scratch using only low-level APIs. The user can create, update, and search pages (by title). Additionally, the visitor can add comments, documents (as attachments to the pages), and links to such documents. We follow the Markmin syntax for our wiki syntax as a convention. An RSS feed for the sites, an XML-RPC handler, and an Ajax search page will be implemented. The actions we must take and the connections we want to make are shown in the diagram below.

Steps actions

We will start by establishing a new framework application called "Coding_Ninjas."

This is the front page for the wiki page, and the files inside it have all the source code:

first view of the page

Page, comment, and document tables must be present in the model. Both comments and the referenced document should be on the same page. A file field of the upload type, similar to the previous image application, is present in a document.

The entire model is provided below.

Model

Page, comment, and document are the three tables that make up the model. Both comments and the referenced document should be on the same page. A file field of the upload type, just like in the previous image application, is present in a document.

db = DAL('sqlite://storage.sqlite')

From gluon. tools import *
auth = Auth(db)
auth.define_tables()
crud = Crud(db)

db.define_table('page',
                Field('title'),
                Field('body', 'text'),
                Field('created_on', 'datetime', default=request.now),
                Field('created_by', 'reference auth_user', default=auth.user_id),
                format='%(title)s')

db.define_table('post',
                Field('page_id', 'reference page'),
                Field('body', 'text'),
                Field('created_on', 'datetime', default=request.now),
                Field('created_by', 'reference auth_user', default=auth.user_id))

db.define_table('document',
                Field('page_id', 'reference page'),
                Field('name'),
                Field('file', 'upload'),
                Field('created_on', 'datetime', default=request.now),
                Field('created_by', 'reference auth_user', default=auth.user_id),
                format='%(name)s')

db.page.title.requires = IS_NOT_IN_DB(db, 'page.title')
db.page.body.requires = IS_NOT_EMPTY()
db.page.created_by.readable = db.page.created_by.writable = False
db.page.created_on.readable = db.page.created_on.writable = False

db.post.body.requires = IS_NOT_EMPTY()
db.post.page_id.readable = db.post.page_id.writable = False
db.post.created_by.readable = db.post.created_by.writable = False
db.post.created_on.readable = db.post.created_on.writable = False

db.document.name.requires = IS_NOT_IN_DB(db, 'document.name')
db.document.page_id.readable = db.document.page_id.writable = False
db.document.created_by.readable = db.document.created_by.writable = False
db.document.created_on.readable = db.document.created_on.writable = False
You can also try this code with Online Python Compiler
Run Code
model code

Controller

The following actions were added to the default.py controller in the controller.

Index: List of all wiki pages.

Create: A new wiki page to be added.

Show: A wiki page and any comments on it, as well as add new comments.

Edit: Modify a current page.

Document: Manage the documents that are attached to a page.

Search: Display a search box and, as the user inputs, have an Ajax callback to return any titles that match.

Callback: Ajax's callback function gives back the HTML inserted into the search results page as the user types.

def index():
""" This controller provides a dictionary, which is what the view that lists all wiki pages has produced.
 >>> index().has_key('pages')
True
"""
page = db().select(db.page.id, db.page.title, orderby=db.page.title)
return dict(pages=pages)

@auth.requires_login()
def create():
 """ Build a new empty wiki page."""
  form = SQLFORM(db.page).process(next=URL('index'))
  return dict(form=form)
  
def show():
 """ Display a wiki page"""
 
	this_page= db.page(request.args(0, cast=int)) or redirect(URL('index'))
	db.post.page_id.default = this_page.id
	form = SQLFORM(db.post).process() if auth.user else None
	page comments = db(db.post.page_id == this_page.id).select(orderby=db.post.id)
	return dict(page=this_page, comments=pagecomments, form=form)

@auth.requires_login()
def edit():
 	""" Edit an current wiki page"""
 
    this_page = db.page(request.args(0, cast=int)) or redirect(URL('index'))
	form = SQLFORM(db.page, this_page).process(
	next = URL('show', args=request.args))
	return dict(form=form)
	
@auth.requires_login()
def documents():
	""" Browser, edit every file associated with that page."""

	page = db.page(request.args(0, cast=int)) or redirect(URL('index'))
	db.document.page_id.default = page.id
	grid = SQLFORM.grid(db.document.page_id == page.id, args=[page.id])
	return dict(page=page, grid=grid)
	def user():
	return dict(form=auth())
	
def download():
	""" Allows document downloads"""

	return response.download(request, db)
	
def search():
	""" An ajax wiki search page"""
	
	return dict(form=FORM(INPUT(_id='keyword',_name='keyword',_onkeyup="ajax('callback', ['keyword'], 'target');")),
	target_div=DIV(_id='target'))
	
def callback():
	""" An ajax callback that returns a list of links to wiki pages in the form of an <ul>."""

	query = db.page.title.contains(request.vars.keyword)
	pages = db(query).select(orderby=db.page.title)
	links = [A(p.title, _href=URL('show', args=p.id)) for p in pages]
	return UL(*links)
	
def news():
	""" Creates an RSS feed from wiki pages."""
	
	response.generic_patterns = ['.rss']
	pages = db().select(db.page.ALL, orderby=db.page.title)
	return dict(title='mywiki RSS feed',
	link='http://127.0.0.1:8000/mywiki/default/index',
	description='mywiki news',created_on=request.now,
	items=[dict(title=row.title,
	link=URL('show', args=row.id, scheme=True, host=True, extension=False),
		description=MARKMIN(row.body).xml(),
		created_on=row.created_on) for row in pages])
	service = Service()
	
@service.xmlrpc
def find_by(keyword):
	""" For XML-RPC, finds pages that include the term."""
	
	return db(db.page.title.contains(keyword)).select().as_list()
def call():
	""" Exposes all logged-in services, including XML-RPC."""
	
return service()
You can also try this code with Online Python Compiler
Run Code

 

Controller code

Views

The application's view component aids in providing users with the data display. Data is pulled from the model for the data display.

default/create.html

{{extend 'layout.html'}}
<h1>Create new wiki page</h1>
{{=form}}
create a new wiki page

default/index.html

{{extend 'layout.html'}}
<h1>Available wiki pages</h1>
[ {{=A('search', _href=URL('search'))}} ]<br />
<ul>{{for page in pages:}}
     {{=LI(A(page.title, _href=URL('show', args=page.id)))}}
{{pass}}</ul>
[ {{=A('create page', _href=URL('create'))}} ]

It generates the following page:

index.html

This page will show the available wiki pages that you have made earlier:

search history

default/show.html

{{extend 'layout.html'}}
<h1>{{=page.title}}</h1>
[ {{=A('edit', _href=URL('edit', args=request.args))}} ]<br />
{{=MARKMIN(page.body)}}
<h2>Comments</h2>
{{for the post in comments:}}
  <p>{{=db.auth_user[post.created_by].first_name}} on {{=post.created_on}}
     says <i>{{=post.body}}</i></p>
{{pass}}
<h2>Post a comment</h2>
{{=form}}

 

main ouptut

default/edit.html

{{extend 'layout.html'}}
<h1>Edit wiki page</h1>
[ {{=A('show', _href=URL('show', args=request.args))}}
| {{=A('documents', _href=URL('documents', args=request.args))}} ]<br />
{{=form}}

It creates a page that resembles the create page almost exactly.

edit.html

default/document.html

{{extend 'layout.html'}}
<h1>Documents for page: {{=page.title}}</h1>
[ {{=A('show', _href=URL('show', args=request.args))}} ]<br />
<h2>Documents</h2>
{{=grid}}
document.html

Here you can put images that you want to add to the page(here, we have added the Coding Ninjas logo):

Document page

The logo is shown in image below:

adding coding ninjas logo

default/search.html

{{extend 'layout.html'}}
<h1>Search wiki pages</h1>
[ {{=A('listall', _href=URL('index'))}}]<br />
{{=form}}<br />{{=target_div}}

which creates the Ajax search form seen below:

search.html

So, this is how one can create a simple wiki page in Web2py.

Frequently Asked Questions 

Do web applications work well with Python?

Python offers various tools and frameworks, making it a strong choice for creating web applications.

Describe the web2py framework.

Web2py, written in Python and programmable in Python, is described as a free, open-source online framework for agile development that involves database-driven web applications.

Should one learn web2py?

Web2py is a top-notch framework, yes. The simplicity of usage, from installation to learning to code to distribution to deployment, is a key objective of web2py.

How to run web2py?

Web2py doesn't need to be installed. Start by running the corresponding web2py.py file after unzipping the downloaded zip file for your particular operating system.

Is web2py an open-source project?

Web2py is a free, open-source web framework for developing secure database-driven web applications in an agile manner.

Conclusion

In this article, we have learned about how to make a simple wiki page in web2py and in which framework it runs, and model, controller, and view in detail. To know more in detail about web2py and the simple wiki page in web2py, you can take a look at the following official articles.


You can check out more articles here other than a simple wiki page in web2py:

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enroll in our courses and refer to the mock test and problems available; look at the Top 150 Interview Puzzles interview experiences, and interview bundle for placement preparations.

Thank you

Do upvote our blog to help other ninjas grow. 

Happy Learning Ninja!

Live masterclass