Do you think IIT Guwahati certified course can help you in your career?
No
Introduction📑
This article in the Developing with Bottle Framework series will focus on HTML forms and GET and POST queries. It'll also demonstrate how to use the plot.ly API to access data. Additionally, you'll learn how to make a fantastic graph displaying cohort analysis research findings.
Also, did you miss the Developing with Bottle Framework- Part 1? If yes, then worry not! Check it out here. Additionally, Bottle version 0.12.3 and Plotly version 1.2.6 are used in this tutorial.
Basic Configuration🧊
Download this Gist from Part 1 first, then execute it by running the following command:
$bash bottle.sh
Thus, a basic project structure will be produced:
├── app.py
├── requirements.txt
└── testenv
Start the virtualenv in Bottle Framework:
$ cd bottle
$ source testenv/bin/activate
Install the necessary components:
$ pip install -r requirements.txt
To generate a new API key, go to this, register a new account, sign in, and then:
Copy the key and Set up plot.ly:
$ pip intall plotly== 1.2.6
The code in app.py will now be updated:
import os
from bottle import run, template, get, post, request
import plotly.plotly as py
from plotly.graph_objs import *
# add your username and API key
py.sign_in("Ninja", "BllXKIYQan1dvZQOX3e0")
@get('/plot')
def form():
return '''<h2>Graph via Plot.ly</h2>
<form method="POST" action="/plot">
Name: <input name="mobile1" type="text" />
Age: <input name="cost1" type="text" /><br/>
Name: <input name="mobilr2" type="text" />
Age: <input name="cost2" type="text" /><br/>
Name: <input name="mobile3" type="text" />
Age: <input name="cost3" type="text" /><br/>
<input type="submit" />
</form>'''
@post('/plot')
def submit():
# grab data from form
name1 = request.forms.get('mobile1')
age1 = request.forms.get('cost1')
name2 = request.forms.get('mobile2')
age2 = request.forms.get('cost2')
name3 = request.forms.get('mobile3')
age3 = request.forms.get('cost3')
data = Data([
Bar(
x=[mobile1, mobile2, mobile3],
y=[cost1, cost2, cost3]
)
])
# make API call
response = py.plot(data, filename='bar')
if response:
return template('''
<h1>HIIII!</h1>
<div>
here is your graph: <a href="{{response}}"</a>{{response}}
</div>
''',
response=response
)
if __name__ == '__main__':
port = int(os.environ.get('PORT', 8080))
run(host='0.0.0.0', port=port, debug=True)
What is happening here?👀
⭐The first function, form(), generates an HTML form to collect the information required to produce a basic bar graph.
⭐The second function, submit(), takes the inputs from the form, assigns them to variables, and then invokes the plot.ly API, providing the data and our login information, to create a new chart. Make sure to use your credentials to replace my username and API key. Don't use mine. It won't function.
Test💡
Run your program locally, type http://localhost:8080/plot into your browser, and then run app.py in Python.
List the mobile brands and their prices. After pressing submit, you should see a congratulations message with a URL if everything went correctly. To view your graph, click here.
Cohort Analysis📈
Let's now examine a more challenging case to make a graph for the following cohort analysis statistics in Bottle Framework:
We'll extend the same app, app.py, but create a new file: Open app.py, then “Save As” cohort.py.
Please upgrade to the Simple Template Engine first so we may customize our templates with styles and Javascript libraries. Create a new directory called "views" and a new file called template.tpl. This code should be added to that file:
This appears to be an HTML file, as you can probably guess. The distinction is that we may now use the syntax python variable to pass Python variables to the file.
Add your Plot.ly username and API key to a data.json file in Bottle Framework. The sample file is accessible here.
When we make the API connection, we'll utilize the credentials from the JSON. To access the information, add the following code to cohort.py.
Python
import os
from bottle import run, template, get, post, request
import plotly.plotly as py
from plotly.graph_objs import *
import json
# grab username and key from config/data file
with open('data.json') as config_file:
config_data = json.load(config_file)
username = config_data["user"]
key = config_data["key"]
py.sign_in(username, key)
You can also try this code with Online Python Compiler
The return statement is visible. Along with any variables, we're sending in the template name. Let's build the new template, which will be called template2.tpl in Bottle Framework:
We may update the form and display the accurate information or chart with the updated changes due to the iframe. To display the graph, we do not need to navigate away from the current page.
Run it. Increase the form's value and then send it. You should see the following in your graph made using the Bottle Framework:
Frequently Asked Questions
What is Bottle API?
The Bottle Framework is a Python WSGI micro web framework that is quick, easy, and lightweight.
What are Flask and Django?
Django, a full-stack web framework, offers applications that are ready to use with its batteries-included approach. Flask is a simple framework with many built-in functionalities, none of which require third-party libraries.
Which is better suited, a bottle or a flask?
Flask supports many features and offers helpful assistance through online forums or tutorials. The Bottle Framework is best for projects that must be completed rapidly.
Define Falcon API.
Falcon is a lightning-quick, simplistic Python web API framework for creating reliable app backends and microservices.
What does a JSON in Python mean?
A standardized format called JavaScript Object Notation (JSON) is frequently used to transmit data as text across a network.
Conclusion
In this blog, we have extensively discussed how to plot a graph and how to study cohort analysis using Bottle Framework in Python. Now go ahead, try to plot a similar chart, and see for yourself how it works!