Forums

How to I print variables to the console?

I have a weird issue at the moment where text I am inserting into my MySQL database in an action menu function in Django admin are getting an 'n' character attached to the left side of the string. This is only happening on PythonAnywhere. I am developing the app predominately on a localhost server on my own machine and the error does not show up there.

What I want to know mainly is how I can print out a variable to a Bash console instance so that I can diagnose what is going on. I tried writing print(word) in the action function, but the output was not echoed to the Bash console I had open in the Virtual Environment (django18) in which my app is running. Are there additional steps I must take before I can get variables to print to the console?

Alternatively, if anyone has some idea why an 'n' would be pre-pended to text I am inserting into my database, this would be very helpful information. At first I thought it was because the input variables were unicode text, but encoding them to utf-8 before inserting them made no difference.

The complete source for my action function is below:

from django.contrib import admin
from django.forms import Textarea
from django.db import models

from .frequency import VocabularyProcess
from .models import School, Grade, Book, Bookmark, Word


# Action menu item
def process_content(modeladmin, request, queryset):
    # Get the text from the database bookmark item
    table = queryset.values_list('id', 'content')
    # Process each record in the queryset
    for record in table:
        bookmark_id = int(record[0])
        content_text = str(record[1].encode('ascii', 'ignore')).lower()
        # make it into a list of lines
        content_list = content_text.splitlines()
        # Process the text into a dictionary of words with counts
        vocabulary_obj = VocabularyProcess(content_list)
        words = vocabulary_obj.get_frequency_dict()
        # Update or create the words and counts in the Words table.
        for word, count in words.items():
            # Encode to utf-8 for compatibility with current DB char encoding
            word = word.encode('utf-8')
            if len(word) <= 20:  # Ensure the word will fit in the DB record
                Word.objects.update_or_create(word=word, \
                           bookmark_id=bookmark_id, defaults={'count': count})

I seem to have resolved the problem which prompted my question. This statement a few lines in seems to causing the issue:

content_text = str(record[1].encode('ascii', 'ignore')).lower()

Once I removed the the function "encode('ascii', 'ignore'))" the text was correctly inserted into the database.

I still want to know how to print variables to the Bash console though to make future diagnostic issues easier.

You can't print to a console from a web app, they are nowhere near each other. You can print to the log files of your web app. Any output you send to stderr will end up in your logs. Depending on what your framework has done with logging it may be in your server log or it may be in your error log.