Saul Salcedo
Saul Salcedo

Reputation: 79

TypeError: render_template() takes 1 positional argument but 2 were given

I'm trying to add a template to my code with Jinja but it sends me the error. What this program does is to graph in real time and I want to add a bit of design with CSS.

This is the code:

import dash
from dash.dependencies import Output, Input
import dash_core_components as dcc
import dash_html_components as html
import plotly
import random
import plotly.graph_objs as go
from collections import deque
from flask import Flask, render_template
from flask import request, jsonify

X = deque(maxlen=20)
Y = deque(maxlen=20)
X.append(1)
Y.append(1)

app = dash.Dash(__name__)
app.layout = html.Div(
    [
        dcc.Graph(id='live-update-graph', animate=True),
        dcc.Interval(
            id='interval-component',
            interval=3000,
            n_intervals=0
            )
        ]
    )

@app.callback(Output('live-update-graph', 'figure'),
        [Input('interval-component', 'n_intervals')])
def update_graph(n):
    X.append(X[-1]+1)
    Y.append(Y[-1]+(Y[-1]*random.uniform(-0.1,0.1)))

data = go.Scatter(
    x = list(X),
    y = list(Y),
    name = 'Scatter',
    mode = 'lines+markers'
    )

return render_template('template.html', {'data':[data], 'layout': go.Layout(xaxis = dict(range=[min(X), max(X)]),
                                           yaxis = dict(range=[min(Y), max(Y)]))})

if __name__ == '__main__':
    app.run_server(debug=True)

Thank you. Regards.

Upvotes: 4

Views: 14960

Answers (1)

Raviteja Ainampudi
Raviteja Ainampudi

Reputation: 282

render_template() accepts only one argument and it should the name of a HTML file(s) as a string.

Read this And from that documentation

flask.render_template(template_name_or_list, **context)

Renders a template from the template folder with the given context.

Parameters:

template_name_or_list – the name of the template to be rendered, or an iterable with template names the first one existing will be rendered

context – the variables that should be available in the context of the template.

Context are key word arguments, which are used to pass into Jinja2 templating in HTML. You placed all your key args as a dictionary. Which are not accepted for context in Flask.

return render_template('template.html', data=[data], layout=go.Layout(xaxis = dict(range=[min(X), max(X)]), yaxis = dict(range=[min(Y), max(Y)]))) 

The keys in keyword args should not be given as strings. It should be data and layout. Not 'data' and 'layout'.

Upvotes: 8

Related Questions