0

I'm trying to make a python script using jupyter-notebook, which is fetching data from my website's sql-server and I want to call this script using a javascript function every time the page is loaded. So the page will have the Plotly graphs. Here is my code:

# coding: utf-8

# In[1]:


#import os
#os.chdir("D:/Datasets/Trell")


# In[2]:


import json
from pandas.io.json import json_normalize
import pandas as pd
import numpy as np
import warnings
warnings.filterwarnings('ignore')

from plotly.offline import init_notebook_mode,plot, iplot
init_notebook_mode(connected=True)
import plotly.graph_objs as go
import plotly.offline as offline
offline.init_notebook_mode()
import plotly.tools as tls



# In[3]:


# importing the requests library
import requests

# api-endpoint
URL = "https://*****.co.in/*****/*******.php"

# location given here
token= '************'

query= 'SELECT userId,createdAt,userName,trails_count,bio FROM users WHERE createdAt >= "2018-07-01"'

# defining a params dict for the parameters to be sent to the API
PARAMS = {'token':token, 'query':query}

# sending get request and saving the response as response object
r = requests.post(url = URL, data = PARAMS)


# In[4]:


data=r.json()


# In[5]:


df=pd.DataFrame(data)


# In[6]:


df.head(1)


# In[7]:


df['date'] = pd.DatetimeIndex(df.createdAt).normalize()


# In[8]:


df['user']=1


# In[9]:


df_user=df.groupby(['date'],as_index=False)['user'].agg('sum')


# In[10]:


data = [go.Scatter( x=df_user['date'], y=df_user['user'] )]
plot(data, filename='time-series.')


# In[11]:


df_user['day_of_week']=df_user['date'].dt.weekday_name
df_newuser_day=df_user.groupby(['day_of_week'],as_index=False)['user'].agg('sum')
df_newuser_day=df_newuser_day.sort_values(['user'],ascending=False)

trace = go.Bar(
    x=df_newuser_day['day_of_week'],
    y=df_newuser_day.user,
    marker=dict(
        color="blue",
        #colorscale = 'Blues',
        reversescale = True
    ),
)

layout = go.Layout(
    title='Days of Week on which max. users register (July)'
)

data = [trace]
fig = go.Figure(data=data, layout=layout)
plot(fig, filename="medal.")

But the problem is that every time the plot() function is executed new HTML tabs are getting open with the filename= mentioned inside the function.

All I want is when I'm executing the file all the graphs come under single HTML page and also I want to give header with <h1> tag before every plot is being so that to the plots are understandable. So is there a way I can do that along with adding of some HTMl and CSS tags before plotly plots so that it looks like a clean webpage with all the plotly graphs along with the headers mentioned under the <h1> tag.

Like I want all the graphs to appear on the same page together one after the other.

P.S. I don't want to use iplot because it plots in the same notebook only and doesn't save the file also.

Debadri Dutta
  • 1,183
  • 1
  • 13
  • 39

2 Answers2

1

To make the plots appear in the same page, please use plotly offline's iplot method, instead of plot.

So the statement.

plot(fig, filename="medal.")

will become.

iplot(fig)

If you wish to add HTML before the plot, please use the display and HTML provided by ipython.

from IPython.core.display import display, HTML
display(HTML('<h1>Hello, world!</h1>'))
iplot(fig)

Thus, first we can insert the html first and then plot the graph!

To know more, visit this SO Answer

Naren Murali
  • 19,250
  • 3
  • 27
  • 54
  • But that plots inside the notebook only. When we are using `plot()` function plots are displayed in new tabs, not inside notebook. So I want it to get displayed on those tabs only, not inside the notebook itself, but yes I want all the graphs to get plotted in one single tab only after the other. Is it possible? – Debadri Dutta Aug 08 '18 at 08:18
1

Late reply: subplots may be the answer to this problem.

For example, to create a subplots of 2 rows and 2 columns,

from plotly import tools
plots = tools.make_subplots(rows=2, cols=2, print_grid=True)
Guddi
  • 55
  • 8